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>
26 #include <asm/lv1call.h>
27 #include <asm/cell-pmu.h>
30 /* BOOKMARK tag macros */
31 #define PS3_PM_BOOKMARK_START 0x8000000000000000ULL
32 #define PS3_PM_BOOKMARK_STOP 0x4000000000000000ULL
33 #define PS3_PM_BOOKMARK_TAG_KERNEL 0x1000000000000000ULL
34 #define PS3_PM_BOOKMARK_TAG_USER 0x3000000000000000ULL
35 #define PS3_PM_BOOKMARK_TAG_MASK_HI 0xF000000000000000ULL
36 #define PS3_PM_BOOKMARK_TAG_MASK_LO 0x0F00000000000000ULL
38 /* CBE PM CONTROL register macros */
39 #define PS3_PM_CONTROL_PPU_TH0_BOOKMARK 0x00001000
40 #define PS3_PM_CONTROL_PPU_TH1_BOOKMARK 0x00000800
41 #define PS3_PM_CONTROL_PPU_COUNT_MODE_MASK 0x000C0000
42 #define PS3_PM_CONTROL_PPU_COUNT_MODE_PROBLEM 0x00080000
43 #define PS3_WRITE_PM_MASK 0xFFFFFFFFFFFFFFFFULL
45 /* CBE PM START STOP register macros */
46 #define PS3_PM_START_STOP_PPU_TH0_BOOKMARK_START 0x02000000
47 #define PS3_PM_START_STOP_PPU_TH1_BOOKMARK_START 0x01000000
48 #define PS3_PM_START_STOP_PPU_TH0_BOOKMARK_STOP 0x00020000
49 #define PS3_PM_START_STOP_PPU_TH1_BOOKMARK_STOP 0x00010000
50 #define PS3_PM_START_STOP_START_MASK 0xFF000000
51 #define PS3_PM_START_STOP_STOP_MASK 0x00FF0000
53 /* CBE PM COUNTER register macres */
54 #define PS3_PM_COUNTER_MASK_HI 0xFFFFFFFF00000000ULL
55 #define PS3_PM_COUNTER_MASK_LO 0x00000000FFFFFFFFULL
57 /* BASE SIGNAL GROUP NUMBER macros */
58 #define PM_ISLAND2_BASE_SIGNAL_GROUP_NUMBER 0
59 #define PM_ISLAND2_SIGNAL_GROUP_NUMBER1 6
60 #define PM_ISLAND2_SIGNAL_GROUP_NUMBER2 7
61 #define PM_ISLAND3_BASE_SIGNAL_GROUP_NUMBER 7
62 #define PM_ISLAND4_BASE_SIGNAL_GROUP_NUMBER 15
63 #define PM_SPU_TRIGGER_SIGNAL_GROUP_NUMBER 17
64 #define PM_SPU_EVENT_SIGNAL_GROUP_NUMBER 18
65 #define PM_ISLAND5_BASE_SIGNAL_GROUP_NUMBER 18
66 #define PM_ISLAND6_BASE_SIGNAL_GROUP_NUMBER 24
67 #define PM_ISLAND7_BASE_SIGNAL_GROUP_NUMBER 49
68 #define PM_ISLAND8_BASE_SIGNAL_GROUP_NUMBER 52
69 #define PM_SIG_GROUP_SPU 41
70 #define PM_SIG_GROUP_SPU_TRIGGER 42
71 #define PM_SIG_GROUP_SPU_EVENT 43
72 #define PM_SIG_GROUP_MFC_MAX 60
75 * struct ps3_lpm_shadow_regs - Performance monitor shadow registers.
77 * @pm_control: Shadow of the processor's pm_control register.
78 * @pm_start_stop: Shadow of the processor's pm_start_stop register.
79 * @group_control: Shadow of the processor's group_control register.
80 * @debug_bus_control: Shadow of the processor's debug_bus_control register.
82 * The logical performance monitor provides a write-only interface to
83 * these processor registers. These shadow variables cache the processor
84 * register values for reading.
86 * The initial value of the shadow registers at lpm creation is
87 * PS3_LPM_SHADOW_REG_INIT.
90 struct ps3_lpm_shadow_regs
{
94 u64 debug_bus_control
;
97 #define PS3_LPM_SHADOW_REG_INIT 0xFFFFFFFF00000000ULL
100 * struct ps3_lpm_priv - Private lpm device data.
102 * @open: An atomic variable indicating the lpm driver has been opened.
103 * @rights: The lpm rigths granted by the system policy module. A logical
104 * OR of enum ps3_lpm_rights.
105 * @node_id: The node id of a BE prosessor whose performance monitor this
106 * lpar has the right to use.
107 * @pu_id: The lv1 id of the logical PU.
108 * @lpm_id: The lv1 id of this lpm instance.
109 * @outlet_id: The outlet created by lv1 for this lpm instance.
110 * @tb_count: The number of bytes of data held in the lv1 trace buffer.
111 * @tb_cache: Kernel buffer to receive the data from the lv1 trace buffer.
112 * Must be 128 byte aligned.
113 * @tb_cache_size: Size of the kernel @tb_cache buffer. Must be 128 byte
115 * @tb_cache_internal: An unaligned buffer allocated by this driver to be
116 * used for the trace buffer cache when ps3_lpm_open() is called with a
117 * NULL tb_cache argument. Otherwise unused.
118 * @shadow: Processor register shadow of type struct ps3_lpm_shadow_regs.
119 * @sbd: The struct ps3_system_bus_device attached to this driver.
121 * The trace buffer is a buffer allocated and used internally to the lv1
122 * hypervisor to collect trace data. The trace buffer cache is a guest
123 * buffer that accepts the trace data from the trace buffer.
126 struct ps3_lpm_priv
{
136 void *tb_cache_internal
;
137 struct ps3_lpm_shadow_regs shadow
;
138 struct ps3_system_bus_device
*sbd
;
142 PS3_LPM_DEFAULT_TB_CACHE_SIZE
= 0x4000,
146 * lpm_priv - Static instance of the lpm data.
148 * Since the exported routines don't support the notion of a device
149 * instance we need to hold the instance in this static variable
150 * and then only allow at most one instance at a time to be created.
153 static struct ps3_lpm_priv
*lpm_priv
;
155 static struct device
*sbd_core(void)
157 BUG_ON(!lpm_priv
|| !lpm_priv
->sbd
);
158 return &lpm_priv
->sbd
->core
;
162 * use_start_stop_bookmark - Enable the PPU bookmark trace.
164 * And it enables PPU bookmark triggers ONLY if the other triggers are not set.
165 * The start/stop bookmarks are inserted at ps3_enable_pm() and ps3_disable_pm()
168 * Used to get good quality of the performance counter.
171 enum {use_start_stop_bookmark
= 1,};
173 void ps3_set_bookmark(u64 bookmark
)
176 * As per the PPE book IV, to avoid bookmark loss there must
177 * not be a traced branch within 10 cycles of setting the
178 * SPRN_BKMK register. The actual text is unclear if 'within'
179 * includes cycles before the call.
182 asm volatile("nop;nop;nop;nop;nop;nop;nop;nop;nop;");
183 mtspr(SPRN_BKMK
, bookmark
);
184 asm volatile("nop;nop;nop;nop;nop;nop;nop;nop;nop;");
186 EXPORT_SYMBOL_GPL(ps3_set_bookmark
);
188 void ps3_set_pm_bookmark(u64 tag
, u64 incident
, u64 th_id
)
192 bookmark
= (get_tb() & 0x00000000FFFFFFFFULL
) |
193 PS3_PM_BOOKMARK_TAG_KERNEL
;
194 bookmark
= ((tag
<< 56) & PS3_PM_BOOKMARK_TAG_MASK_LO
) |
195 (incident
<< 48) | (th_id
<< 32) | bookmark
;
196 ps3_set_bookmark(bookmark
);
198 EXPORT_SYMBOL_GPL(ps3_set_pm_bookmark
);
201 * ps3_read_phys_ctr - Read physical counter registers.
203 * Each physical counter can act as one 32 bit counter or as two 16 bit
207 u32
ps3_read_phys_ctr(u32 cpu
, u32 phys_ctr
)
213 if (phys_ctr
>= NR_PHYS_CTRS
) {
214 dev_dbg(sbd_core(), "%s:%u: phys_ctr too big: %u\n", __func__
,
219 result
= lv1_set_lpm_counter(lpm_priv
->lpm_id
, 0, 0, 0, 0, &counter0415
,
222 dev_err(sbd_core(), "%s:%u: lv1_set_lpm_counter failed: "
223 "phys_ctr %u, %s\n", __func__
, __LINE__
, phys_ctr
,
230 return counter0415
>> 32;
232 return counter0415
& PS3_PM_COUNTER_MASK_LO
;
234 return counter2637
>> 32;
236 return counter2637
& PS3_PM_COUNTER_MASK_LO
;
242 EXPORT_SYMBOL_GPL(ps3_read_phys_ctr
);
245 * ps3_write_phys_ctr - Write physical counter registers.
247 * Each physical counter can act as one 32 bit counter or as two 16 bit
251 void ps3_write_phys_ctr(u32 cpu
, u32 phys_ctr
, u32 val
)
254 u64 counter0415_mask
;
256 u64 counter2637_mask
;
259 if (phys_ctr
>= NR_PHYS_CTRS
) {
260 dev_dbg(sbd_core(), "%s:%u: phys_ctr too big: %u\n", __func__
,
267 counter0415
= (u64
)val
<< 32;
268 counter0415_mask
= PS3_PM_COUNTER_MASK_HI
;
270 counter2637_mask
= 0x0;
273 counter0415
= (u64
)val
;
274 counter0415_mask
= PS3_PM_COUNTER_MASK_LO
;
276 counter2637_mask
= 0x0;
280 counter0415_mask
= 0x0;
281 counter2637
= (u64
)val
<< 32;
282 counter2637_mask
= PS3_PM_COUNTER_MASK_HI
;
286 counter0415_mask
= 0x0;
287 counter2637
= (u64
)val
;
288 counter2637_mask
= PS3_PM_COUNTER_MASK_LO
;
294 result
= lv1_set_lpm_counter(lpm_priv
->lpm_id
,
295 counter0415
, counter0415_mask
,
296 counter2637
, counter2637_mask
,
297 &counter0415
, &counter2637
);
299 dev_err(sbd_core(), "%s:%u: lv1_set_lpm_counter failed: "
300 "phys_ctr %u, val %u, %s\n", __func__
, __LINE__
,
301 phys_ctr
, val
, ps3_result(result
));
303 EXPORT_SYMBOL_GPL(ps3_write_phys_ctr
);
306 * ps3_read_ctr - Read counter.
308 * Read 16 or 32 bits depending on the current size of the counter.
309 * Counters 4, 5, 6 & 7 are always 16 bit.
312 u32
ps3_read_ctr(u32 cpu
, u32 ctr
)
315 u32 phys_ctr
= ctr
& (NR_PHYS_CTRS
- 1);
317 val
= ps3_read_phys_ctr(cpu
, phys_ctr
);
319 if (ps3_get_ctr_size(cpu
, phys_ctr
) == 16)
320 val
= (ctr
< NR_PHYS_CTRS
) ? (val
>> 16) : (val
& 0xffff);
324 EXPORT_SYMBOL_GPL(ps3_read_ctr
);
327 * ps3_write_ctr - Write counter.
329 * Write 16 or 32 bits depending on the current size of the counter.
330 * Counters 4, 5, 6 & 7 are always 16 bit.
333 void ps3_write_ctr(u32 cpu
, u32 ctr
, u32 val
)
338 phys_ctr
= ctr
& (NR_PHYS_CTRS
- 1);
340 if (ps3_get_ctr_size(cpu
, phys_ctr
) == 16) {
341 phys_val
= ps3_read_phys_ctr(cpu
, phys_ctr
);
343 if (ctr
< NR_PHYS_CTRS
)
344 val
= (val
<< 16) | (phys_val
& 0xffff);
346 val
= (val
& 0xffff) | (phys_val
& 0xffff0000);
349 ps3_write_phys_ctr(cpu
, phys_ctr
, val
);
351 EXPORT_SYMBOL_GPL(ps3_write_ctr
);
354 * ps3_read_pm07_control - Read counter control registers.
356 * Each logical counter has a corresponding control register.
359 u32
ps3_read_pm07_control(u32 cpu
, u32 ctr
)
363 EXPORT_SYMBOL_GPL(ps3_read_pm07_control
);
366 * ps3_write_pm07_control - Write counter control registers.
368 * Each logical counter has a corresponding control register.
371 void ps3_write_pm07_control(u32 cpu
, u32 ctr
, u32 val
)
374 static const u64 mask
= 0xFFFFFFFFFFFFFFFFULL
;
377 if (ctr
>= NR_CTRS
) {
378 dev_dbg(sbd_core(), "%s:%u: ctr too big: %u\n", __func__
,
383 result
= lv1_set_lpm_counter_control(lpm_priv
->lpm_id
, ctr
, val
, mask
,
386 dev_err(sbd_core(), "%s:%u: lv1_set_lpm_counter_control "
387 "failed: ctr %u, %s\n", __func__
, __LINE__
, ctr
,
390 EXPORT_SYMBOL_GPL(ps3_write_pm07_control
);
393 * ps3_read_pm - Read Other LPM control registers.
396 u32
ps3_read_pm(u32 cpu
, enum pm_reg_name reg
)
403 return lpm_priv
->shadow
.pm_control
;
405 return CBE_PM_TRACE_BUF_EMPTY
;
407 return lpm_priv
->shadow
.pm_start_stop
;
409 result
= lv1_set_lpm_interval(lpm_priv
->lpm_id
, 0, 0, &val
);
412 dev_dbg(sbd_core(), "%s:%u: lv1 set_inteval failed: "
413 "reg %u, %s\n", __func__
, __LINE__
, reg
,
418 return lpm_priv
->shadow
.group_control
;
419 case debug_bus_control
:
420 return lpm_priv
->shadow
.debug_bus_control
;
422 result
= lv1_get_lpm_interrupt_status(lpm_priv
->lpm_id
,
426 dev_dbg(sbd_core(), "%s:%u: lv1 get_lpm_status failed: "
427 "reg %u, %s\n", __func__
, __LINE__
, reg
,
434 dev_dbg(sbd_core(), "%s:%u: unknown reg: %d\n", __func__
,
442 EXPORT_SYMBOL_GPL(ps3_read_pm
);
445 * ps3_write_pm - Write Other LPM control registers.
448 void ps3_write_pm(u32 cpu
, enum pm_reg_name reg
, u32 val
)
455 if (val
!= lpm_priv
->shadow
.group_control
)
456 result
= lv1_set_lpm_group_control(lpm_priv
->lpm_id
,
460 lpm_priv
->shadow
.group_control
= val
;
462 case debug_bus_control
:
463 if (val
!= lpm_priv
->shadow
.debug_bus_control
)
464 result
= lv1_set_lpm_debug_bus_control(lpm_priv
->lpm_id
,
468 lpm_priv
->shadow
.debug_bus_control
= val
;
471 if (use_start_stop_bookmark
)
472 val
|= (PS3_PM_CONTROL_PPU_TH0_BOOKMARK
|
473 PS3_PM_CONTROL_PPU_TH1_BOOKMARK
);
474 if (val
!= lpm_priv
->shadow
.pm_control
)
475 result
= lv1_set_lpm_general_control(lpm_priv
->lpm_id
,
480 lpm_priv
->shadow
.pm_control
= val
;
483 result
= lv1_set_lpm_interval(lpm_priv
->lpm_id
, val
,
484 PS3_WRITE_PM_MASK
, &dummy
);
487 if (val
!= lpm_priv
->shadow
.pm_start_stop
)
488 result
= lv1_set_lpm_trigger_control(lpm_priv
->lpm_id
,
492 lpm_priv
->shadow
.pm_start_stop
= val
;
499 dev_dbg(sbd_core(), "%s:%u: unknown reg: %d\n", __func__
,
506 dev_err(sbd_core(), "%s:%u: lv1 set_control failed: "
507 "reg %u, %s\n", __func__
, __LINE__
, reg
,
510 EXPORT_SYMBOL_GPL(ps3_write_pm
);
513 * ps3_get_ctr_size - Get the size of a physical counter.
515 * Returns either 16 or 32.
518 u32
ps3_get_ctr_size(u32 cpu
, u32 phys_ctr
)
522 if (phys_ctr
>= NR_PHYS_CTRS
) {
523 dev_dbg(sbd_core(), "%s:%u: phys_ctr too big: %u\n", __func__
,
528 pm_ctrl
= ps3_read_pm(cpu
, pm_control
);
529 return (pm_ctrl
& CBE_PM_16BIT_CTR(phys_ctr
)) ? 16 : 32;
531 EXPORT_SYMBOL_GPL(ps3_get_ctr_size
);
534 * ps3_set_ctr_size - Set the size of a physical counter to 16 or 32 bits.
537 void ps3_set_ctr_size(u32 cpu
, u32 phys_ctr
, u32 ctr_size
)
541 if (phys_ctr
>= NR_PHYS_CTRS
) {
542 dev_dbg(sbd_core(), "%s:%u: phys_ctr too big: %u\n", __func__
,
547 pm_ctrl
= ps3_read_pm(cpu
, pm_control
);
551 pm_ctrl
|= CBE_PM_16BIT_CTR(phys_ctr
);
552 ps3_write_pm(cpu
, pm_control
, pm_ctrl
);
556 pm_ctrl
&= ~CBE_PM_16BIT_CTR(phys_ctr
);
557 ps3_write_pm(cpu
, pm_control
, pm_ctrl
);
563 EXPORT_SYMBOL_GPL(ps3_set_ctr_size
);
565 static u64
pm_translate_signal_group_number_on_island2(u64 subgroup
)
572 return PM_ISLAND2_BASE_SIGNAL_GROUP_NUMBER
+ subgroup
;
573 else if (subgroup
== 7)
574 return PM_ISLAND2_SIGNAL_GROUP_NUMBER1
;
576 return PM_ISLAND2_SIGNAL_GROUP_NUMBER2
;
579 static u64
pm_translate_signal_group_number_on_island3(u64 subgroup
)
594 return PM_ISLAND3_BASE_SIGNAL_GROUP_NUMBER
+ subgroup
;
597 static u64
pm_translate_signal_group_number_on_island4(u64 subgroup
)
599 return PM_ISLAND4_BASE_SIGNAL_GROUP_NUMBER
+ subgroup
;
602 static u64
pm_translate_signal_group_number_on_island5(u64 subgroup
)
615 return PM_ISLAND5_BASE_SIGNAL_GROUP_NUMBER
+ subgroup
;
618 static u64
pm_translate_signal_group_number_on_island6(u64 subgroup
,
631 switch (subsubgroup
) {
653 return (PM_ISLAND6_BASE_SIGNAL_GROUP_NUMBER
+ subgroup
);
655 return (PM_ISLAND6_BASE_SIGNAL_GROUP_NUMBER
+ subgroup
659 static u64
pm_translate_signal_group_number_on_island7(u64 subgroup
)
661 return PM_ISLAND7_BASE_SIGNAL_GROUP_NUMBER
+ subgroup
;
664 static u64
pm_translate_signal_group_number_on_island8(u64 subgroup
)
666 return PM_ISLAND8_BASE_SIGNAL_GROUP_NUMBER
+ subgroup
;
669 static u64
pm_signal_group_to_ps3_lv1_signal_group(u64 group
)
680 if (20 <= group
&& group
< 30) {
682 subgroup
= group
- 20;
683 } else if (30 <= group
&& group
< 40) {
685 subgroup
= group
- 30;
686 } else if (40 <= group
&& group
< 50) {
688 subgroup
= group
- 40;
689 } else if (50 <= group
&& group
< 60) {
691 subgroup
= group
- 50;
692 } else if (60 <= group
&& group
< 70) {
694 subgroup
= group
- 60;
695 } else if (70 <= group
&& group
< 80) {
697 subgroup
= group
- 70;
698 } else if (80 <= group
&& group
< 90) {
700 subgroup
= group
- 80;
702 } else if (200 <= group
&& group
< 300) {
704 subgroup
= group
- 200;
705 } else if (600 <= group
&& group
< 700) {
708 subsubgroup
= group
- 650;
710 } else if (6000 <= group
&& group
< 7000) {
713 subsubgroup
= group
- 6500;
718 return pm_translate_signal_group_number_on_island2(subgroup
);
720 return pm_translate_signal_group_number_on_island3(subgroup
);
722 return pm_translate_signal_group_number_on_island4(subgroup
);
724 return pm_translate_signal_group_number_on_island5(subgroup
);
726 return pm_translate_signal_group_number_on_island6(subgroup
,
729 return pm_translate_signal_group_number_on_island7(subgroup
);
731 return pm_translate_signal_group_number_on_island8(subgroup
);
733 dev_dbg(sbd_core(), "%s:%u: island not found: %lu\n", __func__
,
741 static u64
pm_bus_word_to_ps3_lv1_bus_word(u8 word
)
757 static int __ps3_set_signal(u64 lv1_signal_group
, u64 bus_select
,
758 u64 signal_select
, u64 attr1
, u64 attr2
, u64 attr3
)
762 ret
= lv1_set_lpm_signal(lpm_priv
->lpm_id
, lv1_signal_group
, bus_select
,
763 signal_select
, attr1
, attr2
, attr3
);
766 "%s:%u: error:%d 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
767 __func__
, __LINE__
, ret
, lv1_signal_group
, bus_select
,
768 signal_select
, attr1
, attr2
, attr3
);
773 int ps3_set_signal(u64 signal_group
, u8 signal_bit
, u16 sub_unit
,
777 u64 lv1_signal_group
;
780 u64 attr1
, attr2
, attr3
;
782 if (signal_group
== 0)
783 return __ps3_set_signal(0, 0, 0, 0, 0, 0);
786 pm_signal_group_to_ps3_lv1_signal_group(signal_group
);
787 bus_select
= pm_bus_word_to_ps3_lv1_bus_word(bus_word
);
789 switch (signal_group
) {
790 case PM_SIG_GROUP_SPU_TRIGGER
:
792 signal_select
= signal_select
<< (63 - signal_bit
);
794 case PM_SIG_GROUP_SPU_EVENT
:
796 signal_select
= (signal_select
<< (63 - signal_bit
)) | 0x3;
804 * 0: physical object.
806 * This parameter is only used for the PPE and SPE signals.
811 * This parameter is used to specify the target physical/logical
814 if (PM_SIG_GROUP_SPU
<= signal_group
&&
815 signal_group
< PM_SIG_GROUP_MFC_MAX
)
818 attr2
= lpm_priv
->pu_id
;
821 * This parameter is only used for setting the SPE signal.
825 ret
= __ps3_set_signal(lv1_signal_group
, bus_select
, signal_select
,
826 attr1
, attr2
, attr3
);
828 dev_err(sbd_core(), "%s:%u: __ps3_set_signal failed: %d\n",
829 __func__
, __LINE__
, ret
);
833 EXPORT_SYMBOL_GPL(ps3_set_signal
);
835 u32
ps3_get_hw_thread_id(int cpu
)
837 return get_hard_smp_processor_id(cpu
);
839 EXPORT_SYMBOL_GPL(ps3_get_hw_thread_id
);
842 * ps3_enable_pm - Enable the entire performance monitoring unit.
844 * When we enable the LPM, all pending writes to counters get committed.
847 void ps3_enable_pm(u32 cpu
)
851 int insert_bookmark
= 0;
853 lpm_priv
->tb_count
= 0;
855 if (use_start_stop_bookmark
) {
856 if (!(lpm_priv
->shadow
.pm_start_stop
&
857 (PS3_PM_START_STOP_START_MASK
858 | PS3_PM_START_STOP_STOP_MASK
))) {
859 result
= lv1_set_lpm_trigger_control(lpm_priv
->lpm_id
,
860 (PS3_PM_START_STOP_PPU_TH0_BOOKMARK_START
|
861 PS3_PM_START_STOP_PPU_TH1_BOOKMARK_START
|
862 PS3_PM_START_STOP_PPU_TH0_BOOKMARK_STOP
|
863 PS3_PM_START_STOP_PPU_TH1_BOOKMARK_STOP
),
864 0xFFFFFFFFFFFFFFFFULL
, &tmp
);
867 dev_err(sbd_core(), "%s:%u: "
868 "lv1_set_lpm_trigger_control failed: "
869 "%s\n", __func__
, __LINE__
,
872 insert_bookmark
= !result
;
876 result
= lv1_start_lpm(lpm_priv
->lpm_id
);
879 dev_err(sbd_core(), "%s:%u: lv1_start_lpm failed: %s\n",
880 __func__
, __LINE__
, ps3_result(result
));
882 if (use_start_stop_bookmark
&& !result
&& insert_bookmark
)
883 ps3_set_bookmark(get_tb() | PS3_PM_BOOKMARK_START
);
885 EXPORT_SYMBOL_GPL(ps3_enable_pm
);
888 * ps3_disable_pm - Disable the entire performance monitoring unit.
891 void ps3_disable_pm(u32 cpu
)
896 ps3_set_bookmark(get_tb() | PS3_PM_BOOKMARK_STOP
);
898 result
= lv1_stop_lpm(lpm_priv
->lpm_id
, &tmp
);
901 if(result
!= LV1_WRONG_STATE
)
902 dev_err(sbd_core(), "%s:%u: lv1_stop_lpm failed: %s\n",
903 __func__
, __LINE__
, ps3_result(result
));
907 lpm_priv
->tb_count
= tmp
;
909 dev_dbg(sbd_core(), "%s:%u: tb_count %lu (%lxh)\n", __func__
, __LINE__
,
910 lpm_priv
->tb_count
, lpm_priv
->tb_count
);
912 EXPORT_SYMBOL_GPL(ps3_disable_pm
);
915 * ps3_lpm_copy_tb - Copy data from the trace buffer to a kernel buffer.
916 * @offset: Offset in bytes from the start of the trace buffer.
917 * @buf: Copy destination.
918 * @count: Maximum count of bytes to copy.
919 * @bytes_copied: Pointer to a variable that will recieve the number of
920 * bytes copied to @buf.
922 * On error @buf will contain any successfully copied trace buffer data
923 * and bytes_copied will be set to the number of bytes successfully copied.
926 int ps3_lpm_copy_tb(unsigned long offset
, void *buf
, unsigned long count
,
927 unsigned long *bytes_copied
)
933 if (!lpm_priv
->tb_cache
)
936 if (offset
>= lpm_priv
->tb_count
)
939 count
= min(count
, lpm_priv
->tb_count
- offset
);
941 while (*bytes_copied
< count
) {
942 const unsigned long request
= count
- *bytes_copied
;
945 result
= lv1_copy_lpm_trace_buffer(lpm_priv
->lpm_id
, offset
,
948 dev_dbg(sbd_core(), "%s:%u: 0x%lx bytes at 0x%lx\n",
949 __func__
, __LINE__
, request
, offset
);
951 dev_err(sbd_core(), "%s:%u: lv1_copy_lpm_trace_buffer "
952 "failed: %s\n", __func__
, __LINE__
,
954 return result
== LV1_WRONG_STATE
? -EBUSY
: -EINVAL
;
957 memcpy(buf
, lpm_priv
->tb_cache
, tmp
);
959 *bytes_copied
+= tmp
;
962 dev_dbg(sbd_core(), "%s:%u: copied %lxh bytes\n", __func__
, __LINE__
,
967 EXPORT_SYMBOL_GPL(ps3_lpm_copy_tb
);
970 * ps3_lpm_copy_tb_to_user - Copy data from the trace buffer to a user buffer.
971 * @offset: Offset in bytes from the start of the trace buffer.
972 * @buf: A __user copy destination.
973 * @count: Maximum count of bytes to copy.
974 * @bytes_copied: Pointer to a variable that will recieve the number of
975 * bytes copied to @buf.
977 * On error @buf will contain any successfully copied trace buffer data
978 * and bytes_copied will be set to the number of bytes successfully copied.
981 int ps3_lpm_copy_tb_to_user(unsigned long offset
, void __user
*buf
,
982 unsigned long count
, unsigned long *bytes_copied
)
988 if (!lpm_priv
->tb_cache
)
991 if (offset
>= lpm_priv
->tb_count
)
994 count
= min(count
, lpm_priv
->tb_count
- offset
);
996 while (*bytes_copied
< count
) {
997 const unsigned long request
= count
- *bytes_copied
;
1000 result
= lv1_copy_lpm_trace_buffer(lpm_priv
->lpm_id
, offset
,
1003 dev_dbg(sbd_core(), "%s:%u: 0x%lx bytes at 0x%lx\n",
1004 __func__
, __LINE__
, request
, offset
);
1005 dev_err(sbd_core(), "%s:%u: lv1_copy_lpm_trace_buffer "
1006 "failed: %s\n", __func__
, __LINE__
,
1007 ps3_result(result
));
1008 return result
== LV1_WRONG_STATE
? -EBUSY
: -EINVAL
;
1011 result
= copy_to_user(buf
, lpm_priv
->tb_cache
, tmp
);
1014 dev_dbg(sbd_core(), "%s:%u: 0x%lx bytes at 0x%p\n",
1015 __func__
, __LINE__
, tmp
, buf
);
1016 dev_err(sbd_core(), "%s:%u: copy_to_user failed: %d\n",
1017 __func__
, __LINE__
, result
);
1022 *bytes_copied
+= tmp
;
1025 dev_dbg(sbd_core(), "%s:%u: copied %lxh bytes\n", __func__
, __LINE__
,
1030 EXPORT_SYMBOL_GPL(ps3_lpm_copy_tb_to_user
);
1033 * ps3_get_and_clear_pm_interrupts -
1035 * Clearing interrupts for the entire performance monitoring unit.
1036 * Reading pm_status clears the interrupt bits.
1039 u32
ps3_get_and_clear_pm_interrupts(u32 cpu
)
1041 return ps3_read_pm(cpu
, pm_status
);
1043 EXPORT_SYMBOL_GPL(ps3_get_and_clear_pm_interrupts
);
1046 * ps3_enable_pm_interrupts -
1048 * Enabling interrupts for the entire performance monitoring unit.
1049 * Enables the interrupt bits in the pm_status register.
1052 void ps3_enable_pm_interrupts(u32 cpu
, u32 thread
, u32 mask
)
1055 ps3_write_pm(cpu
, pm_status
, mask
);
1057 EXPORT_SYMBOL_GPL(ps3_enable_pm_interrupts
);
1060 * ps3_enable_pm_interrupts -
1062 * Disabling interrupts for the entire performance monitoring unit.
1065 void ps3_disable_pm_interrupts(u32 cpu
)
1067 ps3_get_and_clear_pm_interrupts(cpu
);
1068 ps3_write_pm(cpu
, pm_status
, 0);
1070 EXPORT_SYMBOL_GPL(ps3_disable_pm_interrupts
);
1073 * ps3_lpm_open - Open the logical performance monitor device.
1074 * @tb_type: Specifies the type of trace buffer lv1 sould use for this lpm
1075 * instance, specified by one of enum ps3_lpm_tb_type.
1076 * @tb_cache: Optional user supplied buffer to use as the trace buffer cache.
1077 * If NULL, the driver will allocate and manage an internal buffer.
1078 * Unused when when @tb_type is PS3_LPM_TB_TYPE_NONE.
1079 * @tb_cache_size: The size in bytes of the user supplied @tb_cache buffer.
1080 * Unused when @tb_cache is NULL or @tb_type is PS3_LPM_TB_TYPE_NONE.
1083 int ps3_lpm_open(enum ps3_lpm_tb_type tb_type
, void *tb_cache
,
1090 BUG_ON(tb_type
!= PS3_LPM_TB_TYPE_NONE
1091 && tb_type
!= PS3_LPM_TB_TYPE_INTERNAL
);
1093 if (tb_type
== PS3_LPM_TB_TYPE_NONE
&& tb_cache
)
1094 dev_dbg(sbd_core(), "%s:%u: bad in vals\n", __func__
, __LINE__
);
1096 if (!atomic_add_unless(&lpm_priv
->open
, 1, 1)) {
1097 dev_dbg(sbd_core(), "%s:%u: busy\n", __func__
, __LINE__
);
1101 /* Note tb_cache needs 128 byte alignment. */
1103 if (tb_type
== PS3_LPM_TB_TYPE_NONE
) {
1104 lpm_priv
->tb_cache_size
= 0;
1105 lpm_priv
->tb_cache_internal
= NULL
;
1106 lpm_priv
->tb_cache
= NULL
;
1107 } else if (tb_cache
) {
1108 if (tb_cache
!= (void *)_ALIGN_UP((unsigned long)tb_cache
, 128)
1109 || tb_cache_size
!= _ALIGN_UP(tb_cache_size
, 128)) {
1110 dev_err(sbd_core(), "%s:%u: unaligned tb_cache\n",
1111 __func__
, __LINE__
);
1115 lpm_priv
->tb_cache_size
= tb_cache_size
;
1116 lpm_priv
->tb_cache_internal
= NULL
;
1117 lpm_priv
->tb_cache
= tb_cache
;
1119 lpm_priv
->tb_cache_size
= PS3_LPM_DEFAULT_TB_CACHE_SIZE
;
1120 lpm_priv
->tb_cache_internal
= kzalloc(
1121 lpm_priv
->tb_cache_size
+ 127, GFP_KERNEL
);
1122 if (!lpm_priv
->tb_cache_internal
) {
1123 dev_err(sbd_core(), "%s:%u: alloc internal tb_cache "
1124 "failed\n", __func__
, __LINE__
);
1128 lpm_priv
->tb_cache
= (void *)_ALIGN_UP(
1129 (unsigned long)lpm_priv
->tb_cache_internal
, 128);
1132 result
= lv1_construct_lpm(lpm_priv
->node_id
, tb_type
, 0, 0,
1133 ps3_mm_phys_to_lpar(__pa(lpm_priv
->tb_cache
)),
1134 lpm_priv
->tb_cache_size
, &lpm_priv
->lpm_id
,
1135 &lpm_priv
->outlet_id
, &tb_size
);
1138 dev_err(sbd_core(), "%s:%u: lv1_construct_lpm failed: %s\n",
1139 __func__
, __LINE__
, ps3_result(result
));
1141 goto fail_construct
;
1144 lpm_priv
->shadow
.pm_control
= PS3_LPM_SHADOW_REG_INIT
;
1145 lpm_priv
->shadow
.pm_start_stop
= PS3_LPM_SHADOW_REG_INIT
;
1146 lpm_priv
->shadow
.group_control
= PS3_LPM_SHADOW_REG_INIT
;
1147 lpm_priv
->shadow
.debug_bus_control
= PS3_LPM_SHADOW_REG_INIT
;
1149 dev_dbg(sbd_core(), "%s:%u: lpm_id 0x%lx, outlet_id 0x%lx, "
1150 "tb_size 0x%lx\n", __func__
, __LINE__
, lpm_priv
->lpm_id
,
1151 lpm_priv
->outlet_id
, tb_size
);
1156 kfree(lpm_priv
->tb_cache_internal
);
1157 lpm_priv
->tb_cache_internal
= NULL
;
1160 atomic_dec(&lpm_priv
->open
);
1163 EXPORT_SYMBOL_GPL(ps3_lpm_open
);
1166 * ps3_lpm_close - Close the lpm device.
1170 int ps3_lpm_close(void)
1172 dev_dbg(sbd_core(), "%s:%u\n", __func__
, __LINE__
);
1174 lv1_destruct_lpm(lpm_priv
->lpm_id
);
1175 lpm_priv
->lpm_id
= 0;
1177 kfree(lpm_priv
->tb_cache_internal
);
1178 lpm_priv
->tb_cache_internal
= NULL
;
1180 atomic_dec(&lpm_priv
->open
);
1183 EXPORT_SYMBOL_GPL(ps3_lpm_close
);
1185 static int __devinit
ps3_lpm_probe(struct ps3_system_bus_device
*dev
)
1187 dev_dbg(&dev
->core
, " -> %s:%u\n", __func__
, __LINE__
);
1190 dev_info(&dev
->core
, "%s:%u: called twice\n",
1191 __func__
, __LINE__
);
1195 lpm_priv
= kzalloc(sizeof(*lpm_priv
), GFP_KERNEL
);
1200 lpm_priv
->sbd
= dev
;
1201 lpm_priv
->node_id
= dev
->lpm
.node_id
;
1202 lpm_priv
->pu_id
= dev
->lpm
.pu_id
;
1203 lpm_priv
->rights
= dev
->lpm
.rights
;
1205 dev_info(&dev
->core
, " <- %s:%u:\n", __func__
, __LINE__
);
1210 static int ps3_lpm_remove(struct ps3_system_bus_device
*dev
)
1212 dev_dbg(&dev
->core
, " -> %s:%u:\n", __func__
, __LINE__
);
1219 dev_info(&dev
->core
, " <- %s:%u:\n", __func__
, __LINE__
);
1223 static struct ps3_system_bus_driver ps3_lpm_driver
= {
1224 .match_id
= PS3_MATCH_ID_LPM
,
1225 .core
.name
= "ps3-lpm",
1226 .core
.owner
= THIS_MODULE
,
1227 .probe
= ps3_lpm_probe
,
1228 .remove
= ps3_lpm_remove
,
1229 .shutdown
= ps3_lpm_remove
,
1232 static int __init
ps3_lpm_init(void)
1234 pr_debug("%s:%d:\n", __func__
, __LINE__
);
1235 return ps3_system_bus_driver_register(&ps3_lpm_driver
);
1238 static void __exit
ps3_lpm_exit(void)
1240 pr_debug("%s:%d:\n", __func__
, __LINE__
);
1241 ps3_system_bus_driver_unregister(&ps3_lpm_driver
);
1244 module_init(ps3_lpm_init
);
1245 module_exit(ps3_lpm_exit
);
1247 MODULE_LICENSE("GPL v2");
1248 MODULE_DESCRIPTION("PS3 Logical Performance Monitor Driver");
1249 MODULE_AUTHOR("Sony Corporation");
1250 MODULE_ALIAS(PS3_MODULE_ALIAS_LPM
);