Merge git://git.kernel.org/pub/scm/linux/kernel/git/x86/linux-2.6-x86
[linux-2.6/x86.git] / drivers / ps3 / ps3-lpm.c
blob6c9592ce49961e6a145463392f2c0523a5569cf6
1 /*
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>
25 #include <asm/ps3.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
74 /**
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 {
91 u64 pm_control;
92 u64 pm_start_stop;
93 u64 group_control;
94 u64 debug_bus_control;
97 #define PS3_LPM_SHADOW_REG_INIT 0xFFFFFFFF00000000ULL
99 /**
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
114 * aligned.
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 {
127 atomic_t open;
128 u64 rights;
129 u64 node_id;
130 u64 pu_id;
131 u64 lpm_id;
132 u64 outlet_id;
133 u64 tb_count;
134 void *tb_cache;
135 u64 tb_cache_size;
136 void *tb_cache_internal;
137 struct ps3_lpm_shadow_regs shadow;
138 struct ps3_system_bus_device *sbd;
141 enum {
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()
166 * to start/stop LPM.
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)
190 u64 bookmark;
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
204 * counters.
207 u32 ps3_read_phys_ctr(u32 cpu, u32 phys_ctr)
209 int result;
210 u64 counter0415;
211 u64 counter2637;
213 if (phys_ctr >= NR_PHYS_CTRS) {
214 dev_dbg(sbd_core(), "%s:%u: phys_ctr too big: %u\n", __func__,
215 __LINE__, phys_ctr);
216 return 0;
219 result = lv1_set_lpm_counter(lpm_priv->lpm_id, 0, 0, 0, 0, &counter0415,
220 &counter2637);
221 if (result) {
222 dev_err(sbd_core(), "%s:%u: lv1_set_lpm_counter failed: "
223 "phys_ctr %u, %s\n", __func__, __LINE__, phys_ctr,
224 ps3_result(result));
225 return 0;
228 switch (phys_ctr) {
229 case 0:
230 return counter0415 >> 32;
231 case 1:
232 return counter0415 & PS3_PM_COUNTER_MASK_LO;
233 case 2:
234 return counter2637 >> 32;
235 case 3:
236 return counter2637 & PS3_PM_COUNTER_MASK_LO;
237 default:
238 BUG();
240 return 0;
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
248 * counters.
251 void ps3_write_phys_ctr(u32 cpu, u32 phys_ctr, u32 val)
253 u64 counter0415;
254 u64 counter0415_mask;
255 u64 counter2637;
256 u64 counter2637_mask;
257 int result;
259 if (phys_ctr >= NR_PHYS_CTRS) {
260 dev_dbg(sbd_core(), "%s:%u: phys_ctr too big: %u\n", __func__,
261 __LINE__, phys_ctr);
262 return;
265 switch (phys_ctr) {
266 case 0:
267 counter0415 = (u64)val << 32;
268 counter0415_mask = PS3_PM_COUNTER_MASK_HI;
269 counter2637 = 0x0;
270 counter2637_mask = 0x0;
271 break;
272 case 1:
273 counter0415 = (u64)val;
274 counter0415_mask = PS3_PM_COUNTER_MASK_LO;
275 counter2637 = 0x0;
276 counter2637_mask = 0x0;
277 break;
278 case 2:
279 counter0415 = 0x0;
280 counter0415_mask = 0x0;
281 counter2637 = (u64)val << 32;
282 counter2637_mask = PS3_PM_COUNTER_MASK_HI;
283 break;
284 case 3:
285 counter0415 = 0x0;
286 counter0415_mask = 0x0;
287 counter2637 = (u64)val;
288 counter2637_mask = PS3_PM_COUNTER_MASK_LO;
289 break;
290 default:
291 BUG();
294 result = lv1_set_lpm_counter(lpm_priv->lpm_id,
295 counter0415, counter0415_mask,
296 counter2637, counter2637_mask,
297 &counter0415, &counter2637);
298 if (result)
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)
314 u32 val;
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);
322 return val;
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)
335 u32 phys_ctr;
336 u32 phys_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);
345 else
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)
361 return 0;
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)
373 int result;
374 static const u64 mask = 0xFFFFFFFFFFFFFFFFULL;
375 u64 old_value;
377 if (ctr >= NR_CTRS) {
378 dev_dbg(sbd_core(), "%s:%u: ctr too big: %u\n", __func__,
379 __LINE__, ctr);
380 return;
383 result = lv1_set_lpm_counter_control(lpm_priv->lpm_id, ctr, val, mask,
384 &old_value);
385 if (result)
386 dev_err(sbd_core(), "%s:%u: lv1_set_lpm_counter_control "
387 "failed: ctr %u, %s\n", __func__, __LINE__, ctr,
388 ps3_result(result));
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)
398 int result = 0;
399 u64 val = 0;
401 switch (reg) {
402 case pm_control:
403 return lpm_priv->shadow.pm_control;
404 case trace_address:
405 return CBE_PM_TRACE_BUF_EMPTY;
406 case pm_start_stop:
407 return lpm_priv->shadow.pm_start_stop;
408 case pm_interval:
409 result = lv1_set_lpm_interval(lpm_priv->lpm_id, 0, 0, &val);
410 if (result) {
411 val = 0;
412 dev_dbg(sbd_core(), "%s:%u: lv1 set_inteval failed: "
413 "reg %u, %s\n", __func__, __LINE__, reg,
414 ps3_result(result));
416 return (u32)val;
417 case group_control:
418 return lpm_priv->shadow.group_control;
419 case debug_bus_control:
420 return lpm_priv->shadow.debug_bus_control;
421 case pm_status:
422 result = lv1_get_lpm_interrupt_status(lpm_priv->lpm_id,
423 &val);
424 if (result) {
425 val = 0;
426 dev_dbg(sbd_core(), "%s:%u: lv1 get_lpm_status failed: "
427 "reg %u, %s\n", __func__, __LINE__, reg,
428 ps3_result(result));
430 return (u32)val;
431 case ext_tr_timer:
432 return 0;
433 default:
434 dev_dbg(sbd_core(), "%s:%u: unknown reg: %d\n", __func__,
435 __LINE__, reg);
436 BUG();
437 break;
440 return 0;
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)
450 int result = 0;
451 u64 dummy;
453 switch (reg) {
454 case group_control:
455 if (val != lpm_priv->shadow.group_control)
456 result = lv1_set_lpm_group_control(lpm_priv->lpm_id,
457 val,
458 PS3_WRITE_PM_MASK,
459 &dummy);
460 lpm_priv->shadow.group_control = val;
461 break;
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,
465 val,
466 PS3_WRITE_PM_MASK,
467 &dummy);
468 lpm_priv->shadow.debug_bus_control = val;
469 break;
470 case pm_control:
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,
476 val,
477 PS3_WRITE_PM_MASK,
478 0, 0, &dummy,
479 &dummy);
480 lpm_priv->shadow.pm_control = val;
481 break;
482 case pm_interval:
483 result = lv1_set_lpm_interval(lpm_priv->lpm_id, val,
484 PS3_WRITE_PM_MASK, &dummy);
485 break;
486 case pm_start_stop:
487 if (val != lpm_priv->shadow.pm_start_stop)
488 result = lv1_set_lpm_trigger_control(lpm_priv->lpm_id,
489 val,
490 PS3_WRITE_PM_MASK,
491 &dummy);
492 lpm_priv->shadow.pm_start_stop = val;
493 break;
494 case trace_address:
495 case ext_tr_timer:
496 case pm_status:
497 break;
498 default:
499 dev_dbg(sbd_core(), "%s:%u: unknown reg: %d\n", __func__,
500 __LINE__, reg);
501 BUG();
502 break;
505 if (result)
506 dev_err(sbd_core(), "%s:%u: lv1 set_control failed: "
507 "reg %u, %s\n", __func__, __LINE__, reg,
508 ps3_result(result));
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)
520 u32 pm_ctrl;
522 if (phys_ctr >= NR_PHYS_CTRS) {
523 dev_dbg(sbd_core(), "%s:%u: phys_ctr too big: %u\n", __func__,
524 __LINE__, phys_ctr);
525 return 0;
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)
539 u32 pm_ctrl;
541 if (phys_ctr >= NR_PHYS_CTRS) {
542 dev_dbg(sbd_core(), "%s:%u: phys_ctr too big: %u\n", __func__,
543 __LINE__, phys_ctr);
544 return;
547 pm_ctrl = ps3_read_pm(cpu, pm_control);
549 switch (ctr_size) {
550 case 16:
551 pm_ctrl |= CBE_PM_16BIT_CTR(phys_ctr);
552 ps3_write_pm(cpu, pm_control, pm_ctrl);
553 break;
555 case 32:
556 pm_ctrl &= ~CBE_PM_16BIT_CTR(phys_ctr);
557 ps3_write_pm(cpu, pm_control, pm_ctrl);
558 break;
559 default:
560 BUG();
563 EXPORT_SYMBOL_GPL(ps3_set_ctr_size);
565 static u64 pm_translate_signal_group_number_on_island2(u64 subgroup)
568 if (subgroup == 2)
569 subgroup = 3;
571 if (subgroup <= 6)
572 return PM_ISLAND2_BASE_SIGNAL_GROUP_NUMBER + subgroup;
573 else if (subgroup == 7)
574 return PM_ISLAND2_SIGNAL_GROUP_NUMBER1;
575 else
576 return PM_ISLAND2_SIGNAL_GROUP_NUMBER2;
579 static u64 pm_translate_signal_group_number_on_island3(u64 subgroup)
582 switch (subgroup) {
583 case 2:
584 case 3:
585 case 4:
586 subgroup += 2;
587 break;
588 case 5:
589 subgroup = 8;
590 break;
591 default:
592 break;
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)
605 switch (subgroup) {
606 case 3:
607 subgroup = 4;
608 break;
609 case 4:
610 subgroup = 6;
611 break;
612 default:
613 break;
615 return PM_ISLAND5_BASE_SIGNAL_GROUP_NUMBER + subgroup;
618 static u64 pm_translate_signal_group_number_on_island6(u64 subgroup,
619 u64 subsubgroup)
621 switch (subgroup) {
622 case 3:
623 case 4:
624 case 5:
625 subgroup += 1;
626 break;
627 default:
628 break;
631 switch (subsubgroup) {
632 case 4:
633 case 5:
634 case 6:
635 subsubgroup += 2;
636 break;
637 case 7:
638 case 8:
639 case 9:
640 case 10:
641 subsubgroup += 4;
642 break;
643 case 11:
644 case 12:
645 case 13:
646 subsubgroup += 5;
647 break;
648 default:
649 break;
652 if (subgroup <= 5)
653 return (PM_ISLAND6_BASE_SIGNAL_GROUP_NUMBER + subgroup);
654 else
655 return (PM_ISLAND6_BASE_SIGNAL_GROUP_NUMBER + subgroup
656 + subsubgroup - 1);
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)
671 u64 island;
672 u64 subgroup;
673 u64 subsubgroup;
675 subgroup = 0;
676 subsubgroup = 0;
677 island = 0;
678 if (group < 1000) {
679 if (group < 100) {
680 if (20 <= group && group < 30) {
681 island = 2;
682 subgroup = group - 20;
683 } else if (30 <= group && group < 40) {
684 island = 3;
685 subgroup = group - 30;
686 } else if (40 <= group && group < 50) {
687 island = 4;
688 subgroup = group - 40;
689 } else if (50 <= group && group < 60) {
690 island = 5;
691 subgroup = group - 50;
692 } else if (60 <= group && group < 70) {
693 island = 6;
694 subgroup = group - 60;
695 } else if (70 <= group && group < 80) {
696 island = 7;
697 subgroup = group - 70;
698 } else if (80 <= group && group < 90) {
699 island = 8;
700 subgroup = group - 80;
702 } else if (200 <= group && group < 300) {
703 island = 2;
704 subgroup = group - 200;
705 } else if (600 <= group && group < 700) {
706 island = 6;
707 subgroup = 5;
708 subsubgroup = group - 650;
710 } else if (6000 <= group && group < 7000) {
711 island = 6;
712 subgroup = 5;
713 subsubgroup = group - 6500;
716 switch (island) {
717 case 2:
718 return pm_translate_signal_group_number_on_island2(subgroup);
719 case 3:
720 return pm_translate_signal_group_number_on_island3(subgroup);
721 case 4:
722 return pm_translate_signal_group_number_on_island4(subgroup);
723 case 5:
724 return pm_translate_signal_group_number_on_island5(subgroup);
725 case 6:
726 return pm_translate_signal_group_number_on_island6(subgroup,
727 subsubgroup);
728 case 7:
729 return pm_translate_signal_group_number_on_island7(subgroup);
730 case 8:
731 return pm_translate_signal_group_number_on_island8(subgroup);
732 default:
733 dev_dbg(sbd_core(), "%s:%u: island not found: %lu\n", __func__,
734 __LINE__, group);
735 BUG();
736 break;
738 return 0;
741 static u64 pm_bus_word_to_ps3_lv1_bus_word(u8 word)
744 switch (word) {
745 case 1:
746 return 0xF000;
747 case 2:
748 return 0x0F00;
749 case 4:
750 return 0x00F0;
751 case 8:
752 default:
753 return 0x000F;
757 static int __ps3_set_signal(u64 lv1_signal_group, u64 bus_select,
758 u64 signal_select, u64 attr1, u64 attr2, u64 attr3)
760 int ret;
762 ret = lv1_set_lpm_signal(lpm_priv->lpm_id, lv1_signal_group, bus_select,
763 signal_select, attr1, attr2, attr3);
764 if (ret)
765 dev_err(sbd_core(),
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);
770 return ret;
773 int ps3_set_signal(u64 signal_group, u8 signal_bit, u16 sub_unit,
774 u8 bus_word)
776 int ret;
777 u64 lv1_signal_group;
778 u64 bus_select;
779 u64 signal_select;
780 u64 attr1, attr2, attr3;
782 if (signal_group == 0)
783 return __ps3_set_signal(0, 0, 0, 0, 0, 0);
785 lv1_signal_group =
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:
791 signal_select = 1;
792 signal_select = signal_select << (63 - signal_bit);
793 break;
794 case PM_SIG_GROUP_SPU_EVENT:
795 signal_select = 1;
796 signal_select = (signal_select << (63 - signal_bit)) | 0x3;
797 break;
798 default:
799 signal_select = 0;
800 break;
804 * 0: physical object.
805 * 1: logical object.
806 * This parameter is only used for the PPE and SPE signals.
808 attr1 = 1;
811 * This parameter is used to specify the target physical/logical
812 * PPE/SPE object.
814 if (PM_SIG_GROUP_SPU <= signal_group &&
815 signal_group < PM_SIG_GROUP_MFC_MAX)
816 attr2 = sub_unit;
817 else
818 attr2 = lpm_priv->pu_id;
821 * This parameter is only used for setting the SPE signal.
823 attr3 = 0;
825 ret = __ps3_set_signal(lv1_signal_group, bus_select, signal_select,
826 attr1, attr2, attr3);
827 if (ret)
828 dev_err(sbd_core(), "%s:%u: __ps3_set_signal failed: %d\n",
829 __func__, __LINE__, ret);
831 return 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)
849 int result;
850 u64 tmp;
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);
866 if (result)
867 dev_err(sbd_core(), "%s:%u: "
868 "lv1_set_lpm_trigger_control failed: "
869 "%s\n", __func__, __LINE__,
870 ps3_result(result));
872 insert_bookmark = !result;
876 result = lv1_start_lpm(lpm_priv->lpm_id);
878 if (result)
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)
893 int result;
894 u64 tmp;
896 ps3_set_bookmark(get_tb() | PS3_PM_BOOKMARK_STOP);
898 result = lv1_stop_lpm(lpm_priv->lpm_id, &tmp);
900 if (result) {
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));
904 return;
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)
929 int result;
931 *bytes_copied = 0;
933 if (!lpm_priv->tb_cache)
934 return -EPERM;
936 if (offset >= lpm_priv->tb_count)
937 return 0;
939 count = min(count, lpm_priv->tb_count - offset);
941 while (*bytes_copied < count) {
942 const unsigned long request = count - *bytes_copied;
943 u64 tmp;
945 result = lv1_copy_lpm_trace_buffer(lpm_priv->lpm_id, offset,
946 request, &tmp);
947 if (result) {
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__,
953 ps3_result(result));
954 return result == LV1_WRONG_STATE ? -EBUSY : -EINVAL;
957 memcpy(buf, lpm_priv->tb_cache, tmp);
958 buf += tmp;
959 *bytes_copied += tmp;
960 offset += tmp;
962 dev_dbg(sbd_core(), "%s:%u: copied %lxh bytes\n", __func__, __LINE__,
963 *bytes_copied);
965 return 0;
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)
984 int result;
986 *bytes_copied = 0;
988 if (!lpm_priv->tb_cache)
989 return -EPERM;
991 if (offset >= lpm_priv->tb_count)
992 return 0;
994 count = min(count, lpm_priv->tb_count - offset);
996 while (*bytes_copied < count) {
997 const unsigned long request = count - *bytes_copied;
998 u64 tmp;
1000 result = lv1_copy_lpm_trace_buffer(lpm_priv->lpm_id, offset,
1001 request, &tmp);
1002 if (result) {
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);
1013 if (result) {
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);
1018 return -EFAULT;
1021 buf += tmp;
1022 *bytes_copied += tmp;
1023 offset += tmp;
1025 dev_dbg(sbd_core(), "%s:%u: copied %lxh bytes\n", __func__, __LINE__,
1026 *bytes_copied);
1028 return 0;
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)
1054 if (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,
1084 u64 tb_cache_size)
1086 int result;
1087 u64 tb_size;
1089 BUG_ON(!lpm_priv);
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__);
1098 return -EBUSY;
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__);
1112 result = -EINVAL;
1113 goto fail_align;
1115 lpm_priv->tb_cache_size = tb_cache_size;
1116 lpm_priv->tb_cache_internal = NULL;
1117 lpm_priv->tb_cache = tb_cache;
1118 } else {
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__);
1125 result = -ENOMEM;
1126 goto fail_malloc;
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);
1137 if (result) {
1138 dev_err(sbd_core(), "%s:%u: lv1_construct_lpm failed: %s\n",
1139 __func__, __LINE__, ps3_result(result));
1140 result = -EINVAL;
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);
1153 return 0;
1155 fail_construct:
1156 kfree(lpm_priv->tb_cache_internal);
1157 lpm_priv->tb_cache_internal = NULL;
1158 fail_malloc:
1159 fail_align:
1160 atomic_dec(&lpm_priv->open);
1161 return result;
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);
1181 return 0;
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__);
1189 if (lpm_priv) {
1190 dev_info(&dev->core, "%s:%u: called twice\n",
1191 __func__, __LINE__);
1192 return -EBUSY;
1195 lpm_priv = kzalloc(sizeof(*lpm_priv), GFP_KERNEL);
1197 if (!lpm_priv)
1198 return -ENOMEM;
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__);
1207 return 0;
1210 static int ps3_lpm_remove(struct ps3_system_bus_device *dev)
1212 dev_dbg(&dev->core, " -> %s:%u:\n", __func__, __LINE__);
1214 ps3_lpm_close();
1216 kfree(lpm_priv);
1217 lpm_priv = NULL;
1219 dev_info(&dev->core, " <- %s:%u:\n", __func__, __LINE__);
1220 return 0;
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);