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