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