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