patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / pci / hotplug / shpchprm_acpi.c
1 /*
2  * SHPCHPRM ACPI: PHP Resource Manager for ACPI platform
3  *
4  * Copyright (C) 2003-2004 Intel Corporation
5  *
6  * All rights reserved.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or (at
11  * your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
16  * NON INFRINGEMENT.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * Send feedback to <dely.l.sy@intel.com>
24  *
25  */
26
27 #include <linux/config.h>
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/types.h>
31 #include <linux/pci.h>
32 #include <linux/init.h>
33 #include <linux/acpi.h>
34 #include <linux/efi.h>
35 #include <asm/uaccess.h>
36 #include <asm/system.h>
37 #ifdef  CONFIG_IA64
38 #include <asm/iosapic.h>
39 #endif
40 #include <acpi/acpi.h>
41 #include <acpi/acpi_bus.h>
42 #include <acpi/actypes.h>
43 #include "shpchp.h"
44 #include "shpchprm.h"
45
46 #define PCI_MAX_BUS             0x100
47 #define ACPI_STA_DEVICE_PRESENT 0x01
48
49 #define METHOD_NAME__SUN        "_SUN"
50 #define METHOD_NAME__HPP        "_HPP"
51 #define METHOD_NAME_OSHP        "OSHP"
52
53 #define PHP_RES_BUS             0xA0
54 #define PHP_RES_IO              0xA1
55 #define PHP_RES_MEM             0xA2
56 #define PHP_RES_PMEM            0xA3
57
58 #define BRIDGE_TYPE_P2P         0x00
59 #define BRIDGE_TYPE_HOST        0x01
60
61 /* this should go to drivers/acpi/include/ */
62 struct acpi__hpp {
63         u8      cache_line_size;
64         u8      latency_timer;
65         u8      enable_serr;
66         u8      enable_perr;
67 };
68
69 struct acpi_php_slot {
70         struct acpi_php_slot    *next;
71         struct acpi_bridge      *bridge;
72         acpi_handle             handle;
73         int     seg;
74         int     bus;
75         int     dev;
76         int     fun;
77         u32     sun;
78         struct pci_resource *mem_head;
79         struct pci_resource *p_mem_head;
80         struct pci_resource *io_head;
81         struct pci_resource *bus_head;
82         void    *slot_ops;      /* _STA, _EJx, etc */
83         struct slot *slot;
84 };              /* per func */
85
86 struct acpi_bridge {
87         struct acpi_bridge      *parent;
88         struct acpi_bridge      *next;
89         struct acpi_bridge      *child;
90         acpi_handle     handle;
91         int seg;
92         int pbus;                               /* pdev->bus->number            */
93         int pdevice;                            /* PCI_SLOT(pdev->devfn)        */
94         int pfunction;                          /* PCI_DEVFN(pdev->devfn)       */
95         int bus;                                /* pdev->subordinate->number    */
96         struct acpi__hpp                *_hpp;
97         struct acpi_php_slot    *slots;
98         struct pci_resource     *tmem_head;     /* total from crs       */
99         struct pci_resource     *tp_mem_head;   /* total from crs       */
100         struct pci_resource     *tio_head;      /* total from crs       */
101         struct pci_resource     *tbus_head;     /* total from crs       */
102         struct pci_resource     *mem_head;      /* available    */
103         struct pci_resource     *p_mem_head;    /* available    */
104         struct pci_resource     *io_head;       /* available    */
105         struct pci_resource     *bus_head;      /* available    */
106         int scanned;
107         int type;
108 };
109
110 static struct acpi_bridge *acpi_bridges_head;
111
112 static u8 * acpi_path_name( acpi_handle handle)
113 {
114         acpi_status             status;
115         static u8       path_name[ACPI_PATHNAME_MAX];
116         struct acpi_buffer              ret_buf = { ACPI_PATHNAME_MAX, path_name };
117
118         memset(path_name, 0, sizeof (path_name));
119         status = acpi_get_name(handle, ACPI_FULL_PATHNAME, &ret_buf);
120
121         if (ACPI_FAILURE(status))
122                 return NULL;
123         else
124                 return path_name;       
125 }
126
127 static void acpi_get__hpp ( struct acpi_bridge  *ab);
128 static void acpi_run_oshp ( struct acpi_bridge  *ab);
129
130 static int acpi_add_slot_to_php_slots(
131         struct acpi_bridge      *ab,
132         int                             bus_num,
133         acpi_handle             handle,
134         u32                             adr,
135         u32                             sun
136         )
137 {
138         struct acpi_php_slot    *aps;
139         static long     samesun = -1;
140
141         aps = (struct acpi_php_slot *) kmalloc (sizeof(struct acpi_php_slot), GFP_KERNEL);
142         if (!aps) {
143                 err ("acpi_shpchprm: alloc for aps fail\n");
144                 return -1;
145         }
146         memset(aps, 0, sizeof(struct acpi_php_slot));
147
148         aps->handle = handle;
149         aps->bus = bus_num;
150         aps->dev = (adr >> 16) & 0xffff;
151         aps->fun = adr & 0xffff;
152         aps->sun = sun;
153
154         aps->next = ab->slots;  /* cling to the bridge */
155         aps->bridge = ab;
156         ab->slots = aps;
157
158         ab->scanned += 1;
159         if (!ab->_hpp)
160                 acpi_get__hpp(ab);
161
162         acpi_run_oshp(ab);
163
164         if (sun != samesun) {
165                 info("acpi_shpchprm:   Slot sun(%x) at s:b:d:f=0x%02x:%02x:%02x:%02x\n", aps->sun, ab->seg, 
166                         aps->bus, aps->dev, aps->fun);
167                 samesun = sun;
168         }
169         return 0;
170 }
171
172 static void acpi_get__hpp ( struct acpi_bridge  *ab)
173 {
174         acpi_status             status;
175         u8                      nui[4];
176         struct acpi_buffer      ret_buf = { 0, NULL};
177         union acpi_object       *ext_obj, *package;
178         u8                      *path_name = acpi_path_name(ab->handle);
179         int                     i, len = 0;
180
181         /* get _hpp */
182         status = acpi_evaluate_object(ab->handle, METHOD_NAME__HPP, NULL, &ret_buf);
183         switch (status) {
184         case AE_BUFFER_OVERFLOW:
185                 ret_buf.pointer = kmalloc (ret_buf.length, GFP_KERNEL);
186                 if (!ret_buf.pointer) {
187                         err ("acpi_shpchprm:%s alloc for _HPP fail\n", path_name);
188                         return;
189                 }
190                 status = acpi_evaluate_object(ab->handle, METHOD_NAME__HPP, NULL, &ret_buf);
191                 if (ACPI_SUCCESS(status))
192                         break;
193         default:
194                 if (ACPI_FAILURE(status)) {
195                         err("acpi_shpchprm:%s _HPP fail=0x%x\n", path_name, status);
196                         return;
197                 }
198         }
199
200         ext_obj = (union acpi_object *) ret_buf.pointer;
201         if (ext_obj->type != ACPI_TYPE_PACKAGE) {
202                 err ("acpi_shpchprm:%s _HPP obj not a package\n", path_name);
203                 goto free_and_return;
204         }
205
206         len = ext_obj->package.count;
207         package = (union acpi_object *) ret_buf.pointer;
208         for ( i = 0; (i < len) || (i < 4); i++) {
209                 ext_obj = (union acpi_object *) &package->package.elements[i];
210                 switch (ext_obj->type) {
211                 case ACPI_TYPE_INTEGER:
212                         nui[i] = (u8)ext_obj->integer.value;
213                         break;
214                 default:
215                         err ("acpi_shpchprm:%s _HPP obj type incorrect\n", path_name);
216                         goto free_and_return;
217                 }
218         }
219
220         ab->_hpp = kmalloc (sizeof (struct acpi__hpp), GFP_KERNEL);
221         memset(ab->_hpp, 0, sizeof(struct acpi__hpp));
222
223         ab->_hpp->cache_line_size       = nui[0];
224         ab->_hpp->latency_timer         = nui[1];
225         ab->_hpp->enable_serr           = nui[2];
226         ab->_hpp->enable_perr           = nui[3];
227
228         dbg("  _HPP: cache_line_size=0x%x\n", ab->_hpp->cache_line_size);
229         dbg("  _HPP: latency timer  =0x%x\n", ab->_hpp->latency_timer);
230         dbg("  _HPP: enable SERR    =0x%x\n", ab->_hpp->enable_serr);
231         dbg("  _HPP: enable PERR    =0x%x\n", ab->_hpp->enable_perr);
232
233 free_and_return:
234         kfree(ret_buf.pointer);
235 }
236
237 static void acpi_run_oshp ( struct acpi_bridge  *ab)
238 {
239         acpi_status             status;
240         u8                      *path_name = acpi_path_name(ab->handle);
241         struct acpi_buffer      ret_buf = { 0, NULL};
242
243         /* run OSHP */
244         status = acpi_evaluate_object(ab->handle, METHOD_NAME_OSHP, NULL, &ret_buf);
245         if (ACPI_FAILURE(status)) {
246                 err("acpi_pciehprm:%s OSHP fails=0x%x\n", path_name, status);
247         } else
248                 dbg("acpi_pciehprm:%s OSHP passes =0x%x\n", path_name, status);
249         return;
250 }
251
252 static acpi_status acpi_evaluate_crs(
253         acpi_handle             handle,
254         struct acpi_resource    **retbuf
255         )
256 {
257         acpi_status             status;
258         struct acpi_buffer              crsbuf;
259         u8                      *path_name = acpi_path_name(handle);
260
261         crsbuf.length  = 0;
262         crsbuf.pointer = NULL;
263
264         status = acpi_get_current_resources (handle, &crsbuf);
265
266         switch (status) {
267         case AE_BUFFER_OVERFLOW:
268                 break;          /* found */
269         case AE_NOT_FOUND:
270                 dbg("acpi_shpchprm:%s _CRS not found\n", path_name);
271                 return status;
272         default:
273                 err ("acpi_shpchprm:%s _CRS fail=0x%x\n", path_name, status);
274                 return status;
275         }
276
277         crsbuf.pointer = kmalloc (crsbuf.length, GFP_KERNEL);
278         if (!crsbuf.pointer) {
279                 err ("acpi_shpchprm: alloc %ld bytes for %s _CRS fail\n", (ulong)crsbuf.length, path_name);
280                 return AE_NO_MEMORY;
281         }
282
283         status = acpi_get_current_resources (handle, &crsbuf);
284         if (ACPI_FAILURE(status)) {
285                 err("acpi_shpchprm: %s _CRS fail=0x%x.\n", path_name, status);
286                 kfree(crsbuf.pointer);
287                 return status;
288         }
289
290         *retbuf = crsbuf.pointer;
291
292         return status;
293 }
294
295 static void free_pci_resource ( struct pci_resource     *aprh)
296 {
297         struct pci_resource     *res, *next;
298
299         for (res = aprh; res; res = next) {
300                 next = res->next;
301                 kfree(res);
302         }
303 }
304
305 static void print_pci_resource ( struct pci_resource    *aprh)
306 {
307         struct pci_resource     *res;
308
309         for (res = aprh; res; res = res->next)
310                 dbg("        base= 0x%x length= 0x%x\n", res->base, res->length);
311 }
312
313 static void print_slot_resources( struct acpi_php_slot  *aps)
314 {
315         if (aps->bus_head) {
316                 dbg("    BUS Resources:\n");
317                 print_pci_resource (aps->bus_head);
318         }
319
320         if (aps->io_head) {
321                 dbg("    IO Resources:\n");
322                 print_pci_resource (aps->io_head);
323         }
324
325         if (aps->mem_head) {
326                 dbg("    MEM Resources:\n");
327                 print_pci_resource (aps->mem_head);
328         }
329
330         if (aps->p_mem_head) {
331                 dbg("    PMEM Resources:\n");
332                 print_pci_resource (aps->p_mem_head);
333         }
334 }
335
336 static void print_pci_resources( struct acpi_bridge     *ab)
337 {
338         if (ab->tbus_head) {
339                 dbg("    Total BUS Resources:\n");
340                 print_pci_resource (ab->tbus_head);
341         }
342         if (ab->bus_head) {
343                 dbg("    BUS Resources:\n");
344                 print_pci_resource (ab->bus_head);
345         }
346
347         if (ab->tio_head) {
348                 dbg("    Total IO Resources:\n");
349                 print_pci_resource (ab->tio_head);
350         }
351         if (ab->io_head) {
352                 dbg("    IO Resources:\n");
353                 print_pci_resource (ab->io_head);
354         }
355
356         if (ab->tmem_head) {
357                 dbg("    Total MEM Resources:\n");
358                 print_pci_resource (ab->tmem_head);
359         }
360         if (ab->mem_head) {
361                 dbg("    MEM Resources:\n");
362                 print_pci_resource (ab->mem_head);
363         }
364
365         if (ab->tp_mem_head) {
366                 dbg("    Total PMEM Resources:\n");
367                 print_pci_resource (ab->tp_mem_head);
368         }
369         if (ab->p_mem_head) {
370                 dbg("    PMEM Resources:\n");
371                 print_pci_resource (ab->p_mem_head);
372         }
373         if (ab->_hpp) {
374                 dbg("    _HPP: cache_line_size=0x%x\n", ab->_hpp->cache_line_size);
375                 dbg("    _HPP: latency timer  =0x%x\n", ab->_hpp->latency_timer);
376                 dbg("    _HPP: enable SERR    =0x%x\n", ab->_hpp->enable_serr);
377                 dbg("    _HPP: enable PERR    =0x%x\n", ab->_hpp->enable_perr);
378         }
379 }
380
381 static int shpchprm_delete_resource(
382         struct pci_resource **aprh,
383         ulong base,
384         ulong size)
385 {
386         struct pci_resource *res;
387         struct pci_resource *prevnode;
388         struct pci_resource *split_node;
389         ulong tbase;
390
391         shpchp_resource_sort_and_combine(aprh);
392
393         for (res = *aprh; res; res = res->next) {
394                 if (res->base > base)
395                         continue;
396
397                 if ((res->base + res->length) < (base + size))
398                         continue;
399
400                 if (res->base < base) {
401                         tbase = base;
402
403                         if ((res->length - (tbase - res->base)) < size)
404                                 continue;
405
406                         split_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
407                         if (!split_node)
408                                 return -ENOMEM;
409
410                         split_node->base = res->base;
411                         split_node->length = tbase - res->base;
412                         res->base = tbase;
413                         res->length -= split_node->length;
414
415                         split_node->next = res->next;
416                         res->next = split_node;
417                 }
418
419                 if (res->length >= size) {
420                         split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
421                         if (!split_node)
422                                 return -ENOMEM;
423
424                         split_node->base = res->base + size;
425                         split_node->length = res->length - size;
426                         res->length = size;
427
428                         split_node->next = res->next;
429                         res->next = split_node;
430                 }
431
432                 if (*aprh == res) {
433                         *aprh = res->next;
434                 } else {
435                         prevnode = *aprh;
436                         while (prevnode->next != res)
437                                 prevnode = prevnode->next;
438
439                         prevnode->next = res->next;
440                 }
441                 res->next = NULL;
442                 kfree(res);
443                 break;
444         }
445
446         return 0;
447 }
448
449 static int shpchprm_delete_resources(
450         struct pci_resource **aprh,
451         struct pci_resource *this
452         )
453 {
454         struct pci_resource *res;
455
456         for (res = this; res; res = res->next)
457                 shpchprm_delete_resource(aprh, res->base, res->length);
458
459         return 0;
460 }
461
462 static int shpchprm_add_resource(
463         struct pci_resource **aprh,
464         ulong base,
465         ulong size)
466 {
467         struct pci_resource *res;
468
469         for (res = *aprh; res; res = res->next) {
470                 if ((res->base + res->length) == base) {
471                         res->length += size;
472                         size = 0L;
473                         break;
474                 }
475                 if (res->next == *aprh)
476                         break;
477         }
478
479         if (size) {
480                 res = kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
481                 if (!res) {
482                         err ("acpi_shpchprm: alloc for res fail\n");
483                         return -ENOMEM;
484                 }
485                 memset(res, 0, sizeof (struct pci_resource));
486
487                 res->base = base;
488                 res->length = size;
489                 res->next = *aprh;
490                 *aprh = res;
491         }
492
493         return 0;
494 }
495
496 static int shpchprm_add_resources(
497         struct pci_resource **aprh,
498         struct pci_resource *this
499         )
500 {
501         struct pci_resource *res;
502         int     rc = 0;
503
504         for (res = this; res && !rc; res = res->next)
505                 rc = shpchprm_add_resource(aprh, res->base, res->length);
506
507         return rc;
508 }
509
510 static void acpi_parse_io (
511         struct acpi_bridge              *ab,
512         union acpi_resource_data        *data
513         )
514 {
515         struct acpi_resource_io *dataio;
516         dataio = (struct acpi_resource_io *) data;
517
518         dbg("Io Resource\n");
519         dbg("  %d bit decode\n", ACPI_DECODE_16 == dataio->io_decode ? 16:10);
520         dbg("  Range minimum base: %08X\n", dataio->min_base_address);
521         dbg("  Range maximum base: %08X\n", dataio->max_base_address);
522         dbg("  Alignment: %08X\n", dataio->alignment);
523         dbg("  Range Length: %08X\n", dataio->range_length);
524 }
525
526 static void acpi_parse_fixed_io (
527         struct acpi_bridge              *ab,
528         union acpi_resource_data        *data
529         )
530 {
531         struct acpi_resource_fixed_io  *datafio;
532         datafio = (struct acpi_resource_fixed_io *) data;
533
534         dbg("Fixed Io Resource\n");
535         dbg("  Range base address: %08X", datafio->base_address);
536         dbg("  Range length: %08X", datafio->range_length);
537 }
538
539 static void acpi_parse_address16_32 (
540         struct acpi_bridge              *ab,
541         union acpi_resource_data        *data,
542         acpi_resource_type              id
543         )
544 {
545         /* 
546          * acpi_resource_address16 == acpi_resource_address32
547          * acpi_resource_address16 *data16 = (acpi_resource_address16 *) data;
548          */
549         struct acpi_resource_address32 *data32 = (struct acpi_resource_address32 *) data;
550         struct pci_resource **aprh, **tprh;
551
552         if (id == ACPI_RSTYPE_ADDRESS16)
553                 dbg("acpi_shpchprm:16-Bit Address Space Resource\n");
554         else
555                 dbg("acpi_shpchprm:32-Bit Address Space Resource\n");
556
557         switch (data32->resource_type) {
558         case ACPI_MEMORY_RANGE: 
559                 dbg("  Resource Type: Memory Range\n");
560                 aprh = &ab->mem_head;
561                 tprh = &ab->tmem_head;
562
563                 switch (data32->attribute.memory.cache_attribute) {
564                 case ACPI_NON_CACHEABLE_MEMORY:
565                         dbg("  Type Specific: Noncacheable memory\n");
566                         break; 
567                 case ACPI_CACHABLE_MEMORY:
568                         dbg("  Type Specific: Cacheable memory\n");
569                         break; 
570                 case ACPI_WRITE_COMBINING_MEMORY:
571                         dbg("  Type Specific: Write-combining memory\n");
572                         break; 
573                 case ACPI_PREFETCHABLE_MEMORY:
574                         aprh = &ab->p_mem_head;
575                         dbg("  Type Specific: Prefetchable memory\n");
576                         break; 
577                 default:
578                         dbg("  Type Specific: Invalid cache attribute\n");
579                         break;
580                 }
581
582                 dbg("  Type Specific: Read%s\n", ACPI_READ_WRITE_MEMORY == data32->attribute.memory.read_write_attribute ? "/Write":" Only");
583                 break;
584
585         case ACPI_IO_RANGE: 
586                 dbg("  Resource Type: I/O Range\n");
587                 aprh = &ab->io_head;
588                 tprh = &ab->tio_head;
589
590                 switch (data32->attribute.io.range_attribute) {
591                 case ACPI_NON_ISA_ONLY_RANGES:
592                         dbg("  Type Specific: Non-ISA Io Addresses\n");
593                         break; 
594                 case ACPI_ISA_ONLY_RANGES:
595                         dbg("  Type Specific: ISA Io Addresses\n");
596                         break; 
597                 case ACPI_ENTIRE_RANGE:
598                         dbg("  Type Specific: ISA and non-ISA Io Addresses\n");
599                         break; 
600                 default:
601                         dbg("  Type Specific: Invalid range attribute\n");
602                         break;
603                 }
604                 break;
605
606         case ACPI_BUS_NUMBER_RANGE: 
607                 dbg("  Resource Type: Bus Number Range(fixed)\n");
608                 /* fixup to be compatible with the rest of php driver */
609                 data32->min_address_range++;
610                 data32->address_length--;
611                 aprh = &ab->bus_head;
612                 tprh = &ab->tbus_head;
613                 break; 
614         default: 
615                 dbg("  Resource Type: Invalid resource type. Exiting.\n");
616                 return;
617         }
618
619         dbg("  Resource %s\n", ACPI_CONSUMER == data32->producer_consumer ? "Consumer":"Producer");
620         dbg("  %s decode\n", ACPI_SUB_DECODE == data32->decode ? "Subtractive":"Positive");
621         dbg("  Min address is %s fixed\n", ACPI_ADDRESS_FIXED == data32->min_address_fixed ? "":"not");
622         dbg("  Max address is %s fixed\n", ACPI_ADDRESS_FIXED == data32->max_address_fixed ? "":"not");
623         dbg("  Granularity: %08X\n", data32->granularity);
624         dbg("  Address range min: %08X\n", data32->min_address_range);
625         dbg("  Address range max: %08X\n", data32->max_address_range);
626         dbg("  Address translation offset: %08X\n", data32->address_translation_offset);
627         dbg("  Address Length: %08X\n", data32->address_length);
628
629         if (0xFF != data32->resource_source.index) {
630                 dbg("  Resource Source Index: %X\n", data32->resource_source.index);
631                 /* dbg("  Resource Source: %s\n", data32->resource_source.string_ptr); */
632         }
633
634         shpchprm_add_resource(aprh, data32->min_address_range, data32->address_length);
635 }
636
637 static acpi_status acpi_parse_crs(
638         struct acpi_bridge      *ab,
639         struct acpi_resource    *crsbuf
640         )
641 {
642         acpi_status             status = AE_OK;
643         struct acpi_resource    *resource = crsbuf;
644         u8                      count = 0;
645         u8                      done = 0;
646
647         while (!done) {
648                 dbg("acpi_shpchprm: PCI bus 0x%x Resource structure %x.\n", ab->bus, count++);
649                 switch (resource->id) {
650                 case ACPI_RSTYPE_IRQ:
651                         dbg("Irq -------- Resource\n");
652                         break; 
653                 case ACPI_RSTYPE_DMA:
654                         dbg("DMA -------- Resource\n");
655                         break; 
656                 case ACPI_RSTYPE_START_DPF:
657                         dbg("Start DPF -------- Resource\n");
658                         break; 
659                 case ACPI_RSTYPE_END_DPF:
660                         dbg("End DPF -------- Resource\n");
661                         break; 
662                 case ACPI_RSTYPE_IO:
663                         acpi_parse_io (ab, &resource->data);
664                         break; 
665                 case ACPI_RSTYPE_FIXED_IO:
666                         acpi_parse_fixed_io (ab, &resource->data);
667                         break; 
668                 case ACPI_RSTYPE_VENDOR:
669                         dbg("Vendor -------- Resource\n");
670                         break; 
671                 case ACPI_RSTYPE_END_TAG:
672                         dbg("End_tag -------- Resource\n");
673                         done = 1;
674                         break; 
675                 case ACPI_RSTYPE_MEM24:
676                         dbg("Mem24 -------- Resource\n");
677                         break; 
678                 case ACPI_RSTYPE_MEM32:
679                         dbg("Mem32 -------- Resource\n");
680                         break; 
681                 case ACPI_RSTYPE_FIXED_MEM32:
682                         dbg("Fixed Mem32 -------- Resource\n");
683                         break; 
684                 case ACPI_RSTYPE_ADDRESS16:
685                         acpi_parse_address16_32(ab, &resource->data, ACPI_RSTYPE_ADDRESS16);
686                         break; 
687                 case ACPI_RSTYPE_ADDRESS32:
688                         acpi_parse_address16_32(ab, &resource->data, ACPI_RSTYPE_ADDRESS32);
689                         break; 
690                 case ACPI_RSTYPE_ADDRESS64:
691                         info("Address64 -------- Resource unparsed\n");
692                         break; 
693                 case ACPI_RSTYPE_EXT_IRQ:
694                         dbg("Ext Irq -------- Resource\n");
695                         break; 
696                 default:
697                         dbg("Invalid -------- resource type 0x%x\n", resource->id);
698                         break;
699                 }
700
701                 resource = (struct acpi_resource *) ((char *)resource + resource->length);
702         }
703
704         return status;
705 }
706
707 static acpi_status acpi_get_crs( struct acpi_bridge     *ab)
708 {
709         acpi_status             status;
710         struct acpi_resource    *crsbuf;
711
712         status = acpi_evaluate_crs(ab->handle, &crsbuf);
713         if (ACPI_SUCCESS(status)) {
714                 status = acpi_parse_crs(ab, crsbuf);
715                 kfree(crsbuf);
716
717                 shpchp_resource_sort_and_combine(&ab->bus_head);
718                 shpchp_resource_sort_and_combine(&ab->io_head);
719                 shpchp_resource_sort_and_combine(&ab->mem_head);
720                 shpchp_resource_sort_and_combine(&ab->p_mem_head);
721
722                 shpchprm_add_resources (&ab->tbus_head, ab->bus_head);
723                 shpchprm_add_resources (&ab->tio_head, ab->io_head);
724                 shpchprm_add_resources (&ab->tmem_head, ab->mem_head);
725                 shpchprm_add_resources (&ab->tp_mem_head, ab->p_mem_head);
726         }
727
728         return status;
729 }
730
731 /* find acpi_bridge downword from ab.  */
732 static struct acpi_bridge *
733 find_acpi_bridge_by_bus(
734         struct acpi_bridge *ab,
735         int seg,
736         int bus         /* pdev->subordinate->number */
737         )
738 {
739         struct acpi_bridge      *lab = NULL;
740
741         if (!ab)
742                 return NULL;
743
744         if ((ab->bus == bus) && (ab->seg == seg))
745                 return ab;
746
747         if (ab->child)
748                 lab = find_acpi_bridge_by_bus(ab->child, seg, bus);
749
750         if (!lab)
751         if (ab->next)
752                 lab = find_acpi_bridge_by_bus(ab->next, seg, bus);
753
754         return lab;
755 }
756
757 /*
758  * Build a device tree of ACPI PCI Bridges
759  */
760 static void shpchprm_acpi_register_a_bridge (
761         struct acpi_bridge      **head,
762         struct acpi_bridge      *pab,   /* parent bridge to which child bridge is added */
763         struct acpi_bridge      *cab    /* child bridge to add */
764         )
765 {
766         struct acpi_bridge      *lpab;
767         struct acpi_bridge      *lcab;
768
769         lpab = find_acpi_bridge_by_bus(*head, pab->seg, pab->bus);
770         if (!lpab) {
771                 if (!(pab->type & BRIDGE_TYPE_HOST))
772                         warn("PCI parent bridge s:b(%x:%x) not in list.\n", pab->seg, pab->bus);
773                 pab->next = *head;
774                 *head = pab;
775                 lpab = pab;
776         }
777
778         if ((cab->type & BRIDGE_TYPE_HOST) && (pab == cab))
779                 return;
780
781         lcab = find_acpi_bridge_by_bus(*head, cab->seg, cab->bus);
782         if (lcab) {
783                 if ((pab->bus != lcab->parent->bus) || (lcab->bus != cab->bus))
784                         err("PCI child bridge s:b(%x:%x) in list with diff parent.\n", cab->seg, cab->bus);
785                 return;
786         } else
787                 lcab = cab;
788
789         lcab->parent = lpab;
790         lcab->next = lpab->child;
791         lpab->child = lcab;
792 }
793
794 static acpi_status shpchprm_acpi_build_php_slots_callback(
795         acpi_handle     handle,
796         u32             Level,
797         void            *context,
798         void            **retval
799         )
800 {
801         ulong           bus_num;
802         ulong           seg_num;
803         ulong           sun, adr;
804         ulong           padr = 0;
805         acpi_handle             phandle = NULL;
806         struct acpi_bridge      *pab = (struct acpi_bridge *)context;
807         struct acpi_bridge      *lab;
808         acpi_status             status;
809         u8                      *path_name = acpi_path_name(handle);
810
811         /* get _SUN */
812         status = acpi_evaluate_integer(handle, METHOD_NAME__SUN, NULL, &sun);
813         switch(status) {
814         case AE_NOT_FOUND:
815                 return AE_OK;
816         default:
817                 if (ACPI_FAILURE(status)) {
818                         err("acpi_shpchprm:%s _SUN fail=0x%x\n", path_name, status);
819                         return status;
820                 }
821         }
822
823         /* get _ADR. _ADR must exist if _SUN exists */
824         status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &adr);
825         if (ACPI_FAILURE(status)) {
826                 err("acpi_shpchprm:%s _ADR fail=0x%x\n", path_name, status);
827                 return status;
828         }
829
830         dbg("acpi_shpchprm:%s sun=0x%08x adr=0x%08x\n", path_name, (u32)sun, (u32)adr);
831
832         status = acpi_get_parent(handle, &phandle);
833         if (ACPI_FAILURE(status)) {
834                 err("acpi_shpchprm:%s get_parent fail=0x%x\n", path_name, status);
835                 return (status);
836         }
837
838         bus_num = pab->bus;
839         seg_num = pab->seg;
840
841         if (pab->bus == bus_num) {
842                 lab = pab;
843         } else {
844                 dbg("WARN: pab is not parent\n");
845                 lab = find_acpi_bridge_by_bus(pab, seg_num, bus_num);
846                 if (!lab) {
847                         dbg("acpi_shpchprm: alloc new P2P bridge(%x) for sun(%08x)\n", (u32)bus_num, (u32)sun);
848                         lab = (struct acpi_bridge *)kmalloc(sizeof(struct acpi_bridge), GFP_KERNEL);
849                         if (!lab) {
850                                 err("acpi_shpchprm: alloc for ab fail\n");
851                                 return AE_NO_MEMORY;
852                         }
853                         memset(lab, 0, sizeof(struct acpi_bridge));
854
855                         lab->handle = phandle;
856                         lab->pbus = pab->bus;
857                         lab->pdevice = (int)(padr >> 16) & 0xffff;
858                         lab->pfunction = (int)(padr & 0xffff);
859                         lab->bus = (int)bus_num;
860                         lab->scanned = 0;
861                         lab->type = BRIDGE_TYPE_P2P;
862
863                         shpchprm_acpi_register_a_bridge (&acpi_bridges_head, pab, lab);
864                 } else
865                         dbg("acpi_shpchprm: found P2P bridge(%x) for sun(%08x)\n", (u32)bus_num, (u32)sun);
866         }
867
868         acpi_add_slot_to_php_slots(lab, (int)bus_num, handle, (u32)adr, (u32)sun);
869         return (status);
870 }
871
872 static int shpchprm_acpi_build_php_slots(
873         struct acpi_bridge      *ab,
874         u32                     depth
875         )
876 {
877         acpi_status     status;
878         u8              *path_name = acpi_path_name(ab->handle);
879
880         /* Walk down this pci bridge to get _SUNs if any behind P2P */
881         status = acpi_walk_namespace ( ACPI_TYPE_DEVICE,
882                                 ab->handle,
883                                 depth,
884                                 shpchprm_acpi_build_php_slots_callback,
885                                 ab,
886                                 NULL );
887         if (ACPI_FAILURE(status)) {
888                 dbg("acpi_shpchprm:%s walk for _SUN on pci bridge seg:bus(%x:%x) fail=0x%x\n", path_name, ab->seg, ab->bus, status);
889                 return -1;
890         }
891
892         return 0;
893 }
894
895 static void build_a_bridge(
896         struct acpi_bridge      *pab,
897         struct acpi_bridge      *ab
898         )
899 {
900         u8              *path_name = acpi_path_name(ab->handle);
901
902         shpchprm_acpi_register_a_bridge (&acpi_bridges_head, pab, ab);
903
904         switch (ab->type) {
905         case BRIDGE_TYPE_HOST:
906                 dbg("acpi_shpchprm: Registered PCI HOST Bridge(%02x)    on s:b:d:f(%02x:%02x:%02x:%02x) [%s]\n",
907                         ab->bus, ab->seg, ab->pbus, ab->pdevice, ab->pfunction, path_name);
908                 break;
909         case BRIDGE_TYPE_P2P:
910                 dbg("acpi_shpchprm: Registered PCI  P2P Bridge(%02x-%02x) on s:b:d:f(%02x:%02x:%02x:%02x) [%s]\n",
911                         ab->pbus, ab->bus, ab->seg, ab->pbus, ab->pdevice, ab->pfunction, path_name);
912                 break;
913         };
914
915         /* build any immediate PHP slots under this pci bridge */
916         shpchprm_acpi_build_php_slots(ab, 1);
917 }
918
919 static struct acpi_bridge * add_p2p_bridge(
920         acpi_handle handle,
921         struct acpi_bridge      *pab,   /* parent */
922         ulong   adr
923         )
924 {
925         struct acpi_bridge      *ab;
926         struct pci_dev  *pdev;
927         ulong           devnum, funcnum;
928         u8                      *path_name = acpi_path_name(handle);
929
930         ab = (struct acpi_bridge *) kmalloc (sizeof(struct acpi_bridge), GFP_KERNEL);
931         if (!ab) {
932                 err("acpi_shpchprm: alloc for ab fail\n");
933                 return NULL;
934         }
935         memset(ab, 0, sizeof(struct acpi_bridge));
936
937         devnum = (adr >> 16) & 0xffff;
938         funcnum = adr & 0xffff;
939
940         pdev = pci_find_slot(pab->bus, PCI_DEVFN(devnum, funcnum));
941         if (!pdev || !pdev->subordinate) {
942                 err("acpi_shpchprm:%s is not a P2P Bridge\n", path_name);
943                 kfree(ab);
944                 return NULL;
945         }
946
947         ab->handle = handle;
948         ab->seg = pab->seg;
949         ab->pbus = pab->bus;            /* or pdev->bus->number */
950         ab->pdevice = devnum;           /* or PCI_SLOT(pdev->devfn) */
951         ab->pfunction = funcnum;        /* or PCI_FUNC(pdev->devfn) */
952         ab->bus = pdev->subordinate->number;
953         ab->scanned = 0;
954         ab->type = BRIDGE_TYPE_P2P;
955
956         dbg("acpi_shpchprm: P2P(%x-%x) on pci=b:d:f(%x:%x:%x) acpi=b:d:f(%x:%x:%x) [%s]\n",
957                 pab->bus, ab->bus, pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
958                 pab->bus, (u32)devnum, (u32)funcnum, path_name);
959
960         build_a_bridge(pab, ab);
961
962         return ab;
963 }
964
965 static acpi_status scan_p2p_bridge(
966         acpi_handle             handle,
967         u32                     Level,
968         void                    *context,
969         void                    **retval
970         )
971 {
972         struct acpi_bridge      *pab = (struct acpi_bridge *)context;
973         struct acpi_bridge      *ab;
974         acpi_status             status;
975         ulong           adr = 0;
976         u8                      *path_name = acpi_path_name(handle);
977         ulong           devnum, funcnum;
978         struct pci_dev  *pdev;
979
980         /* get device, function */
981         status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &adr);
982         if (ACPI_FAILURE(status)) {
983                 if (status != AE_NOT_FOUND)
984                         err("acpi_shpchprm:%s _ADR fail=0x%x\n", path_name, status);
985                 return AE_OK;
986         }
987
988         devnum = (adr >> 16) & 0xffff;
989         funcnum = adr & 0xffff;
990
991         pdev = pci_find_slot(pab->bus, PCI_DEVFN(devnum, funcnum));
992         if (!pdev)
993                 return AE_OK;
994         if (!pdev->subordinate)
995                 return AE_OK;
996
997         ab = add_p2p_bridge(handle, pab, adr);
998         if (ab) {
999                 status = acpi_walk_namespace ( ACPI_TYPE_DEVICE,
1000                                         handle,
1001                                         (u32)1,
1002                                         scan_p2p_bridge,
1003                                         ab,
1004                                         NULL);
1005                 if (ACPI_FAILURE(status))
1006                         dbg("acpi_shpchprm:%s find_p2p fail=0x%x\n", path_name, status);
1007         }
1008
1009         return AE_OK;
1010 }
1011
1012 static struct acpi_bridge * add_host_bridge(
1013         acpi_handle handle,
1014         ulong   segnum,
1015         ulong   busnum
1016         )
1017 {
1018         ulong                   adr = 0;
1019         acpi_status             status;
1020         struct acpi_bridge      *ab;
1021         u8                      *path_name = acpi_path_name(handle);
1022
1023         /* get device, function: host br adr is always 0000 though.  */
1024         status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &adr);
1025         if (ACPI_FAILURE(status)) {
1026                 err("acpi_shpchprm:%s _ADR fail=0x%x\n", path_name, status);
1027                 return NULL;
1028         }
1029         dbg("acpi_shpchprm: ROOT PCI seg(0x%x)bus(0x%x)dev(0x%x)func(0x%x) [%s]\n", (u32)segnum, (u32)busnum, 
1030                 (u32)(adr >> 16) & 0xffff, (u32)adr & 0xffff, path_name);
1031
1032         ab = (struct acpi_bridge *) kmalloc (sizeof(struct acpi_bridge), GFP_KERNEL);
1033         if (!ab) {
1034                 err("acpi_shpchprm: alloc for ab fail\n");
1035                 return NULL;
1036         }
1037         memset(ab, 0, sizeof(struct acpi_bridge));
1038
1039         ab->handle = handle;
1040         ab->seg = (int)segnum;
1041         ab->bus = ab->pbus = (int)busnum;
1042         ab->pdevice = (int)(adr >> 16) & 0xffff;
1043         ab->pfunction = (int)(adr & 0xffff);
1044         ab->scanned = 0;
1045         ab->type = BRIDGE_TYPE_HOST;
1046
1047         /* get root pci bridge's current resources */
1048         status = acpi_get_crs(ab);
1049         if (ACPI_FAILURE(status)) {
1050                 err("acpi_shpchprm:%s evaluate _CRS fail=0x%x\n", path_name, status);
1051                 kfree(ab);
1052                 return NULL;
1053         }
1054         build_a_bridge(ab, ab);
1055
1056         return ab;
1057 }
1058
1059 static acpi_status acpi_scan_from_root_pci_callback (
1060         acpi_handle     handle,
1061         u32                     Level,
1062         void            *context,
1063         void            **retval
1064         )
1065 {
1066         ulong           segnum = 0;
1067         ulong           busnum = 0;
1068         acpi_status             status;
1069         struct acpi_bridge      *ab;
1070         u8                      *path_name = acpi_path_name(handle);
1071
1072         /* get bus number of this pci root bridge */
1073         status = acpi_evaluate_integer(handle, METHOD_NAME__SEG, NULL, &segnum);
1074         if (ACPI_FAILURE(status)) {
1075                 if (status != AE_NOT_FOUND) {
1076                         err("acpi_shpchprm:%s evaluate _SEG fail=0x%x\n", path_name, status);
1077                         return status;
1078                 }
1079                 segnum = 0;
1080         }
1081
1082         /* get bus number of this pci root bridge */
1083         status = acpi_evaluate_integer(handle, METHOD_NAME__BBN, NULL, &busnum);
1084         if (ACPI_FAILURE(status)) {
1085                 err("acpi_shpchprm:%s evaluate _BBN fail=0x%x\n", path_name, status);
1086                 return (status);
1087         }
1088
1089         ab = add_host_bridge(handle, segnum, busnum);
1090         if (ab) {
1091                 status = acpi_walk_namespace ( ACPI_TYPE_DEVICE,
1092                                         handle,
1093                                         1,
1094                                         scan_p2p_bridge,
1095                                         ab,
1096                                         NULL);
1097                 if (ACPI_FAILURE(status))
1098                         dbg("acpi_shpchprm:%s find_p2p fail=0x%x\n", path_name, status);
1099         }
1100
1101         return AE_OK;
1102 }
1103
1104 static int shpchprm_acpi_scan_pci (void)
1105 {
1106         acpi_status     status;
1107
1108         /*
1109          * TBD: traverse LDM device tree with the help of
1110          *  unified ACPI augmented for php device population.
1111          */
1112         status = acpi_get_devices ( PCI_ROOT_HID_STRING,
1113                                 acpi_scan_from_root_pci_callback,
1114                                 NULL,
1115                                 NULL );
1116         if (ACPI_FAILURE(status)) {
1117                 err("acpi_shpchprm:get_device PCI ROOT HID fail=0x%x\n", status);
1118                 return -1;
1119         }
1120
1121         return 0;
1122 }
1123
1124 int shpchprm_init(enum php_ctlr_type ctlr_type)
1125 {
1126         int     rc;
1127
1128         if (ctlr_type != PCI)
1129                 return -ENODEV;
1130
1131         dbg("shpchprm ACPI init <enter>\n");
1132         acpi_bridges_head = NULL;
1133
1134         /* construct PCI bus:device tree of acpi_handles */
1135         rc = shpchprm_acpi_scan_pci();
1136         if (rc)
1137                 return rc;
1138
1139         dbg("shpchprm ACPI init %s\n", (rc)?"fail":"success");
1140         return rc;
1141 }
1142
1143 static void free_a_slot(struct acpi_php_slot *aps)
1144 {
1145         dbg("        free a php func of slot(0x%02x) on PCI b:d:f=0x%02x:%02x:%02x\n", aps->sun, aps->bus, aps->dev, aps->fun);
1146
1147         free_pci_resource (aps->io_head);
1148         free_pci_resource (aps->bus_head);
1149         free_pci_resource (aps->mem_head);
1150         free_pci_resource (aps->p_mem_head);
1151
1152         kfree(aps);
1153 }
1154
1155 static void free_a_bridge( struct acpi_bridge   *ab)
1156 {
1157         struct acpi_php_slot    *aps, *next;
1158
1159         switch (ab->type) {
1160         case BRIDGE_TYPE_HOST:
1161                 dbg("Free ACPI PCI HOST Bridge(%x) [%s] on s:b:d:f(%x:%x:%x:%x)\n",
1162                         ab->bus, acpi_path_name(ab->handle), ab->seg, ab->pbus, ab->pdevice, ab->pfunction);
1163                 break;
1164         case BRIDGE_TYPE_P2P:
1165                 dbg("Free ACPI PCI P2P Bridge(%x-%x) [%s] on s:b:d:f(%x:%x:%x:%x)\n",
1166                         ab->pbus, ab->bus, acpi_path_name(ab->handle), ab->seg, ab->pbus, ab->pdevice, ab->pfunction);
1167                 break;
1168         };
1169
1170         /* free slots first */
1171         for (aps = ab->slots; aps; aps = next) {
1172                 next = aps->next;
1173                 free_a_slot(aps);
1174         }
1175
1176         free_pci_resource (ab->io_head);
1177         free_pci_resource (ab->tio_head);
1178         free_pci_resource (ab->bus_head);
1179         free_pci_resource (ab->tbus_head);
1180         free_pci_resource (ab->mem_head);
1181         free_pci_resource (ab->tmem_head);
1182         free_pci_resource (ab->p_mem_head);
1183         free_pci_resource (ab->tp_mem_head);
1184
1185         kfree(ab);
1186 }
1187
1188 static void shpchprm_free_bridges ( struct acpi_bridge  *ab)
1189 {
1190         if (!ab)
1191                 return;
1192
1193         if (ab->child)
1194                 shpchprm_free_bridges (ab->child);
1195
1196         if (ab->next)
1197                 shpchprm_free_bridges (ab->next);
1198
1199         free_a_bridge(ab);
1200 }
1201
1202 void shpchprm_cleanup(void)
1203 {
1204         shpchprm_free_bridges (acpi_bridges_head);
1205 }
1206
1207 static int get_number_of_slots (
1208         struct acpi_bridge      *ab,
1209         int                             selfonly
1210         )
1211 {
1212         struct acpi_php_slot    *aps;
1213         int     prev_slot = -1;
1214         int     slot_num = 0;
1215
1216         for ( aps = ab->slots; aps; aps = aps->next)
1217                 if (aps->dev != prev_slot) {
1218                         prev_slot = aps->dev;
1219                         slot_num++;
1220                 }
1221
1222         if (ab->child)
1223                 slot_num += get_number_of_slots (ab->child, 0);
1224
1225         if (selfonly)
1226                 return slot_num;
1227
1228         if (ab->next)
1229                 slot_num += get_number_of_slots (ab->next, 0);
1230
1231         return slot_num;
1232 }
1233
1234 static int print_acpi_resources (struct acpi_bridge     *ab)
1235 {
1236         struct acpi_php_slot    *aps;
1237         int     i;
1238
1239         switch (ab->type) {
1240         case BRIDGE_TYPE_HOST:
1241                 dbg("PCI HOST Bridge (%x) [%s]\n", ab->bus, acpi_path_name(ab->handle));
1242                 break;
1243         case BRIDGE_TYPE_P2P:
1244                 dbg("PCI P2P Bridge (%x-%x) [%s]\n", ab->pbus, ab->bus, acpi_path_name(ab->handle));
1245                 break;
1246         };
1247
1248         print_pci_resources (ab);
1249
1250         for ( i = -1, aps = ab->slots; aps; aps = aps->next) {
1251                 if (aps->dev == i)
1252                         continue;
1253                 dbg("  Slot sun(%x) s:b:d:f(%02x:%02x:%02x:%02x)\n", aps->sun, aps->seg, aps->bus, aps->dev, aps->fun);
1254                 print_slot_resources(aps);
1255                 i = aps->dev;
1256         }
1257
1258         if (ab->child)
1259                 print_acpi_resources (ab->child);
1260
1261         if (ab->next)
1262                 print_acpi_resources (ab->next);
1263
1264         return 0;
1265 }
1266
1267 int shpchprm_print_pirt(void)
1268 {
1269         dbg("SHPCHPRM ACPI Slots\n");
1270         if (acpi_bridges_head)
1271                 print_acpi_resources (acpi_bridges_head);
1272         return 0;
1273 }
1274
1275 static struct acpi_php_slot * get_acpi_slot (
1276         struct acpi_bridge *ab,
1277         u32 sun
1278         )
1279 {
1280         struct acpi_php_slot    *aps = NULL;
1281
1282         for ( aps = ab->slots; aps; aps = aps->next)
1283                 if (aps->sun == sun)
1284                         return aps;
1285
1286         if (!aps && ab->child) {
1287                 aps = (struct acpi_php_slot *)get_acpi_slot (ab->child, sun);
1288                 if (aps)
1289                         return aps;
1290         }
1291
1292         if (!aps && ab->next) {
1293                 aps = (struct acpi_php_slot *)get_acpi_slot (ab->next, sun);
1294                 if (aps)
1295                         return aps;
1296         }
1297
1298         return aps;
1299
1300 }
1301
1302 #if 0
1303 static void * shpchprm_get_slot(struct slot *slot)
1304 {
1305         struct acpi_bridge      *ab = acpi_bridges_head;
1306         struct acpi_php_slot    *aps = get_acpi_slot (ab, slot->number);
1307
1308         aps->slot = slot;
1309
1310         dbg("Got acpi slot sun(%x): s:b:d:f(%x:%x:%x:%x)\n", aps->sun, aps->seg, aps->bus, aps->dev, aps->fun);
1311
1312         return (void *)aps;
1313 }
1314 #endif
1315
1316 static void shpchprm_dump_func_res( struct pci_func *fun)
1317 {
1318         struct pci_func *func = fun;
1319
1320         if (func->bus_head) {
1321                 dbg(":    BUS Resources:\n");
1322                 print_pci_resource (func->bus_head);
1323         }
1324         if (func->io_head) {
1325                 dbg(":    IO Resources:\n");
1326                 print_pci_resource (func->io_head);
1327         }
1328         if (func->mem_head) {
1329                 dbg(":    MEM Resources:\n");
1330                 print_pci_resource (func->mem_head);
1331         }
1332         if (func->p_mem_head) {
1333                 dbg(":    PMEM Resources:\n");
1334                 print_pci_resource (func->p_mem_head);
1335         }
1336 }
1337
1338 static void shpchprm_dump_ctrl_res( struct controller *ctlr)
1339 {
1340         struct controller *ctrl = ctlr;
1341
1342         if (ctrl->bus_head) {
1343                 dbg(":    BUS Resources:\n");
1344                 print_pci_resource (ctrl->bus_head);
1345         }
1346         if (ctrl->io_head) {
1347                 dbg(":    IO Resources:\n");
1348                 print_pci_resource (ctrl->io_head);
1349         }
1350         if (ctrl->mem_head) {
1351                 dbg(":    MEM Resources:\n");
1352                 print_pci_resource (ctrl->mem_head);
1353         }
1354         if (ctrl->p_mem_head) {
1355                 dbg(":    PMEM Resources:\n");
1356                 print_pci_resource (ctrl->p_mem_head);
1357         }
1358 }
1359
1360 static int shpchprm_get_used_resources (
1361         struct controller *ctrl,
1362         struct pci_func *func
1363         )
1364 {
1365         return shpchp_save_used_resources (ctrl, func, !DISABLE_CARD);
1366 }
1367
1368 static int configure_existing_function(
1369         struct controller *ctrl,
1370         struct pci_func *func
1371         )
1372 {
1373         int rc;
1374
1375         /* see how much resources the func has used. */
1376         rc = shpchprm_get_used_resources (ctrl, func);
1377
1378         if (!rc) {
1379                 /* subtract the resources used by the func from ctrl resources */
1380                 rc  = shpchprm_delete_resources (&ctrl->bus_head, func->bus_head);
1381                 rc |= shpchprm_delete_resources (&ctrl->io_head, func->io_head);
1382                 rc |= shpchprm_delete_resources (&ctrl->mem_head, func->mem_head);
1383                 rc |= shpchprm_delete_resources (&ctrl->p_mem_head, func->p_mem_head);
1384                 if (rc)
1385                         warn("aCEF: cannot del used resources\n");
1386         } else
1387                 err("aCEF: cannot get used resources\n");
1388
1389         return rc;
1390 }
1391
1392 static int bind_pci_resources_to_slots ( struct controller *ctrl)
1393 {
1394         struct pci_func *func;
1395         int busn = ctrl->bus;
1396         int devn, funn;
1397         u32     vid;
1398
1399         for (devn = 0; devn < 32; devn++) {
1400                 for (funn = 0; funn < 8; funn++) {
1401                         if (devn == ctrl->device && funn == ctrl->function)
1402                                 continue;
1403                         /* find out if this entry is for an occupied slot */
1404                         vid = 0xFFFFFFFF;
1405                         pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(devn, funn), PCI_VENDOR_ID, &vid);
1406
1407                         if (vid != 0xFFFFFFFF) {
1408                                 func = shpchp_slot_find(busn, devn, funn);
1409                                 if (!func)
1410                                         continue;
1411                                 configure_existing_function(ctrl, func);
1412                                 dbg("aCCF:existing PCI 0x%x Func ResourceDump\n", ctrl->bus);
1413                                 shpchprm_dump_func_res(func);
1414                         }
1415                 }
1416         }
1417
1418         return 0;
1419 }
1420
1421 static int bind_pci_resources(
1422         struct controller       *ctrl,
1423         struct acpi_bridge      *ab
1424         )
1425 {
1426         int     status = 0;
1427
1428         if (ab->bus_head) {
1429                 dbg("bapr:  BUS Resources add on PCI 0x%x\n", ab->bus);
1430                 status = shpchprm_add_resources (&ctrl->bus_head, ab->bus_head);
1431                 if (shpchprm_delete_resources (&ab->bus_head, ctrl->bus_head))
1432                         warn("bapr:  cannot sub BUS Resource on PCI 0x%x\n", ab->bus);
1433                 if (status) {
1434                         err("bapr:  BUS Resource add on PCI 0x%x: fail=0x%x\n", ab->bus, status);
1435                         return status;
1436                 }
1437         } else
1438                 info("bapr:  No BUS Resource on PCI 0x%x.\n", ab->bus);
1439
1440         if (ab->io_head) {
1441                 dbg("bapr:  IO Resources add on PCI 0x%x\n", ab->bus);
1442                 status = shpchprm_add_resources (&ctrl->io_head, ab->io_head);
1443                 if (shpchprm_delete_resources (&ab->io_head, ctrl->io_head))
1444                         warn("bapr:  cannot sub IO Resource on PCI 0x%x\n", ab->bus);
1445                 if (status) {
1446                         err("bapr:  IO Resource add on PCI 0x%x: fail=0x%x\n", ab->bus, status);
1447                         return status;
1448                 }
1449         } else
1450                 info("bapr:  No  IO Resource on PCI 0x%x.\n", ab->bus);
1451
1452         if (ab->mem_head) {
1453                 dbg("bapr:  MEM Resources add on PCI 0x%x\n", ab->bus);
1454                 status = shpchprm_add_resources (&ctrl->mem_head, ab->mem_head);
1455                 if (shpchprm_delete_resources (&ab->mem_head, ctrl->mem_head))
1456                         warn("bapr:  cannot sub MEM Resource on PCI 0x%x\n", ab->bus);
1457                 if (status) {
1458                         err("bapr:  MEM Resource add on PCI 0x%x: fail=0x%x\n", ab->bus, status);
1459                         return status;
1460                 }
1461         } else
1462                 info("bapr:  No MEM Resource on PCI 0x%x.\n", ab->bus);
1463
1464         if (ab->p_mem_head) {
1465                 dbg("bapr:  PMEM Resources add on PCI 0x%x\n", ab->bus);
1466                 status = shpchprm_add_resources (&ctrl->p_mem_head, ab->p_mem_head);
1467                 if (shpchprm_delete_resources (&ab->p_mem_head, ctrl->p_mem_head))
1468                         warn("bapr:  cannot sub PMEM Resource on PCI 0x%x\n", ab->bus);
1469                 if (status) {
1470                         err("bapr:  PMEM Resource add on PCI 0x%x: fail=0x%x\n", ab->bus, status);
1471                         return status;
1472                 }
1473         } else
1474                 info("bapr:  No PMEM Resource on PCI 0x%x.\n", ab->bus);
1475
1476         return status;
1477 }
1478
1479 static int no_pci_resources( struct acpi_bridge *ab)
1480 {
1481         return !(ab->p_mem_head || ab->mem_head || ab->io_head || ab->bus_head);
1482 }
1483
1484 static int find_pci_bridge_resources (
1485         struct controller *ctrl,
1486         struct acpi_bridge *ab
1487         )
1488 {
1489         int     rc = 0;
1490         struct pci_func func;
1491
1492         memset(&func, 0, sizeof(struct pci_func));
1493
1494         func.bus = ab->pbus;
1495         func.device = ab->pdevice;
1496         func.function = ab->pfunction;
1497         func.is_a_board = 1;
1498
1499         /* Get used resources for this PCI bridge */
1500         rc = shpchp_save_used_resources (ctrl, &func, !DISABLE_CARD);
1501
1502         ab->io_head = func.io_head;
1503         ab->mem_head = func.mem_head;
1504         ab->p_mem_head = func.p_mem_head;
1505         ab->bus_head = func.bus_head;
1506         if (ab->bus_head)
1507                 shpchprm_delete_resource(&ab->bus_head, ctrl->bus, 1);
1508
1509         return rc;
1510 }
1511
1512 static int get_pci_resources_from_bridge(
1513         struct controller *ctrl,
1514         struct acpi_bridge *ab
1515         )
1516 {
1517         int     rc = 0;
1518
1519         dbg("grfb:  Get Resources for PCI 0x%x from actual PCI bridge 0x%x.\n", ctrl->bus, ab->bus);
1520
1521         rc = find_pci_bridge_resources (ctrl, ab);
1522
1523         shpchp_resource_sort_and_combine(&ab->bus_head);
1524         shpchp_resource_sort_and_combine(&ab->io_head);
1525         shpchp_resource_sort_and_combine(&ab->mem_head);
1526         shpchp_resource_sort_and_combine(&ab->p_mem_head);
1527
1528         shpchprm_add_resources (&ab->tbus_head, ab->bus_head);
1529         shpchprm_add_resources (&ab->tio_head, ab->io_head);
1530         shpchprm_add_resources (&ab->tmem_head, ab->mem_head);
1531         shpchprm_add_resources (&ab->tp_mem_head, ab->p_mem_head);
1532
1533         return rc;
1534 }
1535
1536 static int get_pci_resources(
1537         struct controller       *ctrl,
1538         struct acpi_bridge      *ab
1539         )
1540 {
1541         int     rc = 0;
1542
1543         if (no_pci_resources(ab)) {
1544                 dbg("spbr:PCI 0x%x has no resources. Get parent resources.\n", ab->bus);
1545                 rc = get_pci_resources_from_bridge(ctrl, ab);
1546         }
1547
1548         return rc;
1549 }
1550
1551 int shpchprm_get_physical_slot_number(struct controller *ctrl, u32 *sun, u8 busnum, u8 devnum)
1552 {
1553         int offset = devnum - ctrl->slot_device_offset;
1554
1555         dbg("%s: ctrl->slot_num_inc %d, offset %d\n", __FUNCTION__, ctrl->slot_num_inc, offset);
1556         *sun = (u8) (ctrl->first_slot + ctrl->slot_num_inc *offset);
1557         return 0;
1558 }
1559
1560 /*
1561  * Get resources for this ctrl.
1562  *  1. get total resources from ACPI _CRS or bridge (this ctrl)
1563  *  2. find used resources of existing adapters
1564  *      3. subtract used resources from total resources
1565  */
1566 int shpchprm_find_available_resources( struct controller *ctrl)
1567 {
1568         int rc = 0;
1569         struct acpi_bridge      *ab;
1570
1571         ab = find_acpi_bridge_by_bus(acpi_bridges_head, ctrl->seg, ctrl->pci_dev->subordinate->number);
1572         if (!ab) {
1573                 err("pfar:cannot locate acpi bridge of PCI 0x%x.\n", ctrl->pci_dev->subordinate->number);
1574                 return -1;
1575         }
1576         if (no_pci_resources(ab)) {
1577                 rc = get_pci_resources(ctrl, ab);
1578                 if (rc) {
1579                         err("pfar:cannot get pci resources of PCI 0x%x.\n",ctrl->pci_dev->subordinate->number);
1580                         return -1;
1581                 }
1582         }
1583
1584         rc = bind_pci_resources(ctrl, ab);
1585         dbg("pfar:pre-Bind PCI 0x%x Ctrl Resource Dump\n", ctrl->pci_dev->subordinate->number);
1586         shpchprm_dump_ctrl_res(ctrl);
1587
1588         bind_pci_resources_to_slots (ctrl);
1589
1590         dbg("pfar:post-Bind PCI 0x%x Ctrl Resource Dump\n", ctrl->pci_dev->subordinate->number);
1591         shpchprm_dump_ctrl_res(ctrl);
1592
1593         return rc;
1594 }
1595
1596 int shpchprm_set_hpp(
1597         struct controller *ctrl,
1598         struct pci_func *func,
1599         u8      card_type
1600         )
1601 {
1602         struct acpi_bridge      *ab;
1603         struct pci_bus lpci_bus, *pci_bus;
1604         int                             rc = 0;
1605         unsigned int    devfn;
1606         u8                              cls= 0x08;      /* default cache line size      */
1607         u8                              lt = 0x40;      /* default latency timer        */
1608         u8                              ep = 0;
1609         u8                              es = 0;
1610
1611         memcpy(&lpci_bus, ctrl->pci_bus, sizeof(lpci_bus));
1612         pci_bus = &lpci_bus;
1613         pci_bus->number = func->bus;
1614         devfn = PCI_DEVFN(func->device, func->function);
1615
1616         ab = find_acpi_bridge_by_bus(acpi_bridges_head, ctrl->seg, ctrl->bus);
1617
1618         if (ab) {
1619                 if (ab->_hpp) {
1620                         lt  = (u8)ab->_hpp->latency_timer;
1621                         cls = (u8)ab->_hpp->cache_line_size;
1622                         ep  = (u8)ab->_hpp->enable_perr;
1623                         es  = (u8)ab->_hpp->enable_serr;
1624                 } else
1625                         dbg("_hpp: no _hpp for B/D/F=%#x/%#x/%#x. use default value\n", func->bus, func->device, func->function);
1626         } else
1627                 dbg("_hpp: no acpi bridge for B/D/F = %#x/%#x/%#x. use default value\n", func->bus, func->device, func->function);
1628
1629
1630         if (card_type == PCI_HEADER_TYPE_BRIDGE) {
1631                 /* set subordinate Latency Timer */
1632                 rc |= pci_bus_write_config_byte(pci_bus, devfn, PCI_SEC_LATENCY_TIMER, lt);
1633         }
1634
1635         /* set base Latency Timer */
1636         rc |= pci_bus_write_config_byte(pci_bus, devfn, PCI_LATENCY_TIMER, lt);
1637         dbg("  set latency timer  =0x%02x: %x\n", lt, rc);
1638
1639         rc |= pci_bus_write_config_byte(pci_bus, devfn, PCI_CACHE_LINE_SIZE, cls);
1640         dbg("  set cache_line_size=0x%02x: %x\n", cls, rc);
1641
1642         return rc;
1643 }
1644
1645 void shpchprm_enable_card(
1646         struct controller *ctrl,
1647         struct pci_func *func,
1648         u8 card_type)
1649 {
1650         u16 command, cmd, bcommand, bcmd;
1651         struct pci_bus lpci_bus, *pci_bus;
1652         struct acpi_bridge      *ab;
1653         unsigned int devfn;
1654         int rc;
1655
1656         memcpy(&lpci_bus, ctrl->pci_bus, sizeof(lpci_bus));
1657         pci_bus = &lpci_bus;
1658         pci_bus->number = func->bus;
1659         devfn = PCI_DEVFN(func->device, func->function);
1660
1661         rc = pci_bus_read_config_word(pci_bus, devfn, PCI_COMMAND, &command);
1662
1663         if (card_type == PCI_HEADER_TYPE_BRIDGE) {
1664                 rc = pci_bus_read_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, &bcommand);
1665         }
1666
1667         cmd = command  = command | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE
1668                 | PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
1669         bcmd = bcommand  = bcommand | PCI_BRIDGE_CTL_NO_ISA;
1670
1671         ab = find_acpi_bridge_by_bus(acpi_bridges_head, ctrl->seg, ctrl->bus);
1672         if (ab) {
1673                 if (ab->_hpp) {
1674                         if (ab->_hpp->enable_perr) {
1675                                 command |= PCI_COMMAND_PARITY;
1676                                 bcommand |= PCI_BRIDGE_CTL_PARITY;
1677                         } else {
1678                                 command &= ~PCI_COMMAND_PARITY;
1679                                 bcommand &= ~PCI_BRIDGE_CTL_PARITY;
1680                         }
1681                         if (ab->_hpp->enable_serr) {
1682                                 command |= PCI_COMMAND_SERR;
1683                                 bcommand |= PCI_BRIDGE_CTL_SERR;
1684                         } else {
1685                                 command &= ~PCI_COMMAND_SERR;
1686                                 bcommand &= ~PCI_BRIDGE_CTL_SERR;
1687                         }
1688                 } else
1689                         dbg("no _hpp for B/D/F = %#x/%#x/%#x.\n", func->bus, func->device, func->function);
1690         } else
1691                 dbg("no acpi bridge for B/D/F = %#x/%#x/%#x.\n", func->bus, func->device, func->function);
1692
1693         if (command != cmd) {
1694                 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
1695         }
1696         if ((card_type == PCI_HEADER_TYPE_BRIDGE) && (bcommand != bcmd)) {
1697                 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, bcommand);
1698         }
1699 }
1700