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>
28 #include <asm/lv1call.h>
29 #include <asm/cell-pmu.h>
32 /* BOOKMARK tag macros */
33 #define PS3_PM_BOOKMARK_START 0x8000000000000000ULL
34 #define PS3_PM_BOOKMARK_STOP 0x4000000000000000ULL
35 #define PS3_PM_BOOKMARK_TAG_KERNEL 0x1000000000000000ULL
36 #define PS3_PM_BOOKMARK_TAG_USER 0x3000000000000000ULL
37 #define PS3_PM_BOOKMARK_TAG_MASK_HI 0xF000000000000000ULL
38 #define PS3_PM_BOOKMARK_TAG_MASK_LO 0x0F00000000000000ULL
40 /* CBE PM CONTROL register macros */
41 #define PS3_PM_CONTROL_PPU_TH0_BOOKMARK 0x00001000
42 #define PS3_PM_CONTROL_PPU_TH1_BOOKMARK 0x00000800
43 #define PS3_PM_CONTROL_PPU_COUNT_MODE_MASK 0x000C0000
44 #define PS3_PM_CONTROL_PPU_COUNT_MODE_PROBLEM 0x00080000
45 #define PS3_WRITE_PM_MASK 0xFFFFFFFFFFFFFFFFULL
47 /* CBE PM START STOP register macros */
48 #define PS3_PM_START_STOP_PPU_TH0_BOOKMARK_START 0x02000000
49 #define PS3_PM_START_STOP_PPU_TH1_BOOKMARK_START 0x01000000
50 #define PS3_PM_START_STOP_PPU_TH0_BOOKMARK_STOP 0x00020000
51 #define PS3_PM_START_STOP_PPU_TH1_BOOKMARK_STOP 0x00010000
52 #define PS3_PM_START_STOP_START_MASK 0xFF000000
53 #define PS3_PM_START_STOP_STOP_MASK 0x00FF0000
55 /* CBE PM COUNTER register macres */
56 #define PS3_PM_COUNTER_MASK_HI 0xFFFFFFFF00000000ULL
57 #define PS3_PM_COUNTER_MASK_LO 0x00000000FFFFFFFFULL
59 /* BASE SIGNAL GROUP NUMBER macros */
60 #define PM_ISLAND2_BASE_SIGNAL_GROUP_NUMBER 0
61 #define PM_ISLAND2_SIGNAL_GROUP_NUMBER1 6
62 #define PM_ISLAND2_SIGNAL_GROUP_NUMBER2 7
63 #define PM_ISLAND3_BASE_SIGNAL_GROUP_NUMBER 7
64 #define PM_ISLAND4_BASE_SIGNAL_GROUP_NUMBER 15
65 #define PM_SPU_TRIGGER_SIGNAL_GROUP_NUMBER 17
66 #define PM_SPU_EVENT_SIGNAL_GROUP_NUMBER 18
67 #define PM_ISLAND5_BASE_SIGNAL_GROUP_NUMBER 18
68 #define PM_ISLAND6_BASE_SIGNAL_GROUP_NUMBER 24
69 #define PM_ISLAND7_BASE_SIGNAL_GROUP_NUMBER 49
70 #define PM_ISLAND8_BASE_SIGNAL_GROUP_NUMBER 52
71 #define PM_SIG_GROUP_SPU 41
72 #define PM_SIG_GROUP_SPU_TRIGGER 42
73 #define PM_SIG_GROUP_SPU_EVENT 43
74 #define PM_SIG_GROUP_MFC_MAX 60
77 * struct ps3_lpm_shadow_regs - Performance monitor shadow registers.
79 * @pm_control: Shadow of the processor's pm_control register.
80 * @pm_start_stop: Shadow of the processor's pm_start_stop register.
81 * @group_control: Shadow of the processor's group_control register.
82 * @debug_bus_control: Shadow of the processor's debug_bus_control register.
84 * The logical performance monitor provides a write-only interface to
85 * these processor registers. These shadow variables cache the processor
86 * register values for reading.
88 * The initial value of the shadow registers at lpm creation is
89 * PS3_LPM_SHADOW_REG_INIT.
92 struct ps3_lpm_shadow_regs
{
96 u64 debug_bus_control
;
99 #define PS3_LPM_SHADOW_REG_INIT 0xFFFFFFFF00000000ULL
102 * struct ps3_lpm_priv - Private lpm device data.
104 * @open: An atomic variable indicating the lpm driver has been opened.
105 * @rights: The lpm rigths granted by the system policy module. A logical
106 * OR of enum ps3_lpm_rights.
107 * @node_id: The node id of a BE prosessor whose performance monitor this
108 * lpar has the right to use.
109 * @pu_id: The lv1 id of the logical PU.
110 * @lpm_id: The lv1 id of this lpm instance.
111 * @outlet_id: The outlet created by lv1 for this lpm instance.
112 * @tb_count: The number of bytes of data held in the lv1 trace buffer.
113 * @tb_cache: Kernel buffer to receive the data from the lv1 trace buffer.
114 * Must be 128 byte aligned.
115 * @tb_cache_size: Size of the kernel @tb_cache buffer. Must be 128 byte
117 * @tb_cache_internal: An unaligned buffer allocated by this driver to be
118 * used for the trace buffer cache when ps3_lpm_open() is called with a
119 * NULL tb_cache argument. Otherwise unused.
120 * @shadow: Processor register shadow of type struct ps3_lpm_shadow_regs.
121 * @sbd: The struct ps3_system_bus_device attached to this driver.
123 * The trace buffer is a buffer allocated and used internally to the lv1
124 * hypervisor to collect trace data. The trace buffer cache is a guest
125 * buffer that accepts the trace data from the trace buffer.
128 struct ps3_lpm_priv
{
138 void *tb_cache_internal
;
139 struct ps3_lpm_shadow_regs shadow
;
140 struct ps3_system_bus_device
*sbd
;
144 PS3_LPM_DEFAULT_TB_CACHE_SIZE
= 0x4000,
148 * lpm_priv - Static instance of the lpm data.
150 * Since the exported routines don't support the notion of a device
151 * instance we need to hold the instance in this static variable
152 * and then only allow at most one instance at a time to be created.
155 static struct ps3_lpm_priv
*lpm_priv
;
157 static struct device
*sbd_core(void)
159 BUG_ON(!lpm_priv
|| !lpm_priv
->sbd
);
160 return &lpm_priv
->sbd
->core
;
164 * use_start_stop_bookmark - Enable the PPU bookmark trace.
166 * And it enables PPU bookmark triggers ONLY if the other triggers are not set.
167 * The start/stop bookmarks are inserted at ps3_enable_pm() and ps3_disable_pm()
170 * Used to get good quality of the performance counter.
173 enum {use_start_stop_bookmark
= 1,};
175 void ps3_set_bookmark(u64 bookmark
)
178 * As per the PPE book IV, to avoid bookmark loss there must
179 * not be a traced branch within 10 cycles of setting the
180 * SPRN_BKMK register. The actual text is unclear if 'within'
181 * includes cycles before the call.
184 asm volatile("nop;nop;nop;nop;nop;nop;nop;nop;nop;");
185 mtspr(SPRN_BKMK
, bookmark
);
186 asm volatile("nop;nop;nop;nop;nop;nop;nop;nop;nop;");
188 EXPORT_SYMBOL_GPL(ps3_set_bookmark
);
190 void ps3_set_pm_bookmark(u64 tag
, u64 incident
, u64 th_id
)
194 bookmark
= (get_tb() & 0x00000000FFFFFFFFULL
) |
195 PS3_PM_BOOKMARK_TAG_KERNEL
;
196 bookmark
= ((tag
<< 56) & PS3_PM_BOOKMARK_TAG_MASK_LO
) |
197 (incident
<< 48) | (th_id
<< 32) | bookmark
;
198 ps3_set_bookmark(bookmark
);
200 EXPORT_SYMBOL_GPL(ps3_set_pm_bookmark
);
203 * ps3_read_phys_ctr - Read physical counter registers.
205 * Each physical counter can act as one 32 bit counter or as two 16 bit
209 u32
ps3_read_phys_ctr(u32 cpu
, u32 phys_ctr
)
215 if (phys_ctr
>= NR_PHYS_CTRS
) {
216 dev_dbg(sbd_core(), "%s:%u: phys_ctr too big: %u\n", __func__
,
221 result
= lv1_set_lpm_counter(lpm_priv
->lpm_id
, 0, 0, 0, 0, &counter0415
,
224 dev_err(sbd_core(), "%s:%u: lv1_set_lpm_counter failed: "
225 "phys_ctr %u, %s\n", __func__
, __LINE__
, phys_ctr
,
232 return counter0415
>> 32;
234 return counter0415
& PS3_PM_COUNTER_MASK_LO
;
236 return counter2637
>> 32;
238 return counter2637
& PS3_PM_COUNTER_MASK_LO
;
244 EXPORT_SYMBOL_GPL(ps3_read_phys_ctr
);
247 * ps3_write_phys_ctr - Write physical counter registers.
249 * Each physical counter can act as one 32 bit counter or as two 16 bit
253 void ps3_write_phys_ctr(u32 cpu
, u32 phys_ctr
, u32 val
)
256 u64 counter0415_mask
;
258 u64 counter2637_mask
;
261 if (phys_ctr
>= NR_PHYS_CTRS
) {
262 dev_dbg(sbd_core(), "%s:%u: phys_ctr too big: %u\n", __func__
,
269 counter0415
= (u64
)val
<< 32;
270 counter0415_mask
= PS3_PM_COUNTER_MASK_HI
;
272 counter2637_mask
= 0x0;
275 counter0415
= (u64
)val
;
276 counter0415_mask
= PS3_PM_COUNTER_MASK_LO
;
278 counter2637_mask
= 0x0;
282 counter0415_mask
= 0x0;
283 counter2637
= (u64
)val
<< 32;
284 counter2637_mask
= PS3_PM_COUNTER_MASK_HI
;
288 counter0415_mask
= 0x0;
289 counter2637
= (u64
)val
;
290 counter2637_mask
= PS3_PM_COUNTER_MASK_LO
;
296 result
= lv1_set_lpm_counter(lpm_priv
->lpm_id
,
297 counter0415
, counter0415_mask
,
298 counter2637
, counter2637_mask
,
299 &counter0415
, &counter2637
);
301 dev_err(sbd_core(), "%s:%u: lv1_set_lpm_counter failed: "
302 "phys_ctr %u, val %u, %s\n", __func__
, __LINE__
,
303 phys_ctr
, val
, ps3_result(result
));
305 EXPORT_SYMBOL_GPL(ps3_write_phys_ctr
);
308 * ps3_read_ctr - Read counter.
310 * Read 16 or 32 bits depending on the current size of the counter.
311 * Counters 4, 5, 6 & 7 are always 16 bit.
314 u32
ps3_read_ctr(u32 cpu
, u32 ctr
)
317 u32 phys_ctr
= ctr
& (NR_PHYS_CTRS
- 1);
319 val
= ps3_read_phys_ctr(cpu
, phys_ctr
);
321 if (ps3_get_ctr_size(cpu
, phys_ctr
) == 16)
322 val
= (ctr
< NR_PHYS_CTRS
) ? (val
>> 16) : (val
& 0xffff);
326 EXPORT_SYMBOL_GPL(ps3_read_ctr
);
329 * ps3_write_ctr - Write counter.
331 * Write 16 or 32 bits depending on the current size of the counter.
332 * Counters 4, 5, 6 & 7 are always 16 bit.
335 void ps3_write_ctr(u32 cpu
, u32 ctr
, u32 val
)
340 phys_ctr
= ctr
& (NR_PHYS_CTRS
- 1);
342 if (ps3_get_ctr_size(cpu
, phys_ctr
) == 16) {
343 phys_val
= ps3_read_phys_ctr(cpu
, phys_ctr
);
345 if (ctr
< NR_PHYS_CTRS
)
346 val
= (val
<< 16) | (phys_val
& 0xffff);
348 val
= (val
& 0xffff) | (phys_val
& 0xffff0000);
351 ps3_write_phys_ctr(cpu
, phys_ctr
, val
);
353 EXPORT_SYMBOL_GPL(ps3_write_ctr
);
356 * ps3_read_pm07_control - Read counter control registers.
358 * Each logical counter has a corresponding control register.
361 u32
ps3_read_pm07_control(u32 cpu
, u32 ctr
)
365 EXPORT_SYMBOL_GPL(ps3_read_pm07_control
);
368 * ps3_write_pm07_control - Write counter control registers.
370 * Each logical counter has a corresponding control register.
373 void ps3_write_pm07_control(u32 cpu
, u32 ctr
, u32 val
)
376 static const u64 mask
= 0xFFFFFFFFFFFFFFFFULL
;
379 if (ctr
>= NR_CTRS
) {
380 dev_dbg(sbd_core(), "%s:%u: ctr too big: %u\n", __func__
,
385 result
= lv1_set_lpm_counter_control(lpm_priv
->lpm_id
, ctr
, val
, mask
,
388 dev_err(sbd_core(), "%s:%u: lv1_set_lpm_counter_control "
389 "failed: ctr %u, %s\n", __func__
, __LINE__
, ctr
,
392 EXPORT_SYMBOL_GPL(ps3_write_pm07_control
);
395 * ps3_read_pm - Read Other LPM control registers.
398 u32
ps3_read_pm(u32 cpu
, enum pm_reg_name reg
)
405 return lpm_priv
->shadow
.pm_control
;
407 return CBE_PM_TRACE_BUF_EMPTY
;
409 return lpm_priv
->shadow
.pm_start_stop
;
411 result
= lv1_set_lpm_interval(lpm_priv
->lpm_id
, 0, 0, &val
);
414 dev_dbg(sbd_core(), "%s:%u: lv1 set_inteval failed: "
415 "reg %u, %s\n", __func__
, __LINE__
, reg
,
420 return lpm_priv
->shadow
.group_control
;
421 case debug_bus_control
:
422 return lpm_priv
->shadow
.debug_bus_control
;
424 result
= lv1_get_lpm_interrupt_status(lpm_priv
->lpm_id
,
428 dev_dbg(sbd_core(), "%s:%u: lv1 get_lpm_status failed: "
429 "reg %u, %s\n", __func__
, __LINE__
, reg
,
436 dev_dbg(sbd_core(), "%s:%u: unknown reg: %d\n", __func__
,
444 EXPORT_SYMBOL_GPL(ps3_read_pm
);
447 * ps3_write_pm - Write Other LPM control registers.
450 void ps3_write_pm(u32 cpu
, enum pm_reg_name reg
, u32 val
)
457 if (val
!= lpm_priv
->shadow
.group_control
)
458 result
= lv1_set_lpm_group_control(lpm_priv
->lpm_id
,
462 lpm_priv
->shadow
.group_control
= val
;
464 case debug_bus_control
:
465 if (val
!= lpm_priv
->shadow
.debug_bus_control
)
466 result
= lv1_set_lpm_debug_bus_control(lpm_priv
->lpm_id
,
470 lpm_priv
->shadow
.debug_bus_control
= val
;
473 if (use_start_stop_bookmark
)
474 val
|= (PS3_PM_CONTROL_PPU_TH0_BOOKMARK
|
475 PS3_PM_CONTROL_PPU_TH1_BOOKMARK
);
476 if (val
!= lpm_priv
->shadow
.pm_control
)
477 result
= lv1_set_lpm_general_control(lpm_priv
->lpm_id
,
482 lpm_priv
->shadow
.pm_control
= val
;
485 result
= lv1_set_lpm_interval(lpm_priv
->lpm_id
, val
,
486 PS3_WRITE_PM_MASK
, &dummy
);
489 if (val
!= lpm_priv
->shadow
.pm_start_stop
)
490 result
= lv1_set_lpm_trigger_control(lpm_priv
->lpm_id
,
494 lpm_priv
->shadow
.pm_start_stop
= val
;
501 dev_dbg(sbd_core(), "%s:%u: unknown reg: %d\n", __func__
,
508 dev_err(sbd_core(), "%s:%u: lv1 set_control failed: "
509 "reg %u, %s\n", __func__
, __LINE__
, reg
,
512 EXPORT_SYMBOL_GPL(ps3_write_pm
);
515 * ps3_get_ctr_size - Get the size of a physical counter.
517 * Returns either 16 or 32.
520 u32
ps3_get_ctr_size(u32 cpu
, u32 phys_ctr
)
524 if (phys_ctr
>= NR_PHYS_CTRS
) {
525 dev_dbg(sbd_core(), "%s:%u: phys_ctr too big: %u\n", __func__
,
530 pm_ctrl
= ps3_read_pm(cpu
, pm_control
);
531 return (pm_ctrl
& CBE_PM_16BIT_CTR(phys_ctr
)) ? 16 : 32;
533 EXPORT_SYMBOL_GPL(ps3_get_ctr_size
);
536 * ps3_set_ctr_size - Set the size of a physical counter to 16 or 32 bits.
539 void ps3_set_ctr_size(u32 cpu
, u32 phys_ctr
, u32 ctr_size
)
543 if (phys_ctr
>= NR_PHYS_CTRS
) {
544 dev_dbg(sbd_core(), "%s:%u: phys_ctr too big: %u\n", __func__
,
549 pm_ctrl
= ps3_read_pm(cpu
, pm_control
);
553 pm_ctrl
|= CBE_PM_16BIT_CTR(phys_ctr
);
554 ps3_write_pm(cpu
, pm_control
, pm_ctrl
);
558 pm_ctrl
&= ~CBE_PM_16BIT_CTR(phys_ctr
);
559 ps3_write_pm(cpu
, pm_control
, pm_ctrl
);
565 EXPORT_SYMBOL_GPL(ps3_set_ctr_size
);
567 static u64
pm_translate_signal_group_number_on_island2(u64 subgroup
)
574 return PM_ISLAND2_BASE_SIGNAL_GROUP_NUMBER
+ subgroup
;
575 else if (subgroup
== 7)
576 return PM_ISLAND2_SIGNAL_GROUP_NUMBER1
;
578 return PM_ISLAND2_SIGNAL_GROUP_NUMBER2
;
581 static u64
pm_translate_signal_group_number_on_island3(u64 subgroup
)
596 return PM_ISLAND3_BASE_SIGNAL_GROUP_NUMBER
+ subgroup
;
599 static u64
pm_translate_signal_group_number_on_island4(u64 subgroup
)
601 return PM_ISLAND4_BASE_SIGNAL_GROUP_NUMBER
+ subgroup
;
604 static u64
pm_translate_signal_group_number_on_island5(u64 subgroup
)
617 return PM_ISLAND5_BASE_SIGNAL_GROUP_NUMBER
+ subgroup
;
620 static u64
pm_translate_signal_group_number_on_island6(u64 subgroup
,
633 switch (subsubgroup
) {
655 return (PM_ISLAND6_BASE_SIGNAL_GROUP_NUMBER
+ subgroup
);
657 return (PM_ISLAND6_BASE_SIGNAL_GROUP_NUMBER
+ subgroup
661 static u64
pm_translate_signal_group_number_on_island7(u64 subgroup
)
663 return PM_ISLAND7_BASE_SIGNAL_GROUP_NUMBER
+ subgroup
;
666 static u64
pm_translate_signal_group_number_on_island8(u64 subgroup
)
668 return PM_ISLAND8_BASE_SIGNAL_GROUP_NUMBER
+ subgroup
;
671 static u64
pm_signal_group_to_ps3_lv1_signal_group(u64 group
)
682 if (20 <= group
&& group
< 30) {
684 subgroup
= group
- 20;
685 } else if (30 <= group
&& group
< 40) {
687 subgroup
= group
- 30;
688 } else if (40 <= group
&& group
< 50) {
690 subgroup
= group
- 40;
691 } else if (50 <= group
&& group
< 60) {
693 subgroup
= group
- 50;
694 } else if (60 <= group
&& group
< 70) {
696 subgroup
= group
- 60;
697 } else if (70 <= group
&& group
< 80) {
699 subgroup
= group
- 70;
700 } else if (80 <= group
&& group
< 90) {
702 subgroup
= group
- 80;
704 } else if (200 <= group
&& group
< 300) {
706 subgroup
= group
- 200;
707 } else if (600 <= group
&& group
< 700) {
710 subsubgroup
= group
- 650;
712 } else if (6000 <= group
&& group
< 7000) {
715 subsubgroup
= group
- 6500;
720 return pm_translate_signal_group_number_on_island2(subgroup
);
722 return pm_translate_signal_group_number_on_island3(subgroup
);
724 return pm_translate_signal_group_number_on_island4(subgroup
);
726 return pm_translate_signal_group_number_on_island5(subgroup
);
728 return pm_translate_signal_group_number_on_island6(subgroup
,
731 return pm_translate_signal_group_number_on_island7(subgroup
);
733 return pm_translate_signal_group_number_on_island8(subgroup
);
735 dev_dbg(sbd_core(), "%s:%u: island not found: %llu\n", __func__
,
743 static u64
pm_bus_word_to_ps3_lv1_bus_word(u8 word
)
759 static int __ps3_set_signal(u64 lv1_signal_group
, u64 bus_select
,
760 u64 signal_select
, u64 attr1
, u64 attr2
, u64 attr3
)
764 ret
= lv1_set_lpm_signal(lpm_priv
->lpm_id
, lv1_signal_group
, bus_select
,
765 signal_select
, attr1
, attr2
, attr3
);
768 "%s:%u: error:%d 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx\n",
769 __func__
, __LINE__
, ret
, lv1_signal_group
, bus_select
,
770 signal_select
, attr1
, attr2
, attr3
);
775 int ps3_set_signal(u64 signal_group
, u8 signal_bit
, u16 sub_unit
,
779 u64 lv1_signal_group
;
782 u64 attr1
, attr2
, attr3
;
784 if (signal_group
== 0)
785 return __ps3_set_signal(0, 0, 0, 0, 0, 0);
788 pm_signal_group_to_ps3_lv1_signal_group(signal_group
);
789 bus_select
= pm_bus_word_to_ps3_lv1_bus_word(bus_word
);
791 switch (signal_group
) {
792 case PM_SIG_GROUP_SPU_TRIGGER
:
794 signal_select
= signal_select
<< (63 - signal_bit
);
796 case PM_SIG_GROUP_SPU_EVENT
:
798 signal_select
= (signal_select
<< (63 - signal_bit
)) | 0x3;
806 * 0: physical object.
808 * This parameter is only used for the PPE and SPE signals.
813 * This parameter is used to specify the target physical/logical
816 if (PM_SIG_GROUP_SPU
<= signal_group
&&
817 signal_group
< PM_SIG_GROUP_MFC_MAX
)
820 attr2
= lpm_priv
->pu_id
;
823 * This parameter is only used for setting the SPE signal.
827 ret
= __ps3_set_signal(lv1_signal_group
, bus_select
, signal_select
,
828 attr1
, attr2
, attr3
);
830 dev_err(sbd_core(), "%s:%u: __ps3_set_signal failed: %d\n",
831 __func__
, __LINE__
, ret
);
835 EXPORT_SYMBOL_GPL(ps3_set_signal
);
837 u32
ps3_get_hw_thread_id(int cpu
)
839 return get_hard_smp_processor_id(cpu
);
841 EXPORT_SYMBOL_GPL(ps3_get_hw_thread_id
);
844 * ps3_enable_pm - Enable the entire performance monitoring unit.
846 * When we enable the LPM, all pending writes to counters get committed.
849 void ps3_enable_pm(u32 cpu
)
853 int insert_bookmark
= 0;
855 lpm_priv
->tb_count
= 0;
857 if (use_start_stop_bookmark
) {
858 if (!(lpm_priv
->shadow
.pm_start_stop
&
859 (PS3_PM_START_STOP_START_MASK
860 | PS3_PM_START_STOP_STOP_MASK
))) {
861 result
= lv1_set_lpm_trigger_control(lpm_priv
->lpm_id
,
862 (PS3_PM_START_STOP_PPU_TH0_BOOKMARK_START
|
863 PS3_PM_START_STOP_PPU_TH1_BOOKMARK_START
|
864 PS3_PM_START_STOP_PPU_TH0_BOOKMARK_STOP
|
865 PS3_PM_START_STOP_PPU_TH1_BOOKMARK_STOP
),
866 0xFFFFFFFFFFFFFFFFULL
, &tmp
);
869 dev_err(sbd_core(), "%s:%u: "
870 "lv1_set_lpm_trigger_control failed: "
871 "%s\n", __func__
, __LINE__
,
874 insert_bookmark
= !result
;
878 result
= lv1_start_lpm(lpm_priv
->lpm_id
);
881 dev_err(sbd_core(), "%s:%u: lv1_start_lpm failed: %s\n",
882 __func__
, __LINE__
, ps3_result(result
));
884 if (use_start_stop_bookmark
&& !result
&& insert_bookmark
)
885 ps3_set_bookmark(get_tb() | PS3_PM_BOOKMARK_START
);
887 EXPORT_SYMBOL_GPL(ps3_enable_pm
);
890 * ps3_disable_pm - Disable the entire performance monitoring unit.
893 void ps3_disable_pm(u32 cpu
)
898 ps3_set_bookmark(get_tb() | PS3_PM_BOOKMARK_STOP
);
900 result
= lv1_stop_lpm(lpm_priv
->lpm_id
, &tmp
);
903 if(result
!= LV1_WRONG_STATE
)
904 dev_err(sbd_core(), "%s:%u: lv1_stop_lpm failed: %s\n",
905 __func__
, __LINE__
, ps3_result(result
));
909 lpm_priv
->tb_count
= tmp
;
911 dev_dbg(sbd_core(), "%s:%u: tb_count %llu (%llxh)\n", __func__
, __LINE__
,
912 lpm_priv
->tb_count
, lpm_priv
->tb_count
);
914 EXPORT_SYMBOL_GPL(ps3_disable_pm
);
917 * ps3_lpm_copy_tb - Copy data from the trace buffer to a kernel buffer.
918 * @offset: Offset in bytes from the start of the trace buffer.
919 * @buf: Copy destination.
920 * @count: Maximum count of bytes to copy.
921 * @bytes_copied: Pointer to a variable that will recieve the number of
922 * bytes copied to @buf.
924 * On error @buf will contain any successfully copied trace buffer data
925 * and bytes_copied will be set to the number of bytes successfully copied.
928 int ps3_lpm_copy_tb(unsigned long offset
, void *buf
, unsigned long count
,
929 unsigned long *bytes_copied
)
935 if (!lpm_priv
->tb_cache
)
938 if (offset
>= lpm_priv
->tb_count
)
941 count
= min_t(u64
, count
, lpm_priv
->tb_count
- offset
);
943 while (*bytes_copied
< count
) {
944 const unsigned long request
= count
- *bytes_copied
;
947 result
= lv1_copy_lpm_trace_buffer(lpm_priv
->lpm_id
, offset
,
950 dev_dbg(sbd_core(), "%s:%u: 0x%lx bytes at 0x%lx\n",
951 __func__
, __LINE__
, request
, offset
);
953 dev_err(sbd_core(), "%s:%u: lv1_copy_lpm_trace_buffer "
954 "failed: %s\n", __func__
, __LINE__
,
956 return result
== LV1_WRONG_STATE
? -EBUSY
: -EINVAL
;
959 memcpy(buf
, lpm_priv
->tb_cache
, tmp
);
961 *bytes_copied
+= tmp
;
964 dev_dbg(sbd_core(), "%s:%u: copied %lxh bytes\n", __func__
, __LINE__
,
969 EXPORT_SYMBOL_GPL(ps3_lpm_copy_tb
);
972 * ps3_lpm_copy_tb_to_user - Copy data from the trace buffer to a user buffer.
973 * @offset: Offset in bytes from the start of the trace buffer.
974 * @buf: A __user copy destination.
975 * @count: Maximum count of bytes to copy.
976 * @bytes_copied: Pointer to a variable that will recieve the number of
977 * bytes copied to @buf.
979 * On error @buf will contain any successfully copied trace buffer data
980 * and bytes_copied will be set to the number of bytes successfully copied.
983 int ps3_lpm_copy_tb_to_user(unsigned long offset
, void __user
*buf
,
984 unsigned long count
, unsigned long *bytes_copied
)
990 if (!lpm_priv
->tb_cache
)
993 if (offset
>= lpm_priv
->tb_count
)
996 count
= min_t(u64
, count
, lpm_priv
->tb_count
- offset
);
998 while (*bytes_copied
< count
) {
999 const unsigned long request
= count
- *bytes_copied
;
1002 result
= lv1_copy_lpm_trace_buffer(lpm_priv
->lpm_id
, offset
,
1005 dev_dbg(sbd_core(), "%s:%u: 0x%lx bytes at 0x%lx\n",
1006 __func__
, __LINE__
, request
, offset
);
1007 dev_err(sbd_core(), "%s:%u: lv1_copy_lpm_trace_buffer "
1008 "failed: %s\n", __func__
, __LINE__
,
1009 ps3_result(result
));
1010 return result
== LV1_WRONG_STATE
? -EBUSY
: -EINVAL
;
1013 result
= copy_to_user(buf
, lpm_priv
->tb_cache
, tmp
);
1016 dev_dbg(sbd_core(), "%s:%u: 0x%llx bytes at 0x%p\n",
1017 __func__
, __LINE__
, tmp
, buf
);
1018 dev_err(sbd_core(), "%s:%u: copy_to_user failed: %d\n",
1019 __func__
, __LINE__
, result
);
1024 *bytes_copied
+= tmp
;
1027 dev_dbg(sbd_core(), "%s:%u: copied %lxh bytes\n", __func__
, __LINE__
,
1032 EXPORT_SYMBOL_GPL(ps3_lpm_copy_tb_to_user
);
1035 * ps3_get_and_clear_pm_interrupts -
1037 * Clearing interrupts for the entire performance monitoring unit.
1038 * Reading pm_status clears the interrupt bits.
1041 u32
ps3_get_and_clear_pm_interrupts(u32 cpu
)
1043 return ps3_read_pm(cpu
, pm_status
);
1045 EXPORT_SYMBOL_GPL(ps3_get_and_clear_pm_interrupts
);
1048 * ps3_enable_pm_interrupts -
1050 * Enabling interrupts for the entire performance monitoring unit.
1051 * Enables the interrupt bits in the pm_status register.
1054 void ps3_enable_pm_interrupts(u32 cpu
, u32 thread
, u32 mask
)
1057 ps3_write_pm(cpu
, pm_status
, mask
);
1059 EXPORT_SYMBOL_GPL(ps3_enable_pm_interrupts
);
1062 * ps3_enable_pm_interrupts -
1064 * Disabling interrupts for the entire performance monitoring unit.
1067 void ps3_disable_pm_interrupts(u32 cpu
)
1069 ps3_get_and_clear_pm_interrupts(cpu
);
1070 ps3_write_pm(cpu
, pm_status
, 0);
1072 EXPORT_SYMBOL_GPL(ps3_disable_pm_interrupts
);
1075 * ps3_lpm_open - Open the logical performance monitor device.
1076 * @tb_type: Specifies the type of trace buffer lv1 sould use for this lpm
1077 * instance, specified by one of enum ps3_lpm_tb_type.
1078 * @tb_cache: Optional user supplied buffer to use as the trace buffer cache.
1079 * If NULL, the driver will allocate and manage an internal buffer.
1080 * Unused when when @tb_type is PS3_LPM_TB_TYPE_NONE.
1081 * @tb_cache_size: The size in bytes of the user supplied @tb_cache buffer.
1082 * Unused when @tb_cache is NULL or @tb_type is PS3_LPM_TB_TYPE_NONE.
1085 int ps3_lpm_open(enum ps3_lpm_tb_type tb_type
, void *tb_cache
,
1092 BUG_ON(tb_type
!= PS3_LPM_TB_TYPE_NONE
1093 && tb_type
!= PS3_LPM_TB_TYPE_INTERNAL
);
1095 if (tb_type
== PS3_LPM_TB_TYPE_NONE
&& tb_cache
)
1096 dev_dbg(sbd_core(), "%s:%u: bad in vals\n", __func__
, __LINE__
);
1098 if (!atomic_add_unless(&lpm_priv
->open
, 1, 1)) {
1099 dev_dbg(sbd_core(), "%s:%u: busy\n", __func__
, __LINE__
);
1103 /* Note tb_cache needs 128 byte alignment. */
1105 if (tb_type
== PS3_LPM_TB_TYPE_NONE
) {
1106 lpm_priv
->tb_cache_size
= 0;
1107 lpm_priv
->tb_cache_internal
= NULL
;
1108 lpm_priv
->tb_cache
= NULL
;
1109 } else if (tb_cache
) {
1110 if (tb_cache
!= (void *)_ALIGN_UP((unsigned long)tb_cache
, 128)
1111 || tb_cache_size
!= _ALIGN_UP(tb_cache_size
, 128)) {
1112 dev_err(sbd_core(), "%s:%u: unaligned tb_cache\n",
1113 __func__
, __LINE__
);
1117 lpm_priv
->tb_cache_size
= tb_cache_size
;
1118 lpm_priv
->tb_cache_internal
= NULL
;
1119 lpm_priv
->tb_cache
= tb_cache
;
1121 lpm_priv
->tb_cache_size
= PS3_LPM_DEFAULT_TB_CACHE_SIZE
;
1122 lpm_priv
->tb_cache_internal
= kzalloc(
1123 lpm_priv
->tb_cache_size
+ 127, GFP_KERNEL
);
1124 if (!lpm_priv
->tb_cache_internal
) {
1125 dev_err(sbd_core(), "%s:%u: alloc internal tb_cache "
1126 "failed\n", __func__
, __LINE__
);
1130 lpm_priv
->tb_cache
= (void *)_ALIGN_UP(
1131 (unsigned long)lpm_priv
->tb_cache_internal
, 128);
1134 result
= lv1_construct_lpm(lpm_priv
->node_id
, tb_type
, 0, 0,
1135 ps3_mm_phys_to_lpar(__pa(lpm_priv
->tb_cache
)),
1136 lpm_priv
->tb_cache_size
, &lpm_priv
->lpm_id
,
1137 &lpm_priv
->outlet_id
, &tb_size
);
1140 dev_err(sbd_core(), "%s:%u: lv1_construct_lpm failed: %s\n",
1141 __func__
, __LINE__
, ps3_result(result
));
1143 goto fail_construct
;
1146 lpm_priv
->shadow
.pm_control
= PS3_LPM_SHADOW_REG_INIT
;
1147 lpm_priv
->shadow
.pm_start_stop
= PS3_LPM_SHADOW_REG_INIT
;
1148 lpm_priv
->shadow
.group_control
= PS3_LPM_SHADOW_REG_INIT
;
1149 lpm_priv
->shadow
.debug_bus_control
= PS3_LPM_SHADOW_REG_INIT
;
1151 dev_dbg(sbd_core(), "%s:%u: lpm_id 0x%llx, outlet_id 0x%llx, "
1152 "tb_size 0x%llx\n", __func__
, __LINE__
, lpm_priv
->lpm_id
,
1153 lpm_priv
->outlet_id
, tb_size
);
1158 kfree(lpm_priv
->tb_cache_internal
);
1159 lpm_priv
->tb_cache_internal
= NULL
;
1162 atomic_dec(&lpm_priv
->open
);
1165 EXPORT_SYMBOL_GPL(ps3_lpm_open
);
1168 * ps3_lpm_close - Close the lpm device.
1172 int ps3_lpm_close(void)
1174 dev_dbg(sbd_core(), "%s:%u\n", __func__
, __LINE__
);
1176 lv1_destruct_lpm(lpm_priv
->lpm_id
);
1177 lpm_priv
->lpm_id
= 0;
1179 kfree(lpm_priv
->tb_cache_internal
);
1180 lpm_priv
->tb_cache_internal
= NULL
;
1182 atomic_dec(&lpm_priv
->open
);
1185 EXPORT_SYMBOL_GPL(ps3_lpm_close
);
1187 static int __devinit
ps3_lpm_probe(struct ps3_system_bus_device
*dev
)
1189 dev_dbg(&dev
->core
, " -> %s:%u\n", __func__
, __LINE__
);
1192 dev_info(&dev
->core
, "%s:%u: called twice\n",
1193 __func__
, __LINE__
);
1197 lpm_priv
= kzalloc(sizeof(*lpm_priv
), GFP_KERNEL
);
1202 lpm_priv
->sbd
= dev
;
1203 lpm_priv
->node_id
= dev
->lpm
.node_id
;
1204 lpm_priv
->pu_id
= dev
->lpm
.pu_id
;
1205 lpm_priv
->rights
= dev
->lpm
.rights
;
1207 dev_info(&dev
->core
, " <- %s:%u:\n", __func__
, __LINE__
);
1212 static int ps3_lpm_remove(struct ps3_system_bus_device
*dev
)
1214 dev_dbg(&dev
->core
, " -> %s:%u:\n", __func__
, __LINE__
);
1221 dev_info(&dev
->core
, " <- %s:%u:\n", __func__
, __LINE__
);
1225 static struct ps3_system_bus_driver ps3_lpm_driver
= {
1226 .match_id
= PS3_MATCH_ID_LPM
,
1227 .core
.name
= "ps3-lpm",
1228 .core
.owner
= THIS_MODULE
,
1229 .probe
= ps3_lpm_probe
,
1230 .remove
= ps3_lpm_remove
,
1231 .shutdown
= ps3_lpm_remove
,
1234 static int __init
ps3_lpm_init(void)
1236 pr_debug("%s:%d:\n", __func__
, __LINE__
);
1237 return ps3_system_bus_driver_register(&ps3_lpm_driver
);
1240 static void __exit
ps3_lpm_exit(void)
1242 pr_debug("%s:%d:\n", __func__
, __LINE__
);
1243 ps3_system_bus_driver_unregister(&ps3_lpm_driver
);
1246 module_init(ps3_lpm_init
);
1247 module_exit(ps3_lpm_exit
);
1249 MODULE_LICENSE("GPL v2");
1250 MODULE_DESCRIPTION("PS3 Logical Performance Monitor Driver");
1251 MODULE_AUTHOR("Sony Corporation");
1252 MODULE_ALIAS(PS3_MODULE_ALIAS_LPM
);