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