1 // SPDX-License-Identifier: GPL-2.0-only
3 * PS3 Logical Performance Monitor.
5 * Copyright (C) 2007 Sony Computer Entertainment Inc.
6 * Copyright 2007 Sony Corp.
9 #include <linux/slab.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/uaccess.h>
17 #include <asm/lv1call.h>
18 #include <asm/cell-pmu.h>
21 /* BOOKMARK tag macros */
22 #define PS3_PM_BOOKMARK_START 0x8000000000000000ULL
23 #define PS3_PM_BOOKMARK_STOP 0x4000000000000000ULL
24 #define PS3_PM_BOOKMARK_TAG_KERNEL 0x1000000000000000ULL
25 #define PS3_PM_BOOKMARK_TAG_USER 0x3000000000000000ULL
26 #define PS3_PM_BOOKMARK_TAG_MASK_HI 0xF000000000000000ULL
27 #define PS3_PM_BOOKMARK_TAG_MASK_LO 0x0F00000000000000ULL
29 /* CBE PM CONTROL register macros */
30 #define PS3_PM_CONTROL_PPU_TH0_BOOKMARK 0x00001000
31 #define PS3_PM_CONTROL_PPU_TH1_BOOKMARK 0x00000800
32 #define PS3_PM_CONTROL_PPU_COUNT_MODE_MASK 0x000C0000
33 #define PS3_PM_CONTROL_PPU_COUNT_MODE_PROBLEM 0x00080000
34 #define PS3_WRITE_PM_MASK 0xFFFFFFFFFFFFFFFFULL
36 /* CBE PM START STOP register macros */
37 #define PS3_PM_START_STOP_PPU_TH0_BOOKMARK_START 0x02000000
38 #define PS3_PM_START_STOP_PPU_TH1_BOOKMARK_START 0x01000000
39 #define PS3_PM_START_STOP_PPU_TH0_BOOKMARK_STOP 0x00020000
40 #define PS3_PM_START_STOP_PPU_TH1_BOOKMARK_STOP 0x00010000
41 #define PS3_PM_START_STOP_START_MASK 0xFF000000
42 #define PS3_PM_START_STOP_STOP_MASK 0x00FF0000
44 /* CBE PM COUNTER register macres */
45 #define PS3_PM_COUNTER_MASK_HI 0xFFFFFFFF00000000ULL
46 #define PS3_PM_COUNTER_MASK_LO 0x00000000FFFFFFFFULL
48 /* BASE SIGNAL GROUP NUMBER macros */
49 #define PM_ISLAND2_BASE_SIGNAL_GROUP_NUMBER 0
50 #define PM_ISLAND2_SIGNAL_GROUP_NUMBER1 6
51 #define PM_ISLAND2_SIGNAL_GROUP_NUMBER2 7
52 #define PM_ISLAND3_BASE_SIGNAL_GROUP_NUMBER 7
53 #define PM_ISLAND4_BASE_SIGNAL_GROUP_NUMBER 15
54 #define PM_SPU_TRIGGER_SIGNAL_GROUP_NUMBER 17
55 #define PM_SPU_EVENT_SIGNAL_GROUP_NUMBER 18
56 #define PM_ISLAND5_BASE_SIGNAL_GROUP_NUMBER 18
57 #define PM_ISLAND6_BASE_SIGNAL_GROUP_NUMBER 24
58 #define PM_ISLAND7_BASE_SIGNAL_GROUP_NUMBER 49
59 #define PM_ISLAND8_BASE_SIGNAL_GROUP_NUMBER 52
60 #define PM_SIG_GROUP_SPU 41
61 #define PM_SIG_GROUP_SPU_TRIGGER 42
62 #define PM_SIG_GROUP_SPU_EVENT 43
63 #define PM_SIG_GROUP_MFC_MAX 60
66 * struct ps3_lpm_shadow_regs - Performance monitor shadow registers.
68 * @pm_control: Shadow of the processor's pm_control register.
69 * @pm_start_stop: Shadow of the processor's pm_start_stop register.
70 * @group_control: Shadow of the processor's group_control register.
71 * @debug_bus_control: Shadow of the processor's debug_bus_control register.
73 * The logical performance monitor provides a write-only interface to
74 * these processor registers. These shadow variables cache the processor
75 * register values for reading.
77 * The initial value of the shadow registers at lpm creation is
78 * PS3_LPM_SHADOW_REG_INIT.
81 struct ps3_lpm_shadow_regs {
85 u64 debug_bus_control;
88 #define PS3_LPM_SHADOW_REG_INIT 0xFFFFFFFF00000000ULL
91 * struct ps3_lpm_priv - Private lpm device data.
93 * @open: An atomic variable indicating the lpm driver has been opened.
94 * @rights: The lpm rigths granted by the system policy module. A logical
95 * OR of enum ps3_lpm_rights.
96 * @node_id: The node id of a BE processor whose performance monitor this
97 * lpar has the right to use.
98 * @pu_id: The lv1 id of the logical PU.
99 * @lpm_id: The lv1 id of this lpm instance.
100 * @outlet_id: The outlet created by lv1 for this lpm instance.
101 * @tb_count: The number of bytes of data held in the lv1 trace buffer.
102 * @tb_cache: Kernel buffer to receive the data from the lv1 trace buffer.
103 * Must be 128 byte aligned.
104 * @tb_cache_size: Size of the kernel @tb_cache buffer. Must be 128 byte
106 * @tb_cache_internal: An unaligned buffer allocated by this driver to be
107 * used for the trace buffer cache when ps3_lpm_open() is called with a
108 * NULL tb_cache argument. Otherwise unused.
109 * @shadow: Processor register shadow of type struct ps3_lpm_shadow_regs.
110 * @sbd: The struct ps3_system_bus_device attached to this driver.
112 * The trace buffer is a buffer allocated and used internally to the lv1
113 * hypervisor to collect trace data. The trace buffer cache is a guest
114 * buffer that accepts the trace data from the trace buffer.
117 struct ps3_lpm_priv {
127 void *tb_cache_internal;
128 struct ps3_lpm_shadow_regs shadow;
129 struct ps3_system_bus_device *sbd;
133 PS3_LPM_DEFAULT_TB_CACHE_SIZE = 0x4000,
137 * lpm_priv - Static instance of the lpm data.
139 * Since the exported routines don't support the notion of a device
140 * instance we need to hold the instance in this static variable
141 * and then only allow at most one instance at a time to be created.
144 static struct ps3_lpm_priv *lpm_priv;
146 static struct device *sbd_core(void)
148 BUG_ON(!lpm_priv || !lpm_priv->sbd);
149 return &lpm_priv->sbd->core;
153 * use_start_stop_bookmark - Enable the PPU bookmark trace.
155 * And it enables PPU bookmark triggers ONLY if the other triggers are not set.
156 * The start/stop bookmarks are inserted at ps3_enable_pm() and ps3_disable_pm()
159 * Used to get good quality of the performance counter.
162 enum {use_start_stop_bookmark = 1,};
164 void ps3_set_bookmark(u64 bookmark)
167 * As per the PPE book IV, to avoid bookmark loss there must
168 * not be a traced branch within 10 cycles of setting the
169 * SPRN_BKMK register. The actual text is unclear if 'within'
170 * includes cycles before the call.
173 asm volatile("nop;nop;nop;nop;nop;nop;nop;nop;nop;");
174 mtspr(SPRN_BKMK, bookmark);
175 asm volatile("nop;nop;nop;nop;nop;nop;nop;nop;nop;");
177 EXPORT_SYMBOL_GPL(ps3_set_bookmark);
179 void ps3_set_pm_bookmark(u64 tag, u64 incident, u64 th_id)
183 bookmark = (get_tb() & 0x00000000FFFFFFFFULL) |
184 PS3_PM_BOOKMARK_TAG_KERNEL;
185 bookmark = ((tag << 56) & PS3_PM_BOOKMARK_TAG_MASK_LO) |
186 (incident << 48) | (th_id << 32) | bookmark;
187 ps3_set_bookmark(bookmark);
189 EXPORT_SYMBOL_GPL(ps3_set_pm_bookmark);
192 * ps3_read_phys_ctr - Read physical counter registers.
194 * Each physical counter can act as one 32 bit counter or as two 16 bit
198 u32 ps3_read_phys_ctr(u32 cpu, u32 phys_ctr)
204 if (phys_ctr >= NR_PHYS_CTRS) {
205 dev_dbg(sbd_core(), "%s:%u: phys_ctr too big: %u\n", __func__,
210 result = lv1_set_lpm_counter(lpm_priv->lpm_id, 0, 0, 0, 0, &counter0415,
213 dev_err(sbd_core(), "%s:%u: lv1_set_lpm_counter failed: "
214 "phys_ctr %u, %s\n", __func__, __LINE__, phys_ctr,
221 return counter0415 >> 32;
223 return counter0415 & PS3_PM_COUNTER_MASK_LO;
225 return counter2637 >> 32;
227 return counter2637 & PS3_PM_COUNTER_MASK_LO;
233 EXPORT_SYMBOL_GPL(ps3_read_phys_ctr);
236 * ps3_write_phys_ctr - Write physical counter registers.
238 * Each physical counter can act as one 32 bit counter or as two 16 bit
242 void ps3_write_phys_ctr(u32 cpu, u32 phys_ctr, u32 val)
245 u64 counter0415_mask;
247 u64 counter2637_mask;
250 if (phys_ctr >= NR_PHYS_CTRS) {
251 dev_dbg(sbd_core(), "%s:%u: phys_ctr too big: %u\n", __func__,
258 counter0415 = (u64)val << 32;
259 counter0415_mask = PS3_PM_COUNTER_MASK_HI;
261 counter2637_mask = 0x0;
264 counter0415 = (u64)val;
265 counter0415_mask = PS3_PM_COUNTER_MASK_LO;
267 counter2637_mask = 0x0;
271 counter0415_mask = 0x0;
272 counter2637 = (u64)val << 32;
273 counter2637_mask = PS3_PM_COUNTER_MASK_HI;
277 counter0415_mask = 0x0;
278 counter2637 = (u64)val;
279 counter2637_mask = PS3_PM_COUNTER_MASK_LO;
285 result = lv1_set_lpm_counter(lpm_priv->lpm_id,
286 counter0415, counter0415_mask,
287 counter2637, counter2637_mask,
288 &counter0415, &counter2637);
290 dev_err(sbd_core(), "%s:%u: lv1_set_lpm_counter failed: "
291 "phys_ctr %u, val %u, %s\n", __func__, __LINE__,
292 phys_ctr, val, ps3_result(result));
294 EXPORT_SYMBOL_GPL(ps3_write_phys_ctr);
297 * ps3_read_ctr - Read counter.
299 * Read 16 or 32 bits depending on the current size of the counter.
300 * Counters 4, 5, 6 & 7 are always 16 bit.
303 u32 ps3_read_ctr(u32 cpu, u32 ctr)
306 u32 phys_ctr = ctr & (NR_PHYS_CTRS - 1);
308 val = ps3_read_phys_ctr(cpu, phys_ctr);
310 if (ps3_get_ctr_size(cpu, phys_ctr) == 16)
311 val = (ctr < NR_PHYS_CTRS) ? (val >> 16) : (val & 0xffff);
315 EXPORT_SYMBOL_GPL(ps3_read_ctr);
318 * ps3_write_ctr - Write counter.
320 * Write 16 or 32 bits depending on the current size of the counter.
321 * Counters 4, 5, 6 & 7 are always 16 bit.
324 void ps3_write_ctr(u32 cpu, u32 ctr, u32 val)
329 phys_ctr = ctr & (NR_PHYS_CTRS - 1);
331 if (ps3_get_ctr_size(cpu, phys_ctr) == 16) {
332 phys_val = ps3_read_phys_ctr(cpu, phys_ctr);
334 if (ctr < NR_PHYS_CTRS)
335 val = (val << 16) | (phys_val & 0xffff);
337 val = (val & 0xffff) | (phys_val & 0xffff0000);
340 ps3_write_phys_ctr(cpu, phys_ctr, val);
342 EXPORT_SYMBOL_GPL(ps3_write_ctr);
345 * ps3_read_pm07_control - Read counter control registers.
347 * Each logical counter has a corresponding control register.
350 u32 ps3_read_pm07_control(u32 cpu, u32 ctr)
354 EXPORT_SYMBOL_GPL(ps3_read_pm07_control);
357 * ps3_write_pm07_control - Write counter control registers.
359 * Each logical counter has a corresponding control register.
362 void ps3_write_pm07_control(u32 cpu, u32 ctr, u32 val)
365 static const u64 mask = 0xFFFFFFFFFFFFFFFFULL;
368 if (ctr >= NR_CTRS) {
369 dev_dbg(sbd_core(), "%s:%u: ctr too big: %u\n", __func__,
374 result = lv1_set_lpm_counter_control(lpm_priv->lpm_id, ctr, val, mask,
377 dev_err(sbd_core(), "%s:%u: lv1_set_lpm_counter_control "
378 "failed: ctr %u, %s\n", __func__, __LINE__, ctr,
381 EXPORT_SYMBOL_GPL(ps3_write_pm07_control);
384 * ps3_read_pm - Read Other LPM control registers.
387 u32 ps3_read_pm(u32 cpu, enum pm_reg_name reg)
394 return lpm_priv->shadow.pm_control;
396 return CBE_PM_TRACE_BUF_EMPTY;
398 return lpm_priv->shadow.pm_start_stop;
400 result = lv1_set_lpm_interval(lpm_priv->lpm_id, 0, 0, &val);
403 dev_dbg(sbd_core(), "%s:%u: lv1 set_interval failed: "
404 "reg %u, %s\n", __func__, __LINE__, reg,
409 return lpm_priv->shadow.group_control;
410 case debug_bus_control:
411 return lpm_priv->shadow.debug_bus_control;
413 result = lv1_get_lpm_interrupt_status(lpm_priv->lpm_id,
417 dev_dbg(sbd_core(), "%s:%u: lv1 get_lpm_status failed: "
418 "reg %u, %s\n", __func__, __LINE__, reg,
425 dev_dbg(sbd_core(), "%s:%u: unknown reg: %d\n", __func__,
433 EXPORT_SYMBOL_GPL(ps3_read_pm);
436 * ps3_write_pm - Write Other LPM control registers.
439 void ps3_write_pm(u32 cpu, enum pm_reg_name reg, u32 val)
446 if (val != lpm_priv->shadow.group_control)
447 result = lv1_set_lpm_group_control(lpm_priv->lpm_id,
451 lpm_priv->shadow.group_control = val;
453 case debug_bus_control:
454 if (val != lpm_priv->shadow.debug_bus_control)
455 result = lv1_set_lpm_debug_bus_control(lpm_priv->lpm_id,
459 lpm_priv->shadow.debug_bus_control = val;
462 if (use_start_stop_bookmark)
463 val |= (PS3_PM_CONTROL_PPU_TH0_BOOKMARK |
464 PS3_PM_CONTROL_PPU_TH1_BOOKMARK);
465 if (val != lpm_priv->shadow.pm_control)
466 result = lv1_set_lpm_general_control(lpm_priv->lpm_id,
471 lpm_priv->shadow.pm_control = val;
474 result = lv1_set_lpm_interval(lpm_priv->lpm_id, val,
475 PS3_WRITE_PM_MASK, &dummy);
478 if (val != lpm_priv->shadow.pm_start_stop)
479 result = lv1_set_lpm_trigger_control(lpm_priv->lpm_id,
483 lpm_priv->shadow.pm_start_stop = val;
490 dev_dbg(sbd_core(), "%s:%u: unknown reg: %d\n", __func__,
497 dev_err(sbd_core(), "%s:%u: lv1 set_control failed: "
498 "reg %u, %s\n", __func__, __LINE__, reg,
501 EXPORT_SYMBOL_GPL(ps3_write_pm);
504 * ps3_get_ctr_size - Get the size of a physical counter.
506 * Returns either 16 or 32.
509 u32 ps3_get_ctr_size(u32 cpu, u32 phys_ctr)
513 if (phys_ctr >= NR_PHYS_CTRS) {
514 dev_dbg(sbd_core(), "%s:%u: phys_ctr too big: %u\n", __func__,
519 pm_ctrl = ps3_read_pm(cpu, pm_control);
520 return (pm_ctrl & CBE_PM_16BIT_CTR(phys_ctr)) ? 16 : 32;
522 EXPORT_SYMBOL_GPL(ps3_get_ctr_size);
525 * ps3_set_ctr_size - Set the size of a physical counter to 16 or 32 bits.
528 void ps3_set_ctr_size(u32 cpu, u32 phys_ctr, u32 ctr_size)
532 if (phys_ctr >= NR_PHYS_CTRS) {
533 dev_dbg(sbd_core(), "%s:%u: phys_ctr too big: %u\n", __func__,
538 pm_ctrl = ps3_read_pm(cpu, pm_control);
542 pm_ctrl |= CBE_PM_16BIT_CTR(phys_ctr);
543 ps3_write_pm(cpu, pm_control, pm_ctrl);
547 pm_ctrl &= ~CBE_PM_16BIT_CTR(phys_ctr);
548 ps3_write_pm(cpu, pm_control, pm_ctrl);
554 EXPORT_SYMBOL_GPL(ps3_set_ctr_size);
556 static u64 pm_translate_signal_group_number_on_island2(u64 subgroup)
563 return PM_ISLAND2_BASE_SIGNAL_GROUP_NUMBER + subgroup;
564 else if (subgroup == 7)
565 return PM_ISLAND2_SIGNAL_GROUP_NUMBER1;
567 return PM_ISLAND2_SIGNAL_GROUP_NUMBER2;
570 static u64 pm_translate_signal_group_number_on_island3(u64 subgroup)
585 return PM_ISLAND3_BASE_SIGNAL_GROUP_NUMBER + subgroup;
588 static u64 pm_translate_signal_group_number_on_island4(u64 subgroup)
590 return PM_ISLAND4_BASE_SIGNAL_GROUP_NUMBER + subgroup;
593 static u64 pm_translate_signal_group_number_on_island5(u64 subgroup)
606 return PM_ISLAND5_BASE_SIGNAL_GROUP_NUMBER + subgroup;
609 static u64 pm_translate_signal_group_number_on_island6(u64 subgroup,
622 switch (subsubgroup) {
644 return (PM_ISLAND6_BASE_SIGNAL_GROUP_NUMBER + subgroup);
646 return (PM_ISLAND6_BASE_SIGNAL_GROUP_NUMBER + subgroup
650 static u64 pm_translate_signal_group_number_on_island7(u64 subgroup)
652 return PM_ISLAND7_BASE_SIGNAL_GROUP_NUMBER + subgroup;
655 static u64 pm_translate_signal_group_number_on_island8(u64 subgroup)
657 return PM_ISLAND8_BASE_SIGNAL_GROUP_NUMBER + subgroup;
660 static u64 pm_signal_group_to_ps3_lv1_signal_group(u64 group)
671 if (20 <= group && group < 30) {
673 subgroup = group - 20;
674 } else if (30 <= group && group < 40) {
676 subgroup = group - 30;
677 } else if (40 <= group && group < 50) {
679 subgroup = group - 40;
680 } else if (50 <= group && group < 60) {
682 subgroup = group - 50;
683 } else if (60 <= group && group < 70) {
685 subgroup = group - 60;
686 } else if (70 <= group && group < 80) {
688 subgroup = group - 70;
689 } else if (80 <= group && group < 90) {
691 subgroup = group - 80;
693 } else if (200 <= group && group < 300) {
695 subgroup = group - 200;
696 } else if (600 <= group && group < 700) {
699 subsubgroup = group - 650;
701 } else if (6000 <= group && group < 7000) {
704 subsubgroup = group - 6500;
709 return pm_translate_signal_group_number_on_island2(subgroup);
711 return pm_translate_signal_group_number_on_island3(subgroup);
713 return pm_translate_signal_group_number_on_island4(subgroup);
715 return pm_translate_signal_group_number_on_island5(subgroup);
717 return pm_translate_signal_group_number_on_island6(subgroup,
720 return pm_translate_signal_group_number_on_island7(subgroup);
722 return pm_translate_signal_group_number_on_island8(subgroup);
724 dev_dbg(sbd_core(), "%s:%u: island not found: %llu\n", __func__,
732 static u64 pm_bus_word_to_ps3_lv1_bus_word(u8 word)
748 static int __ps3_set_signal(u64 lv1_signal_group, u64 bus_select,
749 u64 signal_select, u64 attr1, u64 attr2, u64 attr3)
753 ret = lv1_set_lpm_signal(lpm_priv->lpm_id, lv1_signal_group, bus_select,
754 signal_select, attr1, attr2, attr3);
757 "%s:%u: error:%d 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx\n",
758 __func__, __LINE__, ret, lv1_signal_group, bus_select,
759 signal_select, attr1, attr2, attr3);
764 int ps3_set_signal(u64 signal_group, u8 signal_bit, u16 sub_unit,
768 u64 lv1_signal_group;
771 u64 attr1, attr2, attr3;
773 if (signal_group == 0)
774 return __ps3_set_signal(0, 0, 0, 0, 0, 0);
777 pm_signal_group_to_ps3_lv1_signal_group(signal_group);
778 bus_select = pm_bus_word_to_ps3_lv1_bus_word(bus_word);
780 switch (signal_group) {
781 case PM_SIG_GROUP_SPU_TRIGGER:
783 signal_select = signal_select << (63 - signal_bit);
785 case PM_SIG_GROUP_SPU_EVENT:
787 signal_select = (signal_select << (63 - signal_bit)) | 0x3;
795 * 0: physical object.
797 * This parameter is only used for the PPE and SPE signals.
802 * This parameter is used to specify the target physical/logical
805 if (PM_SIG_GROUP_SPU <= signal_group &&
806 signal_group < PM_SIG_GROUP_MFC_MAX)
809 attr2 = lpm_priv->pu_id;
812 * This parameter is only used for setting the SPE signal.
816 ret = __ps3_set_signal(lv1_signal_group, bus_select, signal_select,
817 attr1, attr2, attr3);
819 dev_err(sbd_core(), "%s:%u: __ps3_set_signal failed: %d\n",
820 __func__, __LINE__, ret);
824 EXPORT_SYMBOL_GPL(ps3_set_signal);
826 u32 ps3_get_hw_thread_id(int cpu)
828 return get_hard_smp_processor_id(cpu);
830 EXPORT_SYMBOL_GPL(ps3_get_hw_thread_id);
833 * ps3_enable_pm - Enable the entire performance monitoring unit.
835 * When we enable the LPM, all pending writes to counters get committed.
838 void ps3_enable_pm(u32 cpu)
842 int insert_bookmark = 0;
844 lpm_priv->tb_count = 0;
846 if (use_start_stop_bookmark) {
847 if (!(lpm_priv->shadow.pm_start_stop &
848 (PS3_PM_START_STOP_START_MASK
849 | PS3_PM_START_STOP_STOP_MASK))) {
850 result = lv1_set_lpm_trigger_control(lpm_priv->lpm_id,
851 (PS3_PM_START_STOP_PPU_TH0_BOOKMARK_START |
852 PS3_PM_START_STOP_PPU_TH1_BOOKMARK_START |
853 PS3_PM_START_STOP_PPU_TH0_BOOKMARK_STOP |
854 PS3_PM_START_STOP_PPU_TH1_BOOKMARK_STOP),
855 0xFFFFFFFFFFFFFFFFULL, &tmp);
858 dev_err(sbd_core(), "%s:%u: "
859 "lv1_set_lpm_trigger_control failed: "
860 "%s\n", __func__, __LINE__,
863 insert_bookmark = !result;
867 result = lv1_start_lpm(lpm_priv->lpm_id);
870 dev_err(sbd_core(), "%s:%u: lv1_start_lpm failed: %s\n",
871 __func__, __LINE__, ps3_result(result));
873 if (use_start_stop_bookmark && !result && insert_bookmark)
874 ps3_set_bookmark(get_tb() | PS3_PM_BOOKMARK_START);
876 EXPORT_SYMBOL_GPL(ps3_enable_pm);
879 * ps3_disable_pm - Disable the entire performance monitoring unit.
882 void ps3_disable_pm(u32 cpu)
887 ps3_set_bookmark(get_tb() | PS3_PM_BOOKMARK_STOP);
889 result = lv1_stop_lpm(lpm_priv->lpm_id, &tmp);
892 if (result != LV1_WRONG_STATE)
893 dev_err(sbd_core(), "%s:%u: lv1_stop_lpm failed: %s\n",
894 __func__, __LINE__, ps3_result(result));
898 lpm_priv->tb_count = tmp;
900 dev_dbg(sbd_core(), "%s:%u: tb_count %llu (%llxh)\n", __func__, __LINE__,
901 lpm_priv->tb_count, lpm_priv->tb_count);
903 EXPORT_SYMBOL_GPL(ps3_disable_pm);
906 * ps3_lpm_copy_tb - Copy data from the trace buffer to a kernel buffer.
907 * @offset: Offset in bytes from the start of the trace buffer.
908 * @buf: Copy destination.
909 * @count: Maximum count of bytes to copy.
910 * @bytes_copied: Pointer to a variable that will receive the number of
911 * bytes copied to @buf.
913 * On error @buf will contain any successfully copied trace buffer data
914 * and bytes_copied will be set to the number of bytes successfully copied.
917 int ps3_lpm_copy_tb(unsigned long offset, void *buf, unsigned long count,
918 unsigned long *bytes_copied)
924 if (!lpm_priv->tb_cache)
927 if (offset >= lpm_priv->tb_count)
930 count = min_t(u64, count, lpm_priv->tb_count - offset);
932 while (*bytes_copied < count) {
933 const unsigned long request = count - *bytes_copied;
936 result = lv1_copy_lpm_trace_buffer(lpm_priv->lpm_id, offset,
939 dev_dbg(sbd_core(), "%s:%u: 0x%lx bytes at 0x%lx\n",
940 __func__, __LINE__, request, offset);
942 dev_err(sbd_core(), "%s:%u: lv1_copy_lpm_trace_buffer "
943 "failed: %s\n", __func__, __LINE__,
945 return result == LV1_WRONG_STATE ? -EBUSY : -EINVAL;
948 memcpy(buf, lpm_priv->tb_cache, tmp);
950 *bytes_copied += tmp;
953 dev_dbg(sbd_core(), "%s:%u: copied %lxh bytes\n", __func__, __LINE__,
958 EXPORT_SYMBOL_GPL(ps3_lpm_copy_tb);
961 * ps3_lpm_copy_tb_to_user - Copy data from the trace buffer to a user buffer.
962 * @offset: Offset in bytes from the start of the trace buffer.
963 * @buf: A __user copy destination.
964 * @count: Maximum count of bytes to copy.
965 * @bytes_copied: Pointer to a variable that will receive the number of
966 * bytes copied to @buf.
968 * On error @buf will contain any successfully copied trace buffer data
969 * and bytes_copied will be set to the number of bytes successfully copied.
972 int ps3_lpm_copy_tb_to_user(unsigned long offset, void __user *buf,
973 unsigned long count, unsigned long *bytes_copied)
979 if (!lpm_priv->tb_cache)
982 if (offset >= lpm_priv->tb_count)
985 count = min_t(u64, count, lpm_priv->tb_count - offset);
987 while (*bytes_copied < count) {
988 const unsigned long request = count - *bytes_copied;
991 result = lv1_copy_lpm_trace_buffer(lpm_priv->lpm_id, offset,
994 dev_dbg(sbd_core(), "%s:%u: 0x%lx bytes at 0x%lx\n",
995 __func__, __LINE__, request, offset);
996 dev_err(sbd_core(), "%s:%u: lv1_copy_lpm_trace_buffer "
997 "failed: %s\n", __func__, __LINE__,
999 return result == LV1_WRONG_STATE ? -EBUSY : -EINVAL;
1002 result = copy_to_user(buf, lpm_priv->tb_cache, tmp);
1005 dev_dbg(sbd_core(), "%s:%u: 0x%llx bytes at 0x%p\n",
1006 __func__, __LINE__, tmp, buf);
1007 dev_err(sbd_core(), "%s:%u: copy_to_user failed: %d\n",
1008 __func__, __LINE__, result);
1013 *bytes_copied += tmp;
1016 dev_dbg(sbd_core(), "%s:%u: copied %lxh bytes\n", __func__, __LINE__,
1021 EXPORT_SYMBOL_GPL(ps3_lpm_copy_tb_to_user);
1024 * ps3_get_and_clear_pm_interrupts -
1026 * Clearing interrupts for the entire performance monitoring unit.
1027 * Reading pm_status clears the interrupt bits.
1030 u32 ps3_get_and_clear_pm_interrupts(u32 cpu)
1032 return ps3_read_pm(cpu, pm_status);
1034 EXPORT_SYMBOL_GPL(ps3_get_and_clear_pm_interrupts);
1037 * ps3_enable_pm_interrupts -
1039 * Enabling interrupts for the entire performance monitoring unit.
1040 * Enables the interrupt bits in the pm_status register.
1043 void ps3_enable_pm_interrupts(u32 cpu, u32 thread, u32 mask)
1046 ps3_write_pm(cpu, pm_status, mask);
1048 EXPORT_SYMBOL_GPL(ps3_enable_pm_interrupts);
1051 * ps3_enable_pm_interrupts -
1053 * Disabling interrupts for the entire performance monitoring unit.
1056 void ps3_disable_pm_interrupts(u32 cpu)
1058 ps3_get_and_clear_pm_interrupts(cpu);
1059 ps3_write_pm(cpu, pm_status, 0);
1061 EXPORT_SYMBOL_GPL(ps3_disable_pm_interrupts);
1064 * ps3_lpm_open - Open the logical performance monitor device.
1065 * @tb_type: Specifies the type of trace buffer lv1 should use for this lpm
1066 * instance, specified by one of enum ps3_lpm_tb_type.
1067 * @tb_cache: Optional user supplied buffer to use as the trace buffer cache.
1068 * If NULL, the driver will allocate and manage an internal buffer.
1069 * Unused when when @tb_type is PS3_LPM_TB_TYPE_NONE.
1070 * @tb_cache_size: The size in bytes of the user supplied @tb_cache buffer.
1071 * Unused when @tb_cache is NULL or @tb_type is PS3_LPM_TB_TYPE_NONE.
1074 int ps3_lpm_open(enum ps3_lpm_tb_type tb_type, void *tb_cache,
1081 BUG_ON(tb_type != PS3_LPM_TB_TYPE_NONE
1082 && tb_type != PS3_LPM_TB_TYPE_INTERNAL);
1084 if (tb_type == PS3_LPM_TB_TYPE_NONE && tb_cache)
1085 dev_dbg(sbd_core(), "%s:%u: bad in vals\n", __func__, __LINE__);
1087 if (!atomic_add_unless(&lpm_priv->open, 1, 1)) {
1088 dev_dbg(sbd_core(), "%s:%u: busy\n", __func__, __LINE__);
1092 /* Note tb_cache needs 128 byte alignment. */
1094 if (tb_type == PS3_LPM_TB_TYPE_NONE) {
1095 lpm_priv->tb_cache_size = 0;
1096 lpm_priv->tb_cache_internal = NULL;
1097 lpm_priv->tb_cache = NULL;
1098 } else if (tb_cache) {
1099 if (tb_cache != (void *)ALIGN((unsigned long)tb_cache, 128)
1100 || tb_cache_size != ALIGN(tb_cache_size, 128)) {
1101 dev_err(sbd_core(), "%s:%u: unaligned tb_cache\n",
1102 __func__, __LINE__);
1106 lpm_priv->tb_cache_size = tb_cache_size;
1107 lpm_priv->tb_cache_internal = NULL;
1108 lpm_priv->tb_cache = tb_cache;
1110 lpm_priv->tb_cache_size = PS3_LPM_DEFAULT_TB_CACHE_SIZE;
1111 lpm_priv->tb_cache_internal = kzalloc(
1112 lpm_priv->tb_cache_size + 127, GFP_KERNEL);
1113 if (!lpm_priv->tb_cache_internal) {
1117 lpm_priv->tb_cache = (void *)ALIGN(
1118 (unsigned long)lpm_priv->tb_cache_internal, 128);
1121 result = lv1_construct_lpm(lpm_priv->node_id, tb_type, 0, 0,
1122 ps3_mm_phys_to_lpar(__pa(lpm_priv->tb_cache)),
1123 lpm_priv->tb_cache_size, &lpm_priv->lpm_id,
1124 &lpm_priv->outlet_id, &tb_size);
1127 dev_err(sbd_core(), "%s:%u: lv1_construct_lpm failed: %s\n",
1128 __func__, __LINE__, ps3_result(result));
1130 goto fail_construct;
1133 lpm_priv->shadow.pm_control = PS3_LPM_SHADOW_REG_INIT;
1134 lpm_priv->shadow.pm_start_stop = PS3_LPM_SHADOW_REG_INIT;
1135 lpm_priv->shadow.group_control = PS3_LPM_SHADOW_REG_INIT;
1136 lpm_priv->shadow.debug_bus_control = PS3_LPM_SHADOW_REG_INIT;
1138 dev_dbg(sbd_core(), "%s:%u: lpm_id 0x%llx, outlet_id 0x%llx, "
1139 "tb_size 0x%llx\n", __func__, __LINE__, lpm_priv->lpm_id,
1140 lpm_priv->outlet_id, tb_size);
1145 kfree(lpm_priv->tb_cache_internal);
1146 lpm_priv->tb_cache_internal = NULL;
1149 atomic_dec(&lpm_priv->open);
1152 EXPORT_SYMBOL_GPL(ps3_lpm_open);
1155 * ps3_lpm_close - Close the lpm device.
1159 int ps3_lpm_close(void)
1161 dev_dbg(sbd_core(), "%s:%u\n", __func__, __LINE__);
1163 lv1_destruct_lpm(lpm_priv->lpm_id);
1164 lpm_priv->lpm_id = 0;
1166 kfree(lpm_priv->tb_cache_internal);
1167 lpm_priv->tb_cache_internal = NULL;
1169 atomic_dec(&lpm_priv->open);
1172 EXPORT_SYMBOL_GPL(ps3_lpm_close);
1174 static int ps3_lpm_probe(struct ps3_system_bus_device *dev)
1176 dev_dbg(&dev->core, " -> %s:%u\n", __func__, __LINE__);
1179 dev_info(&dev->core, "%s:%u: called twice\n",
1180 __func__, __LINE__);
1184 lpm_priv = kzalloc(sizeof(*lpm_priv), GFP_KERNEL);
1189 lpm_priv->sbd = dev;
1190 lpm_priv->node_id = dev->lpm.node_id;
1191 lpm_priv->pu_id = dev->lpm.pu_id;
1192 lpm_priv->rights = dev->lpm.rights;
1194 dev_info(&dev->core, " <- %s:%u:\n", __func__, __LINE__);
1199 static void ps3_lpm_remove(struct ps3_system_bus_device *dev)
1201 dev_dbg(&dev->core, " -> %s:%u:\n", __func__, __LINE__);
1208 dev_info(&dev->core, " <- %s:%u:\n", __func__, __LINE__);
1211 static struct ps3_system_bus_driver ps3_lpm_driver = {
1212 .match_id = PS3_MATCH_ID_LPM,
1213 .core.name = "ps3-lpm",
1214 .core.owner = THIS_MODULE,
1215 .probe = ps3_lpm_probe,
1216 .remove = ps3_lpm_remove,
1217 .shutdown = ps3_lpm_remove,
1220 static int __init ps3_lpm_init(void)
1222 pr_debug("%s:%d:\n", __func__, __LINE__);
1223 return ps3_system_bus_driver_register(&ps3_lpm_driver);
1226 static void __exit ps3_lpm_exit(void)
1228 pr_debug("%s:%d:\n", __func__, __LINE__);
1229 ps3_system_bus_driver_unregister(&ps3_lpm_driver);
1232 module_init(ps3_lpm_init);
1233 module_exit(ps3_lpm_exit);
1235 MODULE_LICENSE("GPL v2");
1236 MODULE_DESCRIPTION("PS3 Logical Performance Monitor Driver");
1237 MODULE_AUTHOR("Sony Corporation");
1238 MODULE_ALIAS(PS3_MODULE_ALIAS_LPM);