linux/arch/powerpc/platforms/ps3/device-init.c
<<
>>
Prefs
   1/*
   2 *  PS3 device registration routines.
   3 *
   4 *  Copyright (C) 2007 Sony Computer Entertainment Inc.
   5 *  Copyright 2007 Sony Corp.
   6 *
   7 *  This program is free software; you can redistribute it and/or modify
   8 *  it under the terms of the GNU General Public License as published by
   9 *  the Free Software Foundation; version 2 of the License.
  10 *
  11 *  This program is distributed in the hope that it will be useful,
  12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14 *  GNU General Public License for more details.
  15 *
  16 *  You should have received a copy of the GNU General Public License
  17 *  along with this program; if not, write to the Free Software
  18 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19 */
  20
  21#include <linux/delay.h>
  22#include <linux/freezer.h>
  23#include <linux/kernel.h>
  24#include <linux/kthread.h>
  25#include <linux/init.h>
  26#include <linux/slab.h>
  27#include <linux/reboot.h>
  28
  29#include <asm/firmware.h>
  30#include <asm/lv1call.h>
  31#include <asm/ps3stor.h>
  32
  33#include "platform.h"
  34
  35static int __init ps3_register_lpm_devices(void)
  36{
  37        int result;
  38        u64 tmp1;
  39        u64 tmp2;
  40        struct ps3_system_bus_device *dev;
  41
  42        pr_debug(" -> %s:%d\n", __func__, __LINE__);
  43
  44        dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  45        if (!dev)
  46                return -ENOMEM;
  47
  48        dev->match_id = PS3_MATCH_ID_LPM;
  49        dev->dev_type = PS3_DEVICE_TYPE_LPM;
  50
  51        /* The current lpm driver only supports a single BE processor. */
  52
  53        result = ps3_repository_read_be_node_id(0, &dev->lpm.node_id);
  54
  55        if (result) {
  56                pr_debug("%s:%d: ps3_repository_read_be_node_id failed \n",
  57                        __func__, __LINE__);
  58                goto fail_read_repo;
  59        }
  60
  61        result = ps3_repository_read_lpm_privileges(dev->lpm.node_id, &tmp1,
  62                &dev->lpm.rights);
  63
  64        if (result) {
  65                pr_debug("%s:%d: ps3_repository_read_lpm_privileges failed\n",
  66                        __func__, __LINE__);
  67                goto fail_read_repo;
  68        }
  69
  70        lv1_get_logical_partition_id(&tmp2);
  71
  72        if (tmp1 != tmp2) {
  73                pr_debug("%s:%d: wrong lpar\n",
  74                        __func__, __LINE__);
  75                result = -ENODEV;
  76                goto fail_rights;
  77        }
  78
  79        if (!(dev->lpm.rights & PS3_LPM_RIGHTS_USE_LPM)) {
  80                pr_debug("%s:%d: don't have rights to use lpm\n",
  81                        __func__, __LINE__);
  82                result = -EPERM;
  83                goto fail_rights;
  84        }
  85
  86        pr_debug("%s:%d: pu_id %llu, rights %llu(%llxh)\n",
  87                __func__, __LINE__, dev->lpm.pu_id, dev->lpm.rights,
  88                dev->lpm.rights);
  89
  90        result = ps3_repository_read_pu_id(0, &dev->lpm.pu_id);
  91
  92        if (result) {
  93                pr_debug("%s:%d: ps3_repository_read_pu_id failed \n",
  94                        __func__, __LINE__);
  95                goto fail_read_repo;
  96        }
  97
  98        result = ps3_system_bus_device_register(dev);
  99
 100        if (result) {
 101                pr_debug("%s:%d ps3_system_bus_device_register failed\n",
 102                        __func__, __LINE__);
 103                goto fail_register;
 104        }
 105
 106        pr_debug(" <- %s:%d\n", __func__, __LINE__);
 107        return 0;
 108
 109
 110fail_register:
 111fail_rights:
 112fail_read_repo:
 113        kfree(dev);
 114        pr_debug(" <- %s:%d: failed\n", __func__, __LINE__);
 115        return result;
 116}
 117
 118/**
 119 * ps3_setup_gelic_device - Setup and register a gelic device instance.
 120 *
 121 * Allocates memory for a struct ps3_system_bus_device instance, initialises the
 122 * structure members, and registers the device instance with the system bus.
 123 */
 124
 125static int __init ps3_setup_gelic_device(
 126        const struct ps3_repository_device *repo)
 127{
 128        int result;
 129        struct layout {
 130                struct ps3_system_bus_device dev;
 131                struct ps3_dma_region d_region;
 132        } *p;
 133
 134        pr_debug(" -> %s:%d\n", __func__, __LINE__);
 135
 136        BUG_ON(repo->bus_type != PS3_BUS_TYPE_SB);
 137        BUG_ON(repo->dev_type != PS3_DEV_TYPE_SB_GELIC);
 138
 139        p = kzalloc(sizeof(struct layout), GFP_KERNEL);
 140
 141        if (!p) {
 142                result = -ENOMEM;
 143                goto fail_malloc;
 144        }
 145
 146        p->dev.match_id = PS3_MATCH_ID_GELIC;
 147        p->dev.dev_type = PS3_DEVICE_TYPE_SB;
 148        p->dev.bus_id = repo->bus_id;
 149        p->dev.dev_id = repo->dev_id;
 150        p->dev.d_region = &p->d_region;
 151
 152        result = ps3_repository_find_interrupt(repo,
 153                PS3_INTERRUPT_TYPE_EVENT_PORT, &p->dev.interrupt_id);
 154
 155        if (result) {
 156                pr_debug("%s:%d ps3_repository_find_interrupt failed\n",
 157                        __func__, __LINE__);
 158                goto fail_find_interrupt;
 159        }
 160
 161        BUG_ON(p->dev.interrupt_id != 0);
 162
 163        result = ps3_dma_region_init(&p->dev, p->dev.d_region, PS3_DMA_64K,
 164                PS3_DMA_OTHER, NULL, 0);
 165
 166        if (result) {
 167                pr_debug("%s:%d ps3_dma_region_init failed\n",
 168                        __func__, __LINE__);
 169                goto fail_dma_init;
 170        }
 171
 172        result = ps3_system_bus_device_register(&p->dev);
 173
 174        if (result) {
 175                pr_debug("%s:%d ps3_system_bus_device_register failed\n",
 176                        __func__, __LINE__);
 177                goto fail_device_register;
 178        }
 179
 180        pr_debug(" <- %s:%d\n", __func__, __LINE__);
 181        return result;
 182
 183fail_device_register:
 184fail_dma_init:
 185fail_find_interrupt:
 186        kfree(p);
 187fail_malloc:
 188        pr_debug(" <- %s:%d: fail.\n", __func__, __LINE__);
 189        return result;
 190}
 191
 192static int __ref ps3_setup_uhc_device(
 193        const struct ps3_repository_device *repo, enum ps3_match_id match_id,
 194        enum ps3_interrupt_type interrupt_type, enum ps3_reg_type reg_type)
 195{
 196        int result;
 197        struct layout {
 198                struct ps3_system_bus_device dev;
 199                struct ps3_dma_region d_region;
 200                struct ps3_mmio_region m_region;
 201        } *p;
 202        u64 bus_addr;
 203        u64 len;
 204
 205        pr_debug(" -> %s:%d\n", __func__, __LINE__);
 206
 207        BUG_ON(repo->bus_type != PS3_BUS_TYPE_SB);
 208        BUG_ON(repo->dev_type != PS3_DEV_TYPE_SB_USB);
 209
 210        p = kzalloc(sizeof(struct layout), GFP_KERNEL);
 211
 212        if (!p) {
 213                result = -ENOMEM;
 214                goto fail_malloc;
 215        }
 216
 217        p->dev.match_id = match_id;
 218        p->dev.dev_type = PS3_DEVICE_TYPE_SB;
 219        p->dev.bus_id = repo->bus_id;
 220        p->dev.dev_id = repo->dev_id;
 221        p->dev.d_region = &p->d_region;
 222        p->dev.m_region = &p->m_region;
 223
 224        result = ps3_repository_find_interrupt(repo,
 225                interrupt_type, &p->dev.interrupt_id);
 226
 227        if (result) {
 228                pr_debug("%s:%d ps3_repository_find_interrupt failed\n",
 229                        __func__, __LINE__);
 230                goto fail_find_interrupt;
 231        }
 232
 233        result = ps3_repository_find_reg(repo, reg_type,
 234                &bus_addr, &len);
 235
 236        if (result) {
 237                pr_debug("%s:%d ps3_repository_find_reg failed\n",
 238                        __func__, __LINE__);
 239                goto fail_find_reg;
 240        }
 241
 242        result = ps3_dma_region_init(&p->dev, p->dev.d_region, PS3_DMA_64K,
 243                PS3_DMA_INTERNAL, NULL, 0);
 244
 245        if (result) {
 246                pr_debug("%s:%d ps3_dma_region_init failed\n",
 247                        __func__, __LINE__);
 248                goto fail_dma_init;
 249        }
 250
 251        result = ps3_mmio_region_init(&p->dev, p->dev.m_region, bus_addr, len,
 252                PS3_MMIO_4K);
 253
 254        if (result) {
 255                pr_debug("%s:%d ps3_mmio_region_init failed\n",
 256                        __func__, __LINE__);
 257                goto fail_mmio_init;
 258        }
 259
 260        result = ps3_system_bus_device_register(&p->dev);
 261
 262        if (result) {
 263                pr_debug("%s:%d ps3_system_bus_device_register failed\n",
 264                        __func__, __LINE__);
 265                goto fail_device_register;
 266        }
 267
 268        pr_debug(" <- %s:%d\n", __func__, __LINE__);
 269        return result;
 270
 271fail_device_register:
 272fail_mmio_init:
 273fail_dma_init:
 274fail_find_reg:
 275fail_find_interrupt:
 276        kfree(p);
 277fail_malloc:
 278        pr_debug(" <- %s:%d: fail.\n", __func__, __LINE__);
 279        return result;
 280}
 281
 282static int __init ps3_setup_ehci_device(
 283        const struct ps3_repository_device *repo)
 284{
 285        return ps3_setup_uhc_device(repo, PS3_MATCH_ID_EHCI,
 286                PS3_INTERRUPT_TYPE_SB_EHCI, PS3_REG_TYPE_SB_EHCI);
 287}
 288
 289static int __init ps3_setup_ohci_device(
 290        const struct ps3_repository_device *repo)
 291{
 292        return ps3_setup_uhc_device(repo, PS3_MATCH_ID_OHCI,
 293                PS3_INTERRUPT_TYPE_SB_OHCI, PS3_REG_TYPE_SB_OHCI);
 294}
 295
 296static int __init ps3_setup_vuart_device(enum ps3_match_id match_id,
 297        unsigned int port_number)
 298{
 299        int result;
 300        struct layout {
 301                struct ps3_system_bus_device dev;
 302        } *p;
 303
 304        pr_debug(" -> %s:%d: match_id %u, port %u\n", __func__, __LINE__,
 305                match_id, port_number);
 306
 307        p = kzalloc(sizeof(struct layout), GFP_KERNEL);
 308
 309        if (!p)
 310                return -ENOMEM;
 311
 312        p->dev.match_id = match_id;
 313        p->dev.dev_type = PS3_DEVICE_TYPE_VUART;
 314        p->dev.port_number = port_number;
 315
 316        result = ps3_system_bus_device_register(&p->dev);
 317
 318        if (result) {
 319                pr_debug("%s:%d ps3_system_bus_device_register failed\n",
 320                        __func__, __LINE__);
 321                goto fail_device_register;
 322        }
 323        pr_debug(" <- %s:%d\n", __func__, __LINE__);
 324        return 0;
 325
 326fail_device_register:
 327        kfree(p);
 328        pr_debug(" <- %s:%d fail\n", __func__, __LINE__);
 329        return result;
 330}
 331
 332static int ps3_setup_storage_dev(const struct ps3_repository_device *repo,
 333                                 enum ps3_match_id match_id)
 334{
 335        int result;
 336        struct ps3_storage_device *p;
 337        u64 port, blk_size, num_blocks;
 338        unsigned int num_regions, i;
 339
 340        pr_debug(" -> %s:%u: match_id %u\n", __func__, __LINE__, match_id);
 341
 342        result = ps3_repository_read_stor_dev_info(repo->bus_index,
 343                                                   repo->dev_index, &port,
 344                                                   &blk_size, &num_blocks,
 345                                                   &num_regions);
 346        if (result) {
 347                printk(KERN_ERR "%s:%u: _read_stor_dev_info failed %d\n",
 348                       __func__, __LINE__, result);
 349                return -ENODEV;
 350        }
 351
 352        pr_debug("%s:%u: (%u:%u:%u): port %llu blk_size %llu num_blocks %llu "
 353                 "num_regions %u\n", __func__, __LINE__, repo->bus_index,
 354                 repo->dev_index, repo->dev_type, port, blk_size, num_blocks,
 355                 num_regions);
 356
 357        p = kzalloc(struct_size(p, regions, num_regions), GFP_KERNEL);
 358        if (!p) {
 359                result = -ENOMEM;
 360                goto fail_malloc;
 361        }
 362
 363        p->sbd.match_id = match_id;
 364        p->sbd.dev_type = PS3_DEVICE_TYPE_SB;
 365        p->sbd.bus_id = repo->bus_id;
 366        p->sbd.dev_id = repo->dev_id;
 367        p->sbd.d_region = &p->dma_region;
 368        p->blk_size = blk_size;
 369        p->num_regions = num_regions;
 370
 371        result = ps3_repository_find_interrupt(repo,
 372                                               PS3_INTERRUPT_TYPE_EVENT_PORT,
 373                                               &p->sbd.interrupt_id);
 374        if (result) {
 375                printk(KERN_ERR "%s:%u: find_interrupt failed %d\n", __func__,
 376                       __LINE__, result);
 377                result = -ENODEV;
 378                goto fail_find_interrupt;
 379        }
 380
 381        for (i = 0; i < num_regions; i++) {
 382                unsigned int id;
 383                u64 start, size;
 384
 385                result = ps3_repository_read_stor_dev_region(repo->bus_index,
 386                                                             repo->dev_index,
 387                                                             i, &id, &start,
 388                                                             &size);
 389                if (result) {
 390                        printk(KERN_ERR
 391                               "%s:%u: read_stor_dev_region failed %d\n",
 392                               __func__, __LINE__, result);
 393                        result = -ENODEV;
 394                        goto fail_read_region;
 395                }
 396                pr_debug("%s:%u: region %u: id %u start %llu size %llu\n",
 397                         __func__, __LINE__, i, id, start, size);
 398
 399                p->regions[i].id = id;
 400                p->regions[i].start = start;
 401                p->regions[i].size = size;
 402        }
 403
 404        result = ps3_system_bus_device_register(&p->sbd);
 405        if (result) {
 406                pr_debug("%s:%u ps3_system_bus_device_register failed\n",
 407                         __func__, __LINE__);
 408                goto fail_device_register;
 409        }
 410
 411        pr_debug(" <- %s:%u\n", __func__, __LINE__);
 412        return 0;
 413
 414fail_device_register:
 415fail_read_region:
 416fail_find_interrupt:
 417        kfree(p);
 418fail_malloc:
 419        pr_debug(" <- %s:%u: fail.\n", __func__, __LINE__);
 420        return result;
 421}
 422
 423static int __init ps3_register_vuart_devices(void)
 424{
 425        int result;
 426        unsigned int port_number;
 427
 428        pr_debug(" -> %s:%d\n", __func__, __LINE__);
 429
 430        result = ps3_repository_read_vuart_av_port(&port_number);
 431        if (result)
 432                port_number = 0; /* av default */
 433
 434        result = ps3_setup_vuart_device(PS3_MATCH_ID_AV_SETTINGS, port_number);
 435        WARN_ON(result);
 436
 437        result = ps3_repository_read_vuart_sysmgr_port(&port_number);
 438        if (result)
 439                port_number = 2; /* sysmgr default */
 440
 441        result = ps3_setup_vuart_device(PS3_MATCH_ID_SYSTEM_MANAGER,
 442                port_number);
 443        WARN_ON(result);
 444
 445        pr_debug(" <- %s:%d\n", __func__, __LINE__);
 446        return result;
 447}
 448
 449static int __init ps3_register_sound_devices(void)
 450{
 451        int result;
 452        struct layout {
 453                struct ps3_system_bus_device dev;
 454                struct ps3_dma_region d_region;
 455                struct ps3_mmio_region m_region;
 456        } *p;
 457
 458        pr_debug(" -> %s:%d\n", __func__, __LINE__);
 459
 460        p = kzalloc(sizeof(*p), GFP_KERNEL);
 461        if (!p)
 462                return -ENOMEM;
 463
 464        p->dev.match_id = PS3_MATCH_ID_SOUND;
 465        p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
 466        p->dev.d_region = &p->d_region;
 467        p->dev.m_region = &p->m_region;
 468
 469        result = ps3_system_bus_device_register(&p->dev);
 470
 471        if (result) {
 472                pr_debug("%s:%d ps3_system_bus_device_register failed\n",
 473                        __func__, __LINE__);
 474                goto fail_device_register;
 475        }
 476        pr_debug(" <- %s:%d\n", __func__, __LINE__);
 477        return 0;
 478
 479fail_device_register:
 480        kfree(p);
 481        pr_debug(" <- %s:%d failed\n", __func__, __LINE__);
 482        return result;
 483}
 484
 485static int __init ps3_register_graphics_devices(void)
 486{
 487        int result;
 488        struct layout {
 489                struct ps3_system_bus_device dev;
 490        } *p;
 491
 492        pr_debug(" -> %s:%d\n", __func__, __LINE__);
 493
 494        p = kzalloc(sizeof(struct layout), GFP_KERNEL);
 495
 496        if (!p)
 497                return -ENOMEM;
 498
 499        p->dev.match_id = PS3_MATCH_ID_GPU;
 500        p->dev.match_sub_id = PS3_MATCH_SUB_ID_GPU_FB;
 501        p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
 502
 503        result = ps3_system_bus_device_register(&p->dev);
 504
 505        if (result) {
 506                pr_debug("%s:%d ps3_system_bus_device_register failed\n",
 507                        __func__, __LINE__);
 508                goto fail_device_register;
 509        }
 510
 511        pr_debug(" <- %s:%d\n", __func__, __LINE__);
 512        return 0;
 513
 514fail_device_register:
 515        kfree(p);
 516        pr_debug(" <- %s:%d failed\n", __func__, __LINE__);
 517        return result;
 518}
 519
 520static int __init ps3_register_ramdisk_device(void)
 521{
 522        int result;
 523        struct layout {
 524                struct ps3_system_bus_device dev;
 525        } *p;
 526
 527        pr_debug(" -> %s:%d\n", __func__, __LINE__);
 528
 529        p = kzalloc(sizeof(struct layout), GFP_KERNEL);
 530
 531        if (!p)
 532                return -ENOMEM;
 533
 534        p->dev.match_id = PS3_MATCH_ID_GPU;
 535        p->dev.match_sub_id = PS3_MATCH_SUB_ID_GPU_RAMDISK;
 536        p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
 537
 538        result = ps3_system_bus_device_register(&p->dev);
 539
 540        if (result) {
 541                pr_debug("%s:%d ps3_system_bus_device_register failed\n",
 542                        __func__, __LINE__);
 543                goto fail_device_register;
 544        }
 545
 546        pr_debug(" <- %s:%d\n", __func__, __LINE__);
 547        return 0;
 548
 549fail_device_register:
 550        kfree(p);
 551        pr_debug(" <- %s:%d failed\n", __func__, __LINE__);
 552        return result;
 553}
 554
 555/**
 556 * ps3_setup_dynamic_device - Setup a dynamic device from the repository
 557 */
 558
 559static int ps3_setup_dynamic_device(const struct ps3_repository_device *repo)
 560{
 561        int result;
 562
 563        switch (repo->dev_type) {
 564        case PS3_DEV_TYPE_STOR_DISK:
 565                result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_DISK);
 566
 567                /* Some devices are not accessible from the Other OS lpar. */
 568                if (result == -ENODEV) {
 569                        result = 0;
 570                        pr_debug("%s:%u: not accessible\n", __func__,
 571                                 __LINE__);
 572                }
 573
 574                if (result)
 575                        pr_debug("%s:%u ps3_setup_storage_dev failed\n",
 576                                 __func__, __LINE__);
 577                break;
 578
 579        case PS3_DEV_TYPE_STOR_ROM:
 580                result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_ROM);
 581                if (result)
 582                        pr_debug("%s:%u ps3_setup_storage_dev failed\n",
 583                                 __func__, __LINE__);
 584                break;
 585
 586        case PS3_DEV_TYPE_STOR_FLASH:
 587                result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_FLASH);
 588                if (result)
 589                        pr_debug("%s:%u ps3_setup_storage_dev failed\n",
 590                                 __func__, __LINE__);
 591                break;
 592
 593        default:
 594                result = 0;
 595                pr_debug("%s:%u: unsupported dev_type %u\n", __func__, __LINE__,
 596                        repo->dev_type);
 597        }
 598
 599        return result;
 600}
 601
 602/**
 603 * ps3_setup_static_device - Setup a static device from the repository
 604 */
 605
 606static int __init ps3_setup_static_device(const struct ps3_repository_device *repo)
 607{
 608        int result;
 609
 610        switch (repo->dev_type) {
 611        case PS3_DEV_TYPE_SB_GELIC:
 612                result = ps3_setup_gelic_device(repo);
 613                if (result) {
 614                        pr_debug("%s:%d ps3_setup_gelic_device failed\n",
 615                                __func__, __LINE__);
 616                }
 617                break;
 618        case PS3_DEV_TYPE_SB_USB:
 619
 620                /* Each USB device has both an EHCI and an OHCI HC */
 621
 622                result = ps3_setup_ehci_device(repo);
 623
 624                if (result) {
 625                        pr_debug("%s:%d ps3_setup_ehci_device failed\n",
 626                                __func__, __LINE__);
 627                }
 628
 629                result = ps3_setup_ohci_device(repo);
 630
 631                if (result) {
 632                        pr_debug("%s:%d ps3_setup_ohci_device failed\n",
 633                                __func__, __LINE__);
 634                }
 635                break;
 636
 637        default:
 638                return ps3_setup_dynamic_device(repo);
 639        }
 640
 641        return result;
 642}
 643
 644static void ps3_find_and_add_device(u64 bus_id, u64 dev_id)
 645{
 646        struct ps3_repository_device repo;
 647        int res;
 648        unsigned int retries;
 649        unsigned long rem;
 650
 651        /*
 652         * On some firmware versions (e.g. 1.90), the device may not show up
 653         * in the repository immediately
 654         */
 655        for (retries = 0; retries < 10; retries++) {
 656                res = ps3_repository_find_device_by_id(&repo, bus_id, dev_id);
 657                if (!res)
 658                        goto found;
 659
 660                rem = msleep_interruptible(100);
 661                if (rem)
 662                        break;
 663        }
 664        pr_warn("%s:%u: device %llu:%llu not found\n",
 665                __func__, __LINE__, bus_id, dev_id);
 666        return;
 667
 668found:
 669        if (retries)
 670                pr_debug("%s:%u: device %llu:%llu found after %u retries\n",
 671                         __func__, __LINE__, bus_id, dev_id, retries);
 672
 673        ps3_setup_dynamic_device(&repo);
 674        return;
 675}
 676
 677#define PS3_NOTIFICATION_DEV_ID         ULONG_MAX
 678#define PS3_NOTIFICATION_INTERRUPT_ID   0
 679
 680struct ps3_notification_device {
 681        struct ps3_system_bus_device sbd;
 682        spinlock_t lock;
 683        u64 tag;
 684        u64 lv1_status;
 685        struct completion done;
 686};
 687
 688enum ps3_notify_type {
 689        notify_device_ready = 0,
 690        notify_region_probe = 1,
 691        notify_region_update = 2,
 692};
 693
 694struct ps3_notify_cmd {
 695        u64 operation_code;             /* must be zero */
 696        u64 event_mask;                 /* OR of 1UL << enum ps3_notify_type */
 697};
 698
 699struct ps3_notify_event {
 700        u64 event_type;                 /* enum ps3_notify_type */
 701        u64 bus_id;
 702        u64 dev_id;
 703        u64 dev_type;
 704        u64 dev_port;
 705};
 706
 707static irqreturn_t ps3_notification_interrupt(int irq, void *data)
 708{
 709        struct ps3_notification_device *dev = data;
 710        int res;
 711        u64 tag, status;
 712
 713        spin_lock(&dev->lock);
 714        res = lv1_storage_get_async_status(PS3_NOTIFICATION_DEV_ID, &tag,
 715                                           &status);
 716        if (tag != dev->tag)
 717                pr_err("%s:%u: tag mismatch, got %llx, expected %llx\n",
 718                       __func__, __LINE__, tag, dev->tag);
 719
 720        if (res) {
 721                pr_err("%s:%u: res %d status 0x%llx\n", __func__, __LINE__, res,
 722                       status);
 723        } else {
 724                pr_debug("%s:%u: completed, status 0x%llx\n", __func__,
 725                         __LINE__, status);
 726                dev->lv1_status = status;
 727                complete(&dev->done);
 728        }
 729        spin_unlock(&dev->lock);
 730        return IRQ_HANDLED;
 731}
 732
 733static int ps3_notification_read_write(struct ps3_notification_device *dev,
 734                                       u64 lpar, int write)
 735{
 736        const char *op = write ? "write" : "read";
 737        unsigned long flags;
 738        int res;
 739
 740        init_completion(&dev->done);
 741        spin_lock_irqsave(&dev->lock, flags);
 742        res = write ? lv1_storage_write(dev->sbd.dev_id, 0, 0, 1, 0, lpar,
 743                                        &dev->tag)
 744                    : lv1_storage_read(dev->sbd.dev_id, 0, 0, 1, 0, lpar,
 745                                       &dev->tag);
 746        spin_unlock_irqrestore(&dev->lock, flags);
 747        if (res) {
 748                pr_err("%s:%u: %s failed %d\n", __func__, __LINE__, op, res);
 749                return -EPERM;
 750        }
 751        pr_debug("%s:%u: notification %s issued\n", __func__, __LINE__, op);
 752
 753        res = wait_event_interruptible(dev->done.wait,
 754                                       dev->done.done || kthread_should_stop());
 755        if (kthread_should_stop())
 756                res = -EINTR;
 757        if (res) {
 758                pr_debug("%s:%u: interrupted %s\n", __func__, __LINE__, op);
 759                return res;
 760        }
 761
 762        if (dev->lv1_status) {
 763                pr_err("%s:%u: %s not completed, status 0x%llx\n", __func__,
 764                       __LINE__, op, dev->lv1_status);
 765                return -EIO;
 766        }
 767        pr_debug("%s:%u: notification %s completed\n", __func__, __LINE__, op);
 768
 769        return 0;
 770}
 771
 772static struct task_struct *probe_task;
 773
 774/**
 775 * ps3_probe_thread - Background repository probing at system startup.
 776 *
 777 * This implementation only supports background probing on a single bus.
 778 * It uses the hypervisor's storage device notification mechanism to wait until
 779 * a storage device is ready.  The device notification mechanism uses a
 780 * pseudo device to asynchronously notify the guest when storage devices become
 781 * ready.  The notification device has a block size of 512 bytes.
 782 */
 783
 784static int ps3_probe_thread(void *data)
 785{
 786        struct ps3_notification_device dev;
 787        int res;
 788        unsigned int irq;
 789        u64 lpar;
 790        void *buf;
 791        struct ps3_notify_cmd *notify_cmd;
 792        struct ps3_notify_event *notify_event;
 793
 794        pr_debug(" -> %s:%u: kthread started\n", __func__, __LINE__);
 795
 796        buf = kzalloc(512, GFP_KERNEL);
 797        if (!buf)
 798                return -ENOMEM;
 799
 800        lpar = ps3_mm_phys_to_lpar(__pa(buf));
 801        notify_cmd = buf;
 802        notify_event = buf;
 803
 804        /* dummy system bus device */
 805        dev.sbd.bus_id = (u64)data;
 806        dev.sbd.dev_id = PS3_NOTIFICATION_DEV_ID;
 807        dev.sbd.interrupt_id = PS3_NOTIFICATION_INTERRUPT_ID;
 808
 809        res = lv1_open_device(dev.sbd.bus_id, dev.sbd.dev_id, 0);
 810        if (res) {
 811                pr_err("%s:%u: lv1_open_device failed %s\n", __func__,
 812                       __LINE__, ps3_result(res));
 813                goto fail_free;
 814        }
 815
 816        res = ps3_sb_event_receive_port_setup(&dev.sbd, PS3_BINDING_CPU_ANY,
 817                                              &irq);
 818        if (res) {
 819                pr_err("%s:%u: ps3_sb_event_receive_port_setup failed %d\n",
 820                       __func__, __LINE__, res);
 821               goto fail_close_device;
 822        }
 823
 824        spin_lock_init(&dev.lock);
 825
 826        res = request_irq(irq, ps3_notification_interrupt, 0,
 827                          "ps3_notification", &dev);
 828        if (res) {
 829                pr_err("%s:%u: request_irq failed %d\n", __func__, __LINE__,
 830                       res);
 831                goto fail_sb_event_receive_port_destroy;
 832        }
 833
 834        /* Setup and write the request for device notification. */
 835        notify_cmd->operation_code = 0; /* must be zero */
 836        notify_cmd->event_mask = 1UL << notify_region_probe;
 837
 838        res = ps3_notification_read_write(&dev, lpar, 1);
 839        if (res)
 840                goto fail_free_irq;
 841
 842        /* Loop here processing the requested notification events. */
 843        do {
 844                try_to_freeze();
 845
 846                memset(notify_event, 0, sizeof(*notify_event));
 847
 848                res = ps3_notification_read_write(&dev, lpar, 0);
 849                if (res)
 850                        break;
 851
 852                pr_debug("%s:%u: notify event type 0x%llx bus id %llu dev id %llu"
 853                         " type %llu port %llu\n", __func__, __LINE__,
 854                         notify_event->event_type, notify_event->bus_id,
 855                         notify_event->dev_id, notify_event->dev_type,
 856                         notify_event->dev_port);
 857
 858                if (notify_event->event_type != notify_region_probe ||
 859                    notify_event->bus_id != dev.sbd.bus_id) {
 860                        pr_warn("%s:%u: bad notify_event: event %llu, dev_id %llu, dev_type %llu\n",
 861                                __func__, __LINE__, notify_event->event_type,
 862                                notify_event->dev_id, notify_event->dev_type);
 863                        continue;
 864                }
 865
 866                ps3_find_and_add_device(dev.sbd.bus_id, notify_event->dev_id);
 867
 868        } while (!kthread_should_stop());
 869
 870fail_free_irq:
 871        free_irq(irq, &dev);
 872fail_sb_event_receive_port_destroy:
 873        ps3_sb_event_receive_port_destroy(&dev.sbd, irq);
 874fail_close_device:
 875        lv1_close_device(dev.sbd.bus_id, dev.sbd.dev_id);
 876fail_free:
 877        kfree(buf);
 878
 879        probe_task = NULL;
 880
 881        pr_debug(" <- %s:%u: kthread finished\n", __func__, __LINE__);
 882
 883        return 0;
 884}
 885
 886/**
 887 * ps3_stop_probe_thread - Stops the background probe thread.
 888 *
 889 */
 890
 891static int ps3_stop_probe_thread(struct notifier_block *nb, unsigned long code,
 892                                 void *data)
 893{
 894        if (probe_task)
 895                kthread_stop(probe_task);
 896        return 0;
 897}
 898
 899static struct notifier_block nb = {
 900        .notifier_call = ps3_stop_probe_thread
 901};
 902
 903/**
 904 * ps3_start_probe_thread - Starts the background probe thread.
 905 *
 906 */
 907
 908static int __init ps3_start_probe_thread(enum ps3_bus_type bus_type)
 909{
 910        int result;
 911        struct task_struct *task;
 912        struct ps3_repository_device repo;
 913
 914        pr_debug(" -> %s:%d\n", __func__, __LINE__);
 915
 916        memset(&repo, 0, sizeof(repo));
 917
 918        repo.bus_type = bus_type;
 919
 920        result = ps3_repository_find_bus(repo.bus_type, 0, &repo.bus_index);
 921
 922        if (result) {
 923                printk(KERN_ERR "%s: Cannot find bus (%d)\n", __func__, result);
 924                return -ENODEV;
 925        }
 926
 927        result = ps3_repository_read_bus_id(repo.bus_index, &repo.bus_id);
 928
 929        if (result) {
 930                printk(KERN_ERR "%s: read_bus_id failed %d\n", __func__,
 931                        result);
 932                return -ENODEV;
 933        }
 934
 935        task = kthread_run(ps3_probe_thread, (void *)repo.bus_id,
 936                           "ps3-probe-%u", bus_type);
 937
 938        if (IS_ERR(task)) {
 939                result = PTR_ERR(task);
 940                printk(KERN_ERR "%s: kthread_run failed %d\n", __func__,
 941                       result);
 942                return result;
 943        }
 944
 945        probe_task = task;
 946        register_reboot_notifier(&nb);
 947
 948        pr_debug(" <- %s:%d\n", __func__, __LINE__);
 949        return 0;
 950}
 951
 952/**
 953 * ps3_register_devices - Probe the system and register devices found.
 954 *
 955 * A device_initcall() routine.
 956 */
 957
 958static int __init ps3_register_devices(void)
 959{
 960        int result;
 961
 962        if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
 963                return -ENODEV;
 964
 965        pr_debug(" -> %s:%d\n", __func__, __LINE__);
 966
 967        /* ps3_repository_dump_bus_info(); */
 968
 969        result = ps3_start_probe_thread(PS3_BUS_TYPE_STORAGE);
 970
 971        ps3_register_vuart_devices();
 972
 973        ps3_register_graphics_devices();
 974
 975        ps3_repository_find_devices(PS3_BUS_TYPE_SB, ps3_setup_static_device);
 976
 977        ps3_register_sound_devices();
 978
 979        ps3_register_lpm_devices();
 980
 981        ps3_register_ramdisk_device();
 982
 983        pr_debug(" <- %s:%d\n", __func__, __LINE__);
 984        return 0;
 985}
 986
 987device_initcall(ps3_register_devices);
 988