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 * @pm_interval: Shadow of the processor's pm_interval 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 {
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("or 29, 29, 29;"); /* db10cyc */
185 mtspr(SPRN_BKMK, bookmark);
186 asm volatile("or 29, 29, 29;"); /* db10cyc */
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 return lpm_priv->shadow.pm_interval;
413 return lpm_priv->shadow.group_control;
414 case debug_bus_control:
415 return lpm_priv->shadow.debug_bus_control;
417 result = lv1_get_lpm_interrupt_status(lpm_priv->lpm_id,
421 dev_dbg(sbd_core(), "%s:%u: lv1 get_lpm_status failed: "
422 "reg %u, %s\n", __func__, __LINE__, reg,
429 dev_dbg(sbd_core(), "%s:%u: unknown reg: %d\n", __func__,
437 EXPORT_SYMBOL_GPL(ps3_read_pm);
440 * ps3_write_pm - Write Other LPM control registers.
443 void ps3_write_pm(u32 cpu, enum pm_reg_name reg, u32 val)
450 if (val != lpm_priv->shadow.group_control)
451 result = lv1_set_lpm_group_control(lpm_priv->lpm_id,
455 lpm_priv->shadow.group_control = val;
457 case debug_bus_control:
458 if (val != lpm_priv->shadow.debug_bus_control)
459 result = lv1_set_lpm_debug_bus_control(lpm_priv->lpm_id,
463 lpm_priv->shadow.debug_bus_control = val;
466 if (use_start_stop_bookmark)
467 val |= (PS3_PM_CONTROL_PPU_TH0_BOOKMARK |
468 PS3_PM_CONTROL_PPU_TH1_BOOKMARK);
469 if (val != lpm_priv->shadow.pm_control)
470 result = lv1_set_lpm_general_control(lpm_priv->lpm_id,
475 lpm_priv->shadow.pm_control = val;
478 if (val != lpm_priv->shadow.pm_interval)
479 result = lv1_set_lpm_interval(lpm_priv->lpm_id, val,
480 PS3_WRITE_PM_MASK, &dummy);
481 lpm_priv->shadow.pm_interval = val;
484 if (val != lpm_priv->shadow.pm_start_stop)
485 result = lv1_set_lpm_trigger_control(lpm_priv->lpm_id,
489 lpm_priv->shadow.pm_start_stop = val;
496 dev_dbg(sbd_core(), "%s:%u: unknown reg: %d\n", __func__,
503 dev_err(sbd_core(), "%s:%u: lv1 set_control failed: "
504 "reg %u, %s\n", __func__, __LINE__, reg,
507 EXPORT_SYMBOL_GPL(ps3_write_pm);
510 * ps3_get_ctr_size - Get the size of a physical counter.
512 * Returns either 16 or 32.
515 u32 ps3_get_ctr_size(u32 cpu, u32 phys_ctr)
519 if (phys_ctr >= NR_PHYS_CTRS) {
520 dev_dbg(sbd_core(), "%s:%u: phys_ctr too big: %u\n", __func__,
525 pm_ctrl = ps3_read_pm(cpu, pm_control);
526 return (pm_ctrl & CBE_PM_16BIT_CTR(phys_ctr)) ? 16 : 32;
528 EXPORT_SYMBOL_GPL(ps3_get_ctr_size);
531 * ps3_set_ctr_size - Set the size of a physical counter to 16 or 32 bits.
534 void ps3_set_ctr_size(u32 cpu, u32 phys_ctr, u32 ctr_size)
538 if (phys_ctr >= NR_PHYS_CTRS) {
539 dev_dbg(sbd_core(), "%s:%u: phys_ctr too big: %u\n", __func__,
544 pm_ctrl = ps3_read_pm(cpu, pm_control);
548 pm_ctrl |= CBE_PM_16BIT_CTR(phys_ctr);
549 ps3_write_pm(cpu, pm_control, pm_ctrl);
553 pm_ctrl &= ~CBE_PM_16BIT_CTR(phys_ctr);
554 ps3_write_pm(cpu, pm_control, pm_ctrl);
560 EXPORT_SYMBOL_GPL(ps3_set_ctr_size);
562 static u64 pm_translate_signal_group_number_on_island2(u64 subgroup)
569 return PM_ISLAND2_BASE_SIGNAL_GROUP_NUMBER + subgroup;
570 else if (subgroup == 7)
571 return PM_ISLAND2_SIGNAL_GROUP_NUMBER1;
573 return PM_ISLAND2_SIGNAL_GROUP_NUMBER2;
576 static u64 pm_translate_signal_group_number_on_island3(u64 subgroup)
591 return PM_ISLAND3_BASE_SIGNAL_GROUP_NUMBER + subgroup;
594 static u64 pm_translate_signal_group_number_on_island4(u64 subgroup)
596 return PM_ISLAND4_BASE_SIGNAL_GROUP_NUMBER + subgroup;
599 static u64 pm_translate_signal_group_number_on_island5(u64 subgroup)
612 return PM_ISLAND5_BASE_SIGNAL_GROUP_NUMBER + subgroup;
615 static u64 pm_translate_signal_group_number_on_island6(u64 subgroup,
628 switch (subsubgroup) {
650 return (PM_ISLAND6_BASE_SIGNAL_GROUP_NUMBER + subgroup);
652 return (PM_ISLAND6_BASE_SIGNAL_GROUP_NUMBER + subgroup
656 static u64 pm_translate_signal_group_number_on_island7(u64 subgroup)
658 return PM_ISLAND7_BASE_SIGNAL_GROUP_NUMBER + subgroup;
661 static u64 pm_translate_signal_group_number_on_island8(u64 subgroup)
663 return PM_ISLAND8_BASE_SIGNAL_GROUP_NUMBER + subgroup;
666 static u64 pm_signal_group_to_ps3_lv1_signal_group(u64 group)
677 if (20 <= group && group < 30) {
679 subgroup = group - 20;
680 } else if (30 <= group && group < 40) {
682 subgroup = group - 30;
683 } else if (40 <= group && group < 50) {
685 subgroup = group - 40;
686 } else if (50 <= group && group < 60) {
688 subgroup = group - 50;
689 } else if (60 <= group && group < 70) {
691 subgroup = group - 60;
692 } else if (70 <= group && group < 80) {
694 subgroup = group - 70;
695 } else if (80 <= group && group < 90) {
697 subgroup = group - 80;
699 } else if (200 <= group && group < 300) {
701 subgroup = group - 200;
702 } else if (600 <= group && group < 700) {
705 subsubgroup = group - 650;
707 } else if (6000 <= group && group < 7000) {
710 subsubgroup = group - 6500;
715 return pm_translate_signal_group_number_on_island2(subgroup);
717 return pm_translate_signal_group_number_on_island3(subgroup);
719 return pm_translate_signal_group_number_on_island4(subgroup);
721 return pm_translate_signal_group_number_on_island5(subgroup);
723 return pm_translate_signal_group_number_on_island6(subgroup,
726 return pm_translate_signal_group_number_on_island7(subgroup);
728 return pm_translate_signal_group_number_on_island8(subgroup);
730 dev_dbg(sbd_core(), "%s:%u: island not found: %lu\n", __func__,
738 static u64 pm_bus_word_to_ps3_lv1_bus_word(u8 word)
754 static int __ps3_set_signal(u64 lv1_signal_group, u64 bus_select,
755 u64 signal_select, u64 attr1, u64 attr2, u64 attr3)
759 ret = lv1_set_lpm_signal(lpm_priv->lpm_id, lv1_signal_group, bus_select,
760 signal_select, attr1, attr2, attr3);
763 "%s:%u: error:%d 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
764 __func__, __LINE__, ret, lv1_signal_group, bus_select,
765 signal_select, attr1, attr2, attr3);
770 int ps3_set_signal(u64 signal_group, u8 signal_bit, u16 sub_unit,
774 u64 lv1_signal_group;
777 u64 attr1, attr2, attr3;
779 if (signal_group == 0)
780 return __ps3_set_signal(0, 0, 0, 0, 0, 0);
783 pm_signal_group_to_ps3_lv1_signal_group(signal_group);
784 bus_select = pm_bus_word_to_ps3_lv1_bus_word(bus_word);
786 switch (signal_group) {
787 case PM_SIG_GROUP_SPU_TRIGGER:
789 signal_select = signal_select << (63 - signal_bit);
791 case PM_SIG_GROUP_SPU_EVENT:
793 signal_select = (signal_select << (63 - signal_bit)) | 0x3;
801 * 0: physical object.
803 * This parameter is only used for the PPE and SPE signals.
808 * This parameter is used to specify the target physical/logical
811 if (PM_SIG_GROUP_SPU <= signal_group &&
812 signal_group < PM_SIG_GROUP_MFC_MAX)
815 attr2 = lpm_priv->pu_id;
818 * This parameter is only used for setting the SPE signal.
822 ret = __ps3_set_signal(lv1_signal_group, bus_select, signal_select,
823 attr1, attr2, attr3);
825 dev_err(sbd_core(), "%s:%u: __ps3_set_signal failed: %d\n",
826 __func__, __LINE__, ret);
830 EXPORT_SYMBOL_GPL(ps3_set_signal);
832 u32 ps3_get_hw_thread_id(int cpu)
834 return get_hard_smp_processor_id(cpu);
836 EXPORT_SYMBOL_GPL(ps3_get_hw_thread_id);
839 * ps3_enable_pm - Enable the entire performance monitoring unit.
841 * When we enable the LPM, all pending writes to counters get committed.
844 void ps3_enable_pm(u32 cpu)
848 int insert_bookmark = 0;
850 lpm_priv->tb_count = 0;
852 if (use_start_stop_bookmark) {
853 if (!(lpm_priv->shadow.pm_start_stop &
854 (PS3_PM_START_STOP_START_MASK
855 | PS3_PM_START_STOP_STOP_MASK))) {
856 result = lv1_set_lpm_trigger_control(lpm_priv->lpm_id,
857 (PS3_PM_START_STOP_PPU_TH0_BOOKMARK_START |
858 PS3_PM_START_STOP_PPU_TH1_BOOKMARK_START |
859 PS3_PM_START_STOP_PPU_TH0_BOOKMARK_STOP |
860 PS3_PM_START_STOP_PPU_TH1_BOOKMARK_STOP),
861 0xFFFFFFFFFFFFFFFFULL, &tmp);
864 dev_err(sbd_core(), "%s:%u: "
865 "lv1_set_lpm_trigger_control failed: "
866 "%s\n", __func__, __LINE__,
869 insert_bookmark = !result;
873 result = lv1_start_lpm(lpm_priv->lpm_id);
876 dev_err(sbd_core(), "%s:%u: lv1_start_lpm failed: %s\n",
877 __func__, __LINE__, ps3_result(result));
879 if (use_start_stop_bookmark && !result && insert_bookmark)
880 ps3_set_bookmark(get_tb() | PS3_PM_BOOKMARK_START);
882 EXPORT_SYMBOL_GPL(ps3_enable_pm);
885 * ps3_disable_pm - Disable the entire performance monitoring unit.
888 void ps3_disable_pm(u32 cpu)
893 ps3_set_bookmark(get_tb() | PS3_PM_BOOKMARK_STOP);
895 result = lv1_stop_lpm(lpm_priv->lpm_id, &tmp);
898 if(result != LV1_WRONG_STATE)
899 dev_err(sbd_core(), "%s:%u: lv1_stop_lpm failed: %s\n",
900 __func__, __LINE__, ps3_result(result));
904 lpm_priv->tb_count = tmp;
906 dev_dbg(sbd_core(), "%s:%u: tb_count %lu (%lxh)\n", __func__, __LINE__,
907 lpm_priv->tb_count, lpm_priv->tb_count);
909 EXPORT_SYMBOL_GPL(ps3_disable_pm);
912 * ps3_lpm_copy_tb - Copy data from the trace buffer to a kernel buffer.
913 * @offset: Offset in bytes from the start of the trace buffer.
914 * @buf: Copy destination.
915 * @count: Maximum count of bytes to copy.
916 * @bytes_copied: Pointer to a variable that will recieve the number of
917 * bytes copied to @buf.
919 * On error @buf will contain any successfully copied trace buffer data
920 * and bytes_copied will be set to the number of bytes successfully copied.
923 int ps3_lpm_copy_tb(unsigned long offset, void *buf, unsigned long count,
924 unsigned long *bytes_copied)
930 if (!lpm_priv->tb_cache)
933 if (offset >= lpm_priv->tb_count)
936 count = min(count, lpm_priv->tb_count - offset);
938 while (*bytes_copied < count) {
939 const unsigned long request = count - *bytes_copied;
942 result = lv1_copy_lpm_trace_buffer(lpm_priv->lpm_id, offset,
945 dev_dbg(sbd_core(), "%s:%u: 0x%lx bytes at 0x%lx\n",
946 __func__, __LINE__, request, offset);
948 dev_err(sbd_core(), "%s:%u: lv1_copy_lpm_trace_buffer "
949 "failed: %s\n", __func__, __LINE__,
951 return result == LV1_WRONG_STATE ? -EBUSY : -EINVAL;
954 memcpy(buf, lpm_priv->tb_cache, tmp);
956 *bytes_copied += tmp;
959 dev_dbg(sbd_core(), "%s:%u: copied %lxh bytes\n", __func__, __LINE__,
964 EXPORT_SYMBOL_GPL(ps3_lpm_copy_tb);
967 * ps3_lpm_copy_tb_to_user - Copy data from the trace buffer to a user buffer.
968 * @offset: Offset in bytes from the start of the trace buffer.
969 * @buf: A __user copy destination.
970 * @count: Maximum count of bytes to copy.
971 * @bytes_copied: Pointer to a variable that will recieve the number of
972 * bytes copied to @buf.
974 * On error @buf will contain any successfully copied trace buffer data
975 * and bytes_copied will be set to the number of bytes successfully copied.
978 int ps3_lpm_copy_tb_to_user(unsigned long offset, void __user *buf,
979 unsigned long count, unsigned long *bytes_copied)
985 if (!lpm_priv->tb_cache)
988 if (offset >= lpm_priv->tb_count)
991 count = min(count, lpm_priv->tb_count - offset);
993 while (*bytes_copied < count) {
994 const unsigned long request = count - *bytes_copied;
997 result = lv1_copy_lpm_trace_buffer(lpm_priv->lpm_id, offset,
1000 dev_dbg(sbd_core(), "%s:%u: 0x%lx bytes at 0x%lx\n",
1001 __func__, __LINE__, request, offset);
1002 dev_err(sbd_core(), "%s:%u: lv1_copy_lpm_trace_buffer "
1003 "failed: %s\n", __func__, __LINE__,
1004 ps3_result(result));
1005 return result == LV1_WRONG_STATE ? -EBUSY : -EINVAL;
1008 result = copy_to_user(buf, lpm_priv->tb_cache, tmp);
1011 dev_dbg(sbd_core(), "%s:%u: 0x%lx bytes at 0x%p\n",
1012 __func__, __LINE__, tmp, buf);
1013 dev_err(sbd_core(), "%s:%u: copy_to_user failed: %d\n",
1014 __func__, __LINE__, result);
1019 *bytes_copied += tmp;
1022 dev_dbg(sbd_core(), "%s:%u: copied %lxh bytes\n", __func__, __LINE__,
1027 EXPORT_SYMBOL_GPL(ps3_lpm_copy_tb_to_user);
1030 * ps3_get_and_clear_pm_interrupts -
1032 * Clearing interrupts for the entire performance monitoring unit.
1033 * Reading pm_status clears the interrupt bits.
1036 u32 ps3_get_and_clear_pm_interrupts(u32 cpu)
1038 return ps3_read_pm(cpu, pm_status);
1040 EXPORT_SYMBOL_GPL(ps3_get_and_clear_pm_interrupts);
1043 * ps3_enable_pm_interrupts -
1045 * Enabling interrupts for the entire performance monitoring unit.
1046 * Enables the interrupt bits in the pm_status register.
1049 void ps3_enable_pm_interrupts(u32 cpu, u32 thread, u32 mask)
1052 ps3_write_pm(cpu, pm_status, mask);
1054 EXPORT_SYMBOL_GPL(ps3_enable_pm_interrupts);
1057 * ps3_enable_pm_interrupts -
1059 * Disabling interrupts for the entire performance monitoring unit.
1062 void ps3_disable_pm_interrupts(u32 cpu)
1064 ps3_get_and_clear_pm_interrupts(cpu);
1065 ps3_write_pm(cpu, pm_status, 0);
1067 EXPORT_SYMBOL_GPL(ps3_disable_pm_interrupts);
1070 * ps3_lpm_open - Open the logical performance monitor device.
1071 * @tb_type: Specifies the type of trace buffer lv1 sould use for this lpm
1072 * instance, specified by one of enum ps3_lpm_tb_type.
1073 * @tb_cache: Optional user supplied buffer to use as the trace buffer cache.
1074 * If NULL, the driver will allocate and manage an internal buffer.
1075 * Unused when when @tb_type is PS3_LPM_TB_TYPE_NONE.
1076 * @tb_cache_size: The size in bytes of the user supplied @tb_cache buffer.
1077 * Unused when @tb_cache is NULL or @tb_type is PS3_LPM_TB_TYPE_NONE.
1080 int ps3_lpm_open(enum ps3_lpm_tb_type tb_type, void *tb_cache,
1087 BUG_ON(tb_type != PS3_LPM_TB_TYPE_NONE
1088 && tb_type != PS3_LPM_TB_TYPE_INTERNAL);
1090 if (tb_type == PS3_LPM_TB_TYPE_NONE && tb_cache)
1091 dev_dbg(sbd_core(), "%s:%u: bad in vals\n", __func__, __LINE__);
1093 if (!atomic_add_unless(&lpm_priv->open, 1, 1)) {
1094 dev_dbg(sbd_core(), "%s:%u: busy\n", __func__, __LINE__);
1098 /* Note tb_cache needs 128 byte alignment. */
1100 if (tb_type == PS3_LPM_TB_TYPE_NONE) {
1101 lpm_priv->tb_cache_size = 0;
1102 lpm_priv->tb_cache_internal = NULL;
1103 lpm_priv->tb_cache = NULL;
1104 } else if (tb_cache) {
1105 if (tb_cache != (void *)_ALIGN_UP((unsigned long)tb_cache, 128)
1106 || tb_cache_size != _ALIGN_UP(tb_cache_size, 128)) {
1107 dev_err(sbd_core(), "%s:%u: unaligned tb_cache\n",
1108 __func__, __LINE__);
1112 lpm_priv->tb_cache_size = tb_cache_size;
1113 lpm_priv->tb_cache_internal = NULL;
1114 lpm_priv->tb_cache = tb_cache;
1116 lpm_priv->tb_cache_size = PS3_LPM_DEFAULT_TB_CACHE_SIZE;
1117 lpm_priv->tb_cache_internal = kzalloc(
1118 lpm_priv->tb_cache_size + 127, GFP_KERNEL);
1119 if (!lpm_priv->tb_cache_internal) {
1120 dev_err(sbd_core(), "%s:%u: alloc internal tb_cache "
1121 "failed\n", __func__, __LINE__);
1125 lpm_priv->tb_cache = (void *)_ALIGN_UP(
1126 (unsigned long)lpm_priv->tb_cache_internal, 128);
1129 result = lv1_construct_lpm(lpm_priv->node_id, tb_type, 0, 0,
1130 ps3_mm_phys_to_lpar(__pa(lpm_priv->tb_cache)),
1131 lpm_priv->tb_cache_size, &lpm_priv->lpm_id,
1132 &lpm_priv->outlet_id, &tb_size);
1135 dev_err(sbd_core(), "%s:%u: lv1_construct_lpm failed: %s\n",
1136 __func__, __LINE__, ps3_result(result));
1138 goto fail_construct;
1141 lpm_priv->shadow.pm_control = PS3_LPM_SHADOW_REG_INIT;
1142 lpm_priv->shadow.pm_start_stop = PS3_LPM_SHADOW_REG_INIT;
1143 lpm_priv->shadow.pm_interval = PS3_LPM_SHADOW_REG_INIT;
1144 lpm_priv->shadow.group_control = PS3_LPM_SHADOW_REG_INIT;
1145 lpm_priv->shadow.debug_bus_control = PS3_LPM_SHADOW_REG_INIT;
1147 dev_dbg(sbd_core(), "%s:%u: lpm_id 0x%lx, outlet_id 0x%lx, "
1148 "tb_size 0x%lx\n", __func__, __LINE__, lpm_priv->lpm_id,
1149 lpm_priv->outlet_id, tb_size);
1154 kfree(lpm_priv->tb_cache_internal);
1155 lpm_priv->tb_cache_internal = NULL;
1158 atomic_dec(&lpm_priv->open);
1161 EXPORT_SYMBOL_GPL(ps3_lpm_open);
1164 * ps3_lpm_close - Close the lpm device.
1168 int ps3_lpm_close(void)
1170 dev_dbg(sbd_core(), "%s:%u\n", __func__, __LINE__);
1172 lv1_destruct_lpm(lpm_priv->lpm_id);
1173 lpm_priv->lpm_id = 0;
1175 kfree(lpm_priv->tb_cache_internal);
1176 lpm_priv->tb_cache_internal = NULL;
1178 atomic_dec(&lpm_priv->open);
1181 EXPORT_SYMBOL_GPL(ps3_lpm_close);
1183 static int __devinit ps3_lpm_probe(struct ps3_system_bus_device *dev)
1185 dev_dbg(&dev->core, " -> %s:%u\n", __func__, __LINE__);
1188 dev_info(&dev->core, "%s:%u: called twice\n",
1189 __func__, __LINE__);
1193 lpm_priv = kzalloc(sizeof(*lpm_priv), GFP_KERNEL);
1198 lpm_priv->sbd = dev;
1199 lpm_priv->node_id = dev->lpm.node_id;
1200 lpm_priv->pu_id = dev->lpm.pu_id;
1201 lpm_priv->rights = dev->lpm.rights;
1203 dev_info(&dev->core, " <- %s:%u:\n", __func__, __LINE__);
1208 static int ps3_lpm_remove(struct ps3_system_bus_device *dev)
1210 dev_dbg(&dev->core, " -> %s:%u:\n", __func__, __LINE__);
1217 dev_info(&dev->core, " <- %s:%u:\n", __func__, __LINE__);
1221 static struct ps3_system_bus_driver ps3_lpm_driver = {
1222 .match_id = PS3_MATCH_ID_LPM,
1223 .core.name = "ps3-lpm",
1224 .core.owner = THIS_MODULE,
1225 .probe = ps3_lpm_probe,
1226 .remove = ps3_lpm_remove,
1227 .shutdown = ps3_lpm_remove,
1230 static int __init ps3_lpm_init(void)
1232 pr_debug("%s:%d:\n", __func__, __LINE__);
1233 return ps3_system_bus_driver_register(&ps3_lpm_driver);
1236 static void __exit ps3_lpm_exit(void)
1238 pr_debug("%s:%d:\n", __func__, __LINE__);
1239 ps3_system_bus_driver_unregister(&ps3_lpm_driver);
1242 module_init(ps3_lpm_init);
1243 module_exit(ps3_lpm_exit);
1245 MODULE_LICENSE("GPL v2");
1246 MODULE_DESCRIPTION("PS3 Logical Performance Monitor Driver");
1247 MODULE_AUTHOR("Sony Corporation");
1248 MODULE_ALIAS(PS3_MODULE_ALIAS_LPM);