2  * PS3 Logical Performance Monitor.
 
   4  *  Copyright (C) 2007 Sony Computer Entertainment Inc.
 
   5  *  Copyright 2007 Sony Corp.
 
   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.
 
  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.
 
  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
 
  21 #include <linux/kernel.h>
 
  22 #include <linux/module.h>
 
  23 #include <linux/interrupt.h>
 
  24 #include <linux/uaccess.h>
 
  27 #include <asm/lv1call.h>
 
  28 #include <asm/cell-pmu.h>
 
  31 /* BOOKMARK tag macros */
 
  32 #define PS3_PM_BOOKMARK_START                    0x8000000000000000ULL
 
  33 #define PS3_PM_BOOKMARK_STOP                     0x4000000000000000ULL
 
  34 #define PS3_PM_BOOKMARK_TAG_KERNEL               0x1000000000000000ULL
 
  35 #define PS3_PM_BOOKMARK_TAG_USER                 0x3000000000000000ULL
 
  36 #define PS3_PM_BOOKMARK_TAG_MASK_HI              0xF000000000000000ULL
 
  37 #define PS3_PM_BOOKMARK_TAG_MASK_LO              0x0F00000000000000ULL
 
  39 /* CBE PM CONTROL register macros */
 
  40 #define PS3_PM_CONTROL_PPU_TH0_BOOKMARK          0x00001000
 
  41 #define PS3_PM_CONTROL_PPU_TH1_BOOKMARK          0x00000800
 
  42 #define PS3_PM_CONTROL_PPU_COUNT_MODE_MASK       0x000C0000
 
  43 #define PS3_PM_CONTROL_PPU_COUNT_MODE_PROBLEM    0x00080000
 
  44 #define PS3_WRITE_PM_MASK                        0xFFFFFFFFFFFFFFFFULL
 
  46 /* CBE PM START STOP register macros */
 
  47 #define PS3_PM_START_STOP_PPU_TH0_BOOKMARK_START 0x02000000
 
  48 #define PS3_PM_START_STOP_PPU_TH1_BOOKMARK_START 0x01000000
 
  49 #define PS3_PM_START_STOP_PPU_TH0_BOOKMARK_STOP  0x00020000
 
  50 #define PS3_PM_START_STOP_PPU_TH1_BOOKMARK_STOP  0x00010000
 
  51 #define PS3_PM_START_STOP_START_MASK             0xFF000000
 
  52 #define PS3_PM_START_STOP_STOP_MASK              0x00FF0000
 
  54 /* CBE PM COUNTER register macres */
 
  55 #define PS3_PM_COUNTER_MASK_HI                   0xFFFFFFFF00000000ULL
 
  56 #define PS3_PM_COUNTER_MASK_LO                   0x00000000FFFFFFFFULL
 
  58 /* BASE SIGNAL GROUP NUMBER macros */
 
  59 #define PM_ISLAND2_BASE_SIGNAL_GROUP_NUMBER  0
 
  60 #define PM_ISLAND2_SIGNAL_GROUP_NUMBER1      6
 
  61 #define PM_ISLAND2_SIGNAL_GROUP_NUMBER2      7
 
  62 #define PM_ISLAND3_BASE_SIGNAL_GROUP_NUMBER  7
 
  63 #define PM_ISLAND4_BASE_SIGNAL_GROUP_NUMBER  15
 
  64 #define PM_SPU_TRIGGER_SIGNAL_GROUP_NUMBER   17
 
  65 #define PM_SPU_EVENT_SIGNAL_GROUP_NUMBER     18
 
  66 #define PM_ISLAND5_BASE_SIGNAL_GROUP_NUMBER  18
 
  67 #define PM_ISLAND6_BASE_SIGNAL_GROUP_NUMBER  24
 
  68 #define PM_ISLAND7_BASE_SIGNAL_GROUP_NUMBER  49
 
  69 #define PM_ISLAND8_BASE_SIGNAL_GROUP_NUMBER  52
 
  70 #define PM_SIG_GROUP_SPU                     41
 
  71 #define PM_SIG_GROUP_SPU_TRIGGER             42
 
  72 #define PM_SIG_GROUP_SPU_EVENT               43
 
  73 #define PM_SIG_GROUP_MFC_MAX                 60
 
  76  * struct ps3_lpm_shadow_regs - Performance monitor shadow registers.
 
  78  * @pm_control: Shadow of the processor's pm_control register.
 
  79  * @pm_start_stop: Shadow of the processor's pm_start_stop register.
 
  80  * @group_control: Shadow of the processor's group_control register.
 
  81  * @debug_bus_control: Shadow of the processor's debug_bus_control register.
 
  83  * The logical performance monitor provides a write-only interface to
 
  84  * these processor registers.  These shadow variables cache the processor
 
  85  * register values for reading.
 
  87  * The initial value of the shadow registers at lpm creation is
 
  88  * PS3_LPM_SHADOW_REG_INIT.
 
  91 struct ps3_lpm_shadow_regs {
 
  95         u64 debug_bus_control;
 
  98 #define PS3_LPM_SHADOW_REG_INIT 0xFFFFFFFF00000000ULL
 
 101  * struct ps3_lpm_priv - Private lpm device data.
 
 103  * @open: An atomic variable indicating the lpm driver has been opened.
 
 104  * @rights: The lpm rigths granted by the system policy module.  A logical
 
 105  *  OR of enum ps3_lpm_rights.
 
 106  * @node_id: The node id of a BE prosessor whose performance monitor this
 
 107  *  lpar has the right to use.
 
 108  * @pu_id: The lv1 id of the logical PU.
 
 109  * @lpm_id: The lv1 id of this lpm instance.
 
 110  * @outlet_id: The outlet created by lv1 for this lpm instance.
 
 111  * @tb_count: The number of bytes of data held in the lv1 trace buffer.
 
 112  * @tb_cache: Kernel buffer to receive the data from the lv1 trace buffer.
 
 113  *  Must be 128 byte aligned.
 
 114  * @tb_cache_size: Size of the kernel @tb_cache buffer.  Must be 128 byte
 
 116  * @tb_cache_internal: An unaligned buffer allocated by this driver to be
 
 117  *  used for the trace buffer cache when ps3_lpm_open() is called with a
 
 118  *  NULL tb_cache argument.  Otherwise unused.
 
 119  * @shadow: Processor register shadow of type struct ps3_lpm_shadow_regs.
 
 120  * @sbd: The struct ps3_system_bus_device attached to this driver.
 
 122  * The trace buffer is a buffer allocated and used internally to the lv1
 
 123  * hypervisor to collect trace data.  The trace buffer cache is a guest
 
 124  * buffer that accepts the trace data from the trace buffer.
 
 127 struct ps3_lpm_priv {
 
 137         void *tb_cache_internal;
 
 138         struct ps3_lpm_shadow_regs shadow;
 
 139         struct ps3_system_bus_device *sbd;
 
 143         PS3_LPM_DEFAULT_TB_CACHE_SIZE = 0x4000,
 
 147  * lpm_priv - Static instance of the lpm data.
 
 149  * Since the exported routines don't support the notion of a device
 
 150  * instance we need to hold the instance in this static variable
 
 151  * and then only allow at most one instance at a time to be created.
 
 154 static struct ps3_lpm_priv *lpm_priv;
 
 156 static struct device *sbd_core(void)
 
 158         BUG_ON(!lpm_priv || !lpm_priv->sbd);
 
 159         return &lpm_priv->sbd->core;
 
 163  * use_start_stop_bookmark - Enable the PPU bookmark trace.
 
 165  * And it enables PPU bookmark triggers ONLY if the other triggers are not set.
 
 166  * The start/stop bookmarks are inserted at ps3_enable_pm() and ps3_disable_pm()
 
 169  * Used to get good quality of the performance counter.
 
 172 enum {use_start_stop_bookmark = 1,};
 
 174 void ps3_set_bookmark(u64 bookmark)
 
 177          * As per the PPE book IV, to avoid bookmark loss there must
 
 178          * not be a traced branch within 10 cycles of setting the
 
 179          * SPRN_BKMK register.  The actual text is unclear if 'within'
 
 180          * includes cycles before the call.
 
 183         asm volatile("nop;nop;nop;nop;nop;nop;nop;nop;nop;");
 
 184         mtspr(SPRN_BKMK, bookmark);
 
 185         asm volatile("nop;nop;nop;nop;nop;nop;nop;nop;nop;");
 
 187 EXPORT_SYMBOL_GPL(ps3_set_bookmark);
 
 189 void ps3_set_pm_bookmark(u64 tag, u64 incident, u64 th_id)
 
 193         bookmark = (get_tb() & 0x00000000FFFFFFFFULL) |
 
 194                 PS3_PM_BOOKMARK_TAG_KERNEL;
 
 195         bookmark = ((tag << 56) & PS3_PM_BOOKMARK_TAG_MASK_LO) |
 
 196                 (incident << 48) | (th_id << 32) | bookmark;
 
 197         ps3_set_bookmark(bookmark);
 
 199 EXPORT_SYMBOL_GPL(ps3_set_pm_bookmark);
 
 202  * ps3_read_phys_ctr - Read physical counter registers.
 
 204  * Each physical counter can act as one 32 bit counter or as two 16 bit
 
 208 u32 ps3_read_phys_ctr(u32 cpu, u32 phys_ctr)
 
 214         if (phys_ctr >= NR_PHYS_CTRS) {
 
 215                 dev_dbg(sbd_core(), "%s:%u: phys_ctr too big: %u\n", __func__,
 
 220         result = lv1_set_lpm_counter(lpm_priv->lpm_id, 0, 0, 0, 0, &counter0415,
 
 223                 dev_err(sbd_core(), "%s:%u: lv1_set_lpm_counter failed: "
 
 224                         "phys_ctr %u, %s\n", __func__, __LINE__, phys_ctr,
 
 231                 return counter0415 >> 32;
 
 233                 return counter0415 & PS3_PM_COUNTER_MASK_LO;
 
 235                 return counter2637 >> 32;
 
 237                 return counter2637 & PS3_PM_COUNTER_MASK_LO;
 
 243 EXPORT_SYMBOL_GPL(ps3_read_phys_ctr);
 
 246  * ps3_write_phys_ctr - Write physical counter registers.
 
 248  * Each physical counter can act as one 32 bit counter or as two 16 bit
 
 252 void ps3_write_phys_ctr(u32 cpu, u32 phys_ctr, u32 val)
 
 255         u64 counter0415_mask;
 
 257         u64 counter2637_mask;
 
 260         if (phys_ctr >= NR_PHYS_CTRS) {
 
 261                 dev_dbg(sbd_core(), "%s:%u: phys_ctr too big: %u\n", __func__,
 
 268                 counter0415 = (u64)val << 32;
 
 269                 counter0415_mask = PS3_PM_COUNTER_MASK_HI;
 
 271                 counter2637_mask = 0x0;
 
 274                 counter0415 = (u64)val;
 
 275                 counter0415_mask = PS3_PM_COUNTER_MASK_LO;
 
 277                 counter2637_mask = 0x0;
 
 281                 counter0415_mask = 0x0;
 
 282                 counter2637 = (u64)val << 32;
 
 283                 counter2637_mask = PS3_PM_COUNTER_MASK_HI;
 
 287                 counter0415_mask = 0x0;
 
 288                 counter2637 = (u64)val;
 
 289                 counter2637_mask = PS3_PM_COUNTER_MASK_LO;
 
 295         result = lv1_set_lpm_counter(lpm_priv->lpm_id,
 
 296                                      counter0415, counter0415_mask,
 
 297                                      counter2637, counter2637_mask,
 
 298                                      &counter0415, &counter2637);
 
 300                 dev_err(sbd_core(), "%s:%u: lv1_set_lpm_counter failed: "
 
 301                         "phys_ctr %u, val %u, %s\n", __func__, __LINE__,
 
 302                         phys_ctr, val, ps3_result(result));
 
 304 EXPORT_SYMBOL_GPL(ps3_write_phys_ctr);
 
 307  * ps3_read_ctr - Read counter.
 
 309  * Read 16 or 32 bits depending on the current size of the counter.
 
 310  * Counters 4, 5, 6 & 7 are always 16 bit.
 
 313 u32 ps3_read_ctr(u32 cpu, u32 ctr)
 
 316         u32 phys_ctr = ctr & (NR_PHYS_CTRS - 1);
 
 318         val = ps3_read_phys_ctr(cpu, phys_ctr);
 
 320         if (ps3_get_ctr_size(cpu, phys_ctr) == 16)
 
 321                 val = (ctr < NR_PHYS_CTRS) ? (val >> 16) : (val & 0xffff);
 
 325 EXPORT_SYMBOL_GPL(ps3_read_ctr);
 
 328  * ps3_write_ctr - Write counter.
 
 330  * Write 16 or 32 bits depending on the current size of the counter.
 
 331  * Counters 4, 5, 6 & 7 are always 16 bit.
 
 334 void ps3_write_ctr(u32 cpu, u32 ctr, u32 val)
 
 339         phys_ctr = ctr & (NR_PHYS_CTRS - 1);
 
 341         if (ps3_get_ctr_size(cpu, phys_ctr) == 16) {
 
 342                 phys_val = ps3_read_phys_ctr(cpu, phys_ctr);
 
 344                 if (ctr < NR_PHYS_CTRS)
 
 345                         val = (val << 16) | (phys_val & 0xffff);
 
 347                         val = (val & 0xffff) | (phys_val & 0xffff0000);
 
 350         ps3_write_phys_ctr(cpu, phys_ctr, val);
 
 352 EXPORT_SYMBOL_GPL(ps3_write_ctr);
 
 355  * ps3_read_pm07_control - Read counter control registers.
 
 357  * Each logical counter has a corresponding control register.
 
 360 u32 ps3_read_pm07_control(u32 cpu, u32 ctr)
 
 364 EXPORT_SYMBOL_GPL(ps3_read_pm07_control);
 
 367  * ps3_write_pm07_control - Write counter control registers.
 
 369  * Each logical counter has a corresponding control register.
 
 372 void ps3_write_pm07_control(u32 cpu, u32 ctr, u32 val)
 
 375         static const u64 mask = 0xFFFFFFFFFFFFFFFFULL;
 
 378         if (ctr >= NR_CTRS) {
 
 379                 dev_dbg(sbd_core(), "%s:%u: ctr too big: %u\n", __func__,
 
 384         result = lv1_set_lpm_counter_control(lpm_priv->lpm_id, ctr, val, mask,
 
 387                 dev_err(sbd_core(), "%s:%u: lv1_set_lpm_counter_control "
 
 388                         "failed: ctr %u, %s\n", __func__, __LINE__, ctr,
 
 391 EXPORT_SYMBOL_GPL(ps3_write_pm07_control);
 
 394  * ps3_read_pm - Read Other LPM control registers.
 
 397 u32 ps3_read_pm(u32 cpu, enum pm_reg_name reg)
 
 404                 return lpm_priv->shadow.pm_control;
 
 406                 return CBE_PM_TRACE_BUF_EMPTY;
 
 408                 return lpm_priv->shadow.pm_start_stop;
 
 410                 result = lv1_set_lpm_interval(lpm_priv->lpm_id, 0, 0, &val);
 
 413                         dev_dbg(sbd_core(), "%s:%u: lv1 set_inteval failed: "
 
 414                                 "reg %u, %s\n", __func__, __LINE__, reg,
 
 419                 return lpm_priv->shadow.group_control;
 
 420         case debug_bus_control:
 
 421                 return lpm_priv->shadow.debug_bus_control;
 
 423                 result = lv1_get_lpm_interrupt_status(lpm_priv->lpm_id,
 
 427                         dev_dbg(sbd_core(), "%s:%u: lv1 get_lpm_status failed: "
 
 428                                 "reg %u, %s\n", __func__, __LINE__, reg,
 
 435                 dev_dbg(sbd_core(), "%s:%u: unknown reg: %d\n", __func__,
 
 443 EXPORT_SYMBOL_GPL(ps3_read_pm);
 
 446  * ps3_write_pm - Write Other LPM control registers.
 
 449 void ps3_write_pm(u32 cpu, enum pm_reg_name reg, u32 val)
 
 456                 if (val != lpm_priv->shadow.group_control)
 
 457                         result = lv1_set_lpm_group_control(lpm_priv->lpm_id,
 
 461                 lpm_priv->shadow.group_control = val;
 
 463         case debug_bus_control:
 
 464                 if (val != lpm_priv->shadow.debug_bus_control)
 
 465                         result = lv1_set_lpm_debug_bus_control(lpm_priv->lpm_id,
 
 469                 lpm_priv->shadow.debug_bus_control = val;
 
 472                 if (use_start_stop_bookmark)
 
 473                         val |= (PS3_PM_CONTROL_PPU_TH0_BOOKMARK |
 
 474                                 PS3_PM_CONTROL_PPU_TH1_BOOKMARK);
 
 475                 if (val != lpm_priv->shadow.pm_control)
 
 476                         result = lv1_set_lpm_general_control(lpm_priv->lpm_id,
 
 481                 lpm_priv->shadow.pm_control = val;
 
 484                 result = lv1_set_lpm_interval(lpm_priv->lpm_id, val,
 
 485                                               PS3_WRITE_PM_MASK, &dummy);
 
 488                 if (val != lpm_priv->shadow.pm_start_stop)
 
 489                         result = lv1_set_lpm_trigger_control(lpm_priv->lpm_id,
 
 493                 lpm_priv->shadow.pm_start_stop = val;
 
 500                 dev_dbg(sbd_core(), "%s:%u: unknown reg: %d\n", __func__,
 
 507                 dev_err(sbd_core(), "%s:%u: lv1 set_control failed: "
 
 508                         "reg %u, %s\n", __func__, __LINE__, reg,
 
 511 EXPORT_SYMBOL_GPL(ps3_write_pm);
 
 514  * ps3_get_ctr_size - Get the size of a physical counter.
 
 516  * Returns either 16 or 32.
 
 519 u32 ps3_get_ctr_size(u32 cpu, u32 phys_ctr)
 
 523         if (phys_ctr >= NR_PHYS_CTRS) {
 
 524                 dev_dbg(sbd_core(), "%s:%u: phys_ctr too big: %u\n", __func__,
 
 529         pm_ctrl = ps3_read_pm(cpu, pm_control);
 
 530         return (pm_ctrl & CBE_PM_16BIT_CTR(phys_ctr)) ? 16 : 32;
 
 532 EXPORT_SYMBOL_GPL(ps3_get_ctr_size);
 
 535  * ps3_set_ctr_size - Set the size of a physical counter to 16 or 32 bits.
 
 538 void ps3_set_ctr_size(u32 cpu, u32 phys_ctr, u32 ctr_size)
 
 542         if (phys_ctr >= NR_PHYS_CTRS) {
 
 543                 dev_dbg(sbd_core(), "%s:%u: phys_ctr too big: %u\n", __func__,
 
 548         pm_ctrl = ps3_read_pm(cpu, pm_control);
 
 552                 pm_ctrl |= CBE_PM_16BIT_CTR(phys_ctr);
 
 553                 ps3_write_pm(cpu, pm_control, pm_ctrl);
 
 557                 pm_ctrl &= ~CBE_PM_16BIT_CTR(phys_ctr);
 
 558                 ps3_write_pm(cpu, pm_control, pm_ctrl);
 
 564 EXPORT_SYMBOL_GPL(ps3_set_ctr_size);
 
 566 static u64 pm_translate_signal_group_number_on_island2(u64 subgroup)
 
 573                 return PM_ISLAND2_BASE_SIGNAL_GROUP_NUMBER + subgroup;
 
 574         else if (subgroup == 7)
 
 575                 return PM_ISLAND2_SIGNAL_GROUP_NUMBER1;
 
 577                 return PM_ISLAND2_SIGNAL_GROUP_NUMBER2;
 
 580 static u64 pm_translate_signal_group_number_on_island3(u64 subgroup)
 
 595         return PM_ISLAND3_BASE_SIGNAL_GROUP_NUMBER + subgroup;
 
 598 static u64 pm_translate_signal_group_number_on_island4(u64 subgroup)
 
 600         return PM_ISLAND4_BASE_SIGNAL_GROUP_NUMBER + subgroup;
 
 603 static u64 pm_translate_signal_group_number_on_island5(u64 subgroup)
 
 616         return PM_ISLAND5_BASE_SIGNAL_GROUP_NUMBER + subgroup;
 
 619 static u64 pm_translate_signal_group_number_on_island6(u64 subgroup,
 
 632         switch (subsubgroup) {
 
 654                 return (PM_ISLAND6_BASE_SIGNAL_GROUP_NUMBER + subgroup);
 
 656                 return (PM_ISLAND6_BASE_SIGNAL_GROUP_NUMBER + subgroup
 
 660 static u64 pm_translate_signal_group_number_on_island7(u64 subgroup)
 
 662         return PM_ISLAND7_BASE_SIGNAL_GROUP_NUMBER + subgroup;
 
 665 static u64 pm_translate_signal_group_number_on_island8(u64 subgroup)
 
 667         return PM_ISLAND8_BASE_SIGNAL_GROUP_NUMBER + subgroup;
 
 670 static u64 pm_signal_group_to_ps3_lv1_signal_group(u64 group)
 
 681                         if (20 <= group && group < 30) {
 
 683                                 subgroup = group - 20;
 
 684                         } else if (30 <= group && group < 40) {
 
 686                                 subgroup = group - 30;
 
 687                         } else if (40 <= group && group < 50) {
 
 689                                 subgroup = group - 40;
 
 690                         } else if (50 <= group && group < 60) {
 
 692                                 subgroup = group - 50;
 
 693                         } else if (60 <= group && group < 70) {
 
 695                                 subgroup = group - 60;
 
 696                         } else if (70 <= group && group < 80) {
 
 698                                 subgroup = group - 70;
 
 699                         } else if (80 <= group && group < 90) {
 
 701                                 subgroup = group - 80;
 
 703                 } else if (200 <= group && group < 300) {
 
 705                         subgroup = group - 200;
 
 706                 } else if (600 <= group && group < 700) {
 
 709                         subsubgroup = group - 650;
 
 711         } else if (6000 <= group && group < 7000) {
 
 714                 subsubgroup = group - 6500;
 
 719                 return pm_translate_signal_group_number_on_island2(subgroup);
 
 721                 return pm_translate_signal_group_number_on_island3(subgroup);
 
 723                 return pm_translate_signal_group_number_on_island4(subgroup);
 
 725                 return pm_translate_signal_group_number_on_island5(subgroup);
 
 727                 return pm_translate_signal_group_number_on_island6(subgroup,
 
 730                 return pm_translate_signal_group_number_on_island7(subgroup);
 
 732                 return pm_translate_signal_group_number_on_island8(subgroup);
 
 734                 dev_dbg(sbd_core(), "%s:%u: island not found: %lu\n", __func__,
 
 742 static u64 pm_bus_word_to_ps3_lv1_bus_word(u8 word)
 
 758 static int __ps3_set_signal(u64 lv1_signal_group, u64 bus_select,
 
 759                             u64 signal_select, u64 attr1, u64 attr2, u64 attr3)
 
 763         ret = lv1_set_lpm_signal(lpm_priv->lpm_id, lv1_signal_group, bus_select,
 
 764                                  signal_select, attr1, attr2, attr3);
 
 767                         "%s:%u: error:%d 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
 
 768                         __func__, __LINE__, ret, lv1_signal_group, bus_select,
 
 769                         signal_select, attr1, attr2, attr3);
 
 774 int ps3_set_signal(u64 signal_group, u8 signal_bit, u16 sub_unit,
 
 778         u64 lv1_signal_group;
 
 781         u64 attr1, attr2, attr3;
 
 783         if (signal_group == 0)
 
 784                 return __ps3_set_signal(0, 0, 0, 0, 0, 0);
 
 787                 pm_signal_group_to_ps3_lv1_signal_group(signal_group);
 
 788         bus_select = pm_bus_word_to_ps3_lv1_bus_word(bus_word);
 
 790         switch (signal_group) {
 
 791         case PM_SIG_GROUP_SPU_TRIGGER:
 
 793                 signal_select = signal_select << (63 - signal_bit);
 
 795         case PM_SIG_GROUP_SPU_EVENT:
 
 797                 signal_select = (signal_select << (63 - signal_bit)) | 0x3;
 
 805          * 0: physical object.
 
 807          * This parameter is only used for the PPE and SPE signals.
 
 812          * This parameter is used to specify the target physical/logical
 
 815         if (PM_SIG_GROUP_SPU <= signal_group &&
 
 816                 signal_group < PM_SIG_GROUP_MFC_MAX)
 
 819                 attr2 = lpm_priv->pu_id;
 
 822          * This parameter is only used for setting the SPE signal.
 
 826         ret = __ps3_set_signal(lv1_signal_group, bus_select, signal_select,
 
 827                                attr1, attr2, attr3);
 
 829                 dev_err(sbd_core(), "%s:%u: __ps3_set_signal failed: %d\n",
 
 830                         __func__, __LINE__, ret);
 
 834 EXPORT_SYMBOL_GPL(ps3_set_signal);
 
 836 u32 ps3_get_hw_thread_id(int cpu)
 
 838         return get_hard_smp_processor_id(cpu);
 
 840 EXPORT_SYMBOL_GPL(ps3_get_hw_thread_id);
 
 843  * ps3_enable_pm - Enable the entire performance monitoring unit.
 
 845  * When we enable the LPM, all pending writes to counters get committed.
 
 848 void ps3_enable_pm(u32 cpu)
 
 852         int insert_bookmark = 0;
 
 854         lpm_priv->tb_count = 0;
 
 856         if (use_start_stop_bookmark) {
 
 857                 if (!(lpm_priv->shadow.pm_start_stop &
 
 858                         (PS3_PM_START_STOP_START_MASK
 
 859                         | PS3_PM_START_STOP_STOP_MASK))) {
 
 860                         result = lv1_set_lpm_trigger_control(lpm_priv->lpm_id,
 
 861                                 (PS3_PM_START_STOP_PPU_TH0_BOOKMARK_START |
 
 862                                 PS3_PM_START_STOP_PPU_TH1_BOOKMARK_START |
 
 863                                 PS3_PM_START_STOP_PPU_TH0_BOOKMARK_STOP |
 
 864                                 PS3_PM_START_STOP_PPU_TH1_BOOKMARK_STOP),
 
 865                                 0xFFFFFFFFFFFFFFFFULL, &tmp);
 
 868                                 dev_err(sbd_core(), "%s:%u: "
 
 869                                         "lv1_set_lpm_trigger_control failed: "
 
 870                                         "%s\n", __func__, __LINE__,
 
 873                         insert_bookmark = !result;
 
 877         result = lv1_start_lpm(lpm_priv->lpm_id);
 
 880                 dev_err(sbd_core(), "%s:%u: lv1_start_lpm failed: %s\n",
 
 881                         __func__, __LINE__, ps3_result(result));
 
 883         if (use_start_stop_bookmark && !result && insert_bookmark)
 
 884                 ps3_set_bookmark(get_tb() | PS3_PM_BOOKMARK_START);
 
 886 EXPORT_SYMBOL_GPL(ps3_enable_pm);
 
 889  * ps3_disable_pm - Disable the entire performance monitoring unit.
 
 892 void ps3_disable_pm(u32 cpu)
 
 897         ps3_set_bookmark(get_tb() | PS3_PM_BOOKMARK_STOP);
 
 899         result = lv1_stop_lpm(lpm_priv->lpm_id, &tmp);
 
 902                 if(result != LV1_WRONG_STATE)
 
 903                         dev_err(sbd_core(), "%s:%u: lv1_stop_lpm failed: %s\n",
 
 904                                 __func__, __LINE__, ps3_result(result));
 
 908         lpm_priv->tb_count = tmp;
 
 910         dev_dbg(sbd_core(), "%s:%u: tb_count %lu (%lxh)\n", __func__, __LINE__,
 
 911                 lpm_priv->tb_count, lpm_priv->tb_count);
 
 913 EXPORT_SYMBOL_GPL(ps3_disable_pm);
 
 916  * ps3_lpm_copy_tb - Copy data from the trace buffer to a kernel buffer.
 
 917  * @offset: Offset in bytes from the start of the trace buffer.
 
 918  * @buf: Copy destination.
 
 919  * @count: Maximum count of bytes to copy.
 
 920  * @bytes_copied: Pointer to a variable that will recieve the number of
 
 921  *  bytes copied to @buf.
 
 923  * On error @buf will contain any successfully copied trace buffer data
 
 924  * and bytes_copied will be set to the number of bytes successfully copied.
 
 927 int ps3_lpm_copy_tb(unsigned long offset, void *buf, unsigned long count,
 
 928                     unsigned long *bytes_copied)
 
 934         if (!lpm_priv->tb_cache)
 
 937         if (offset >= lpm_priv->tb_count)
 
 940         count = min(count, lpm_priv->tb_count - offset);
 
 942         while (*bytes_copied < count) {
 
 943                 const unsigned long request = count - *bytes_copied;
 
 946                 result = lv1_copy_lpm_trace_buffer(lpm_priv->lpm_id, offset,
 
 949                         dev_dbg(sbd_core(), "%s:%u: 0x%lx bytes at 0x%lx\n",
 
 950                                 __func__, __LINE__, request, offset);
 
 952                         dev_err(sbd_core(), "%s:%u: lv1_copy_lpm_trace_buffer "
 
 953                                 "failed: %s\n", __func__, __LINE__,
 
 955                         return result == LV1_WRONG_STATE ? -EBUSY : -EINVAL;
 
 958                 memcpy(buf, lpm_priv->tb_cache, tmp);
 
 960                 *bytes_copied += tmp;
 
 963         dev_dbg(sbd_core(), "%s:%u: copied %lxh bytes\n", __func__, __LINE__,
 
 968 EXPORT_SYMBOL_GPL(ps3_lpm_copy_tb);
 
 971  * ps3_lpm_copy_tb_to_user - Copy data from the trace buffer to a user buffer.
 
 972  * @offset: Offset in bytes from the start of the trace buffer.
 
 973  * @buf: A __user copy destination.
 
 974  * @count: Maximum count of bytes to copy.
 
 975  * @bytes_copied: Pointer to a variable that will recieve the number of
 
 976  *  bytes copied to @buf.
 
 978  * On error @buf will contain any successfully copied trace buffer data
 
 979  * and bytes_copied will be set to the number of bytes successfully copied.
 
 982 int ps3_lpm_copy_tb_to_user(unsigned long offset, void __user *buf,
 
 983                             unsigned long count, unsigned long *bytes_copied)
 
 989         if (!lpm_priv->tb_cache)
 
 992         if (offset >= lpm_priv->tb_count)
 
 995         count = min(count, lpm_priv->tb_count - offset);
 
 997         while (*bytes_copied < count) {
 
 998                 const unsigned long request = count - *bytes_copied;
 
1001                 result = lv1_copy_lpm_trace_buffer(lpm_priv->lpm_id, offset,
 
1004                         dev_dbg(sbd_core(), "%s:%u: 0x%lx bytes at 0x%lx\n",
 
1005                                 __func__, __LINE__, request, offset);
 
1006                         dev_err(sbd_core(), "%s:%u: lv1_copy_lpm_trace_buffer "
 
1007                                 "failed: %s\n", __func__, __LINE__,
 
1008                                 ps3_result(result));
 
1009                         return result == LV1_WRONG_STATE ? -EBUSY : -EINVAL;
 
1012                 result = copy_to_user(buf, lpm_priv->tb_cache, tmp);
 
1015                         dev_dbg(sbd_core(), "%s:%u: 0x%lx bytes at 0x%p\n",
 
1016                                 __func__, __LINE__, tmp, buf);
 
1017                         dev_err(sbd_core(), "%s:%u: copy_to_user failed: %d\n",
 
1018                                 __func__, __LINE__, result);
 
1023                 *bytes_copied += tmp;
 
1026         dev_dbg(sbd_core(), "%s:%u: copied %lxh bytes\n", __func__, __LINE__,
 
1031 EXPORT_SYMBOL_GPL(ps3_lpm_copy_tb_to_user);
 
1034  * ps3_get_and_clear_pm_interrupts -
 
1036  * Clearing interrupts for the entire performance monitoring unit.
 
1037  * Reading pm_status clears the interrupt bits.
 
1040 u32 ps3_get_and_clear_pm_interrupts(u32 cpu)
 
1042         return ps3_read_pm(cpu, pm_status);
 
1044 EXPORT_SYMBOL_GPL(ps3_get_and_clear_pm_interrupts);
 
1047  * ps3_enable_pm_interrupts -
 
1049  * Enabling interrupts for the entire performance monitoring unit.
 
1050  * Enables the interrupt bits in the pm_status register.
 
1053 void ps3_enable_pm_interrupts(u32 cpu, u32 thread, u32 mask)
 
1056                 ps3_write_pm(cpu, pm_status, mask);
 
1058 EXPORT_SYMBOL_GPL(ps3_enable_pm_interrupts);
 
1061  * ps3_enable_pm_interrupts -
 
1063  * Disabling interrupts for the entire performance monitoring unit.
 
1066 void ps3_disable_pm_interrupts(u32 cpu)
 
1068         ps3_get_and_clear_pm_interrupts(cpu);
 
1069         ps3_write_pm(cpu, pm_status, 0);
 
1071 EXPORT_SYMBOL_GPL(ps3_disable_pm_interrupts);
 
1074  * ps3_lpm_open - Open the logical performance monitor device.
 
1075  * @tb_type: Specifies the type of trace buffer lv1 sould use for this lpm
 
1076  *  instance, specified by one of enum ps3_lpm_tb_type.
 
1077  * @tb_cache: Optional user supplied buffer to use as the trace buffer cache.
 
1078  *  If NULL, the driver will allocate and manage an internal buffer.
 
1079  *  Unused when when @tb_type is PS3_LPM_TB_TYPE_NONE.
 
1080  * @tb_cache_size: The size in bytes of the user supplied @tb_cache buffer.
 
1081  *  Unused when @tb_cache is NULL or @tb_type is PS3_LPM_TB_TYPE_NONE.
 
1084 int ps3_lpm_open(enum ps3_lpm_tb_type tb_type, void *tb_cache,
 
1091         BUG_ON(tb_type != PS3_LPM_TB_TYPE_NONE
 
1092                 && tb_type != PS3_LPM_TB_TYPE_INTERNAL);
 
1094         if (tb_type == PS3_LPM_TB_TYPE_NONE && tb_cache)
 
1095                 dev_dbg(sbd_core(), "%s:%u: bad in vals\n", __func__, __LINE__);
 
1097         if (!atomic_add_unless(&lpm_priv->open, 1, 1)) {
 
1098                 dev_dbg(sbd_core(), "%s:%u: busy\n", __func__, __LINE__);
 
1102         /* Note tb_cache needs 128 byte alignment. */
 
1104         if (tb_type == PS3_LPM_TB_TYPE_NONE) {
 
1105                 lpm_priv->tb_cache_size = 0;
 
1106                 lpm_priv->tb_cache_internal = NULL;
 
1107                 lpm_priv->tb_cache = NULL;
 
1108         } else if (tb_cache) {
 
1109                 if (tb_cache != (void *)_ALIGN_UP((unsigned long)tb_cache, 128)
 
1110                         || tb_cache_size != _ALIGN_UP(tb_cache_size, 128)) {
 
1111                         dev_err(sbd_core(), "%s:%u: unaligned tb_cache\n",
 
1112                                 __func__, __LINE__);
 
1116                 lpm_priv->tb_cache_size = tb_cache_size;
 
1117                 lpm_priv->tb_cache_internal = NULL;
 
1118                 lpm_priv->tb_cache = tb_cache;
 
1120                 lpm_priv->tb_cache_size = PS3_LPM_DEFAULT_TB_CACHE_SIZE;
 
1121                 lpm_priv->tb_cache_internal = kzalloc(
 
1122                         lpm_priv->tb_cache_size + 127, GFP_KERNEL);
 
1123                 if (!lpm_priv->tb_cache_internal) {
 
1124                         dev_err(sbd_core(), "%s:%u: alloc internal tb_cache "
 
1125                                 "failed\n", __func__, __LINE__);
 
1129                 lpm_priv->tb_cache = (void *)_ALIGN_UP(
 
1130                         (unsigned long)lpm_priv->tb_cache_internal, 128);
 
1133         result = lv1_construct_lpm(lpm_priv->node_id, tb_type, 0, 0,
 
1134                                 ps3_mm_phys_to_lpar(__pa(lpm_priv->tb_cache)),
 
1135                                 lpm_priv->tb_cache_size, &lpm_priv->lpm_id,
 
1136                                 &lpm_priv->outlet_id, &tb_size);
 
1139                 dev_err(sbd_core(), "%s:%u: lv1_construct_lpm failed: %s\n",
 
1140                         __func__, __LINE__, ps3_result(result));
 
1142                 goto fail_construct;
 
1145         lpm_priv->shadow.pm_control = PS3_LPM_SHADOW_REG_INIT;
 
1146         lpm_priv->shadow.pm_start_stop = PS3_LPM_SHADOW_REG_INIT;
 
1147         lpm_priv->shadow.group_control = PS3_LPM_SHADOW_REG_INIT;
 
1148         lpm_priv->shadow.debug_bus_control = PS3_LPM_SHADOW_REG_INIT;
 
1150         dev_dbg(sbd_core(), "%s:%u: lpm_id 0x%lx, outlet_id 0x%lx, "
 
1151                 "tb_size 0x%lx\n", __func__, __LINE__, lpm_priv->lpm_id,
 
1152                 lpm_priv->outlet_id, tb_size);
 
1157         kfree(lpm_priv->tb_cache_internal);
 
1158         lpm_priv->tb_cache_internal = NULL;
 
1161         atomic_dec(&lpm_priv->open);
 
1164 EXPORT_SYMBOL_GPL(ps3_lpm_open);
 
1167  * ps3_lpm_close - Close the lpm device.
 
1171 int ps3_lpm_close(void)
 
1173         dev_dbg(sbd_core(), "%s:%u\n", __func__, __LINE__);
 
1175         lv1_destruct_lpm(lpm_priv->lpm_id);
 
1176         lpm_priv->lpm_id = 0;
 
1178         kfree(lpm_priv->tb_cache_internal);
 
1179         lpm_priv->tb_cache_internal = NULL;
 
1181         atomic_dec(&lpm_priv->open);
 
1184 EXPORT_SYMBOL_GPL(ps3_lpm_close);
 
1186 static int __devinit ps3_lpm_probe(struct ps3_system_bus_device *dev)
 
1188         dev_dbg(&dev->core, " -> %s:%u\n", __func__, __LINE__);
 
1191                 dev_info(&dev->core, "%s:%u: called twice\n",
 
1192                         __func__, __LINE__);
 
1196         lpm_priv = kzalloc(sizeof(*lpm_priv), GFP_KERNEL);
 
1201         lpm_priv->sbd = dev;
 
1202         lpm_priv->node_id = dev->lpm.node_id;
 
1203         lpm_priv->pu_id = dev->lpm.pu_id;
 
1204         lpm_priv->rights = dev->lpm.rights;
 
1206         dev_info(&dev->core, " <- %s:%u:\n", __func__, __LINE__);
 
1211 static int ps3_lpm_remove(struct ps3_system_bus_device *dev)
 
1213         dev_dbg(&dev->core, " -> %s:%u:\n", __func__, __LINE__);
 
1220         dev_info(&dev->core, " <- %s:%u:\n", __func__, __LINE__);
 
1224 static struct ps3_system_bus_driver ps3_lpm_driver = {
 
1225         .match_id = PS3_MATCH_ID_LPM,
 
1226         .core.name      = "ps3-lpm",
 
1227         .core.owner     = THIS_MODULE,
 
1228         .probe          = ps3_lpm_probe,
 
1229         .remove         = ps3_lpm_remove,
 
1230         .shutdown       = ps3_lpm_remove,
 
1233 static int __init ps3_lpm_init(void)
 
1235         pr_debug("%s:%d:\n", __func__, __LINE__);
 
1236         return ps3_system_bus_driver_register(&ps3_lpm_driver);
 
1239 static void __exit ps3_lpm_exit(void)
 
1241         pr_debug("%s:%d:\n", __func__, __LINE__);
 
1242         ps3_system_bus_driver_unregister(&ps3_lpm_driver);
 
1245 module_init(ps3_lpm_init);
 
1246 module_exit(ps3_lpm_exit);
 
1248 MODULE_LICENSE("GPL v2");
 
1249 MODULE_DESCRIPTION("PS3 Logical Performance Monitor Driver");
 
1250 MODULE_AUTHOR("Sony Corporation");
 
1251 MODULE_ALIAS(PS3_MODULE_ALIAS_LPM);