sb_tbprof.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version 2
  5. * of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. *
  16. * Copyright (C) 2001, 2002, 2003 Broadcom Corporation
  17. * Copyright (C) 2007 Ralf Baechle <ralf@linux-mips.org>
  18. * Copyright (C) 2007 MIPS Technologies, Inc.
  19. * written by Ralf Baechle <ralf@linux-mips.org>
  20. */
  21. #undef DEBUG
  22. #include <linux/device.h>
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/types.h>
  26. #include <linux/init.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/sched.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/fs.h>
  31. #include <linux/errno.h>
  32. #include <linux/wait.h>
  33. #include <asm/io.h>
  34. #include <asm/sibyte/sb1250.h>
  35. #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
  36. #include <asm/sibyte/bcm1480_regs.h>
  37. #include <asm/sibyte/bcm1480_scd.h>
  38. #include <asm/sibyte/bcm1480_int.h>
  39. #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
  40. #include <asm/sibyte/sb1250_regs.h>
  41. #include <asm/sibyte/sb1250_scd.h>
  42. #include <asm/sibyte/sb1250_int.h>
  43. #else
  44. #error invalid SiByte UART configuration
  45. #endif
  46. #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
  47. #undef K_INT_TRACE_FREEZE
  48. #define K_INT_TRACE_FREEZE K_BCM1480_INT_TRACE_FREEZE
  49. #undef K_INT_PERF_CNT
  50. #define K_INT_PERF_CNT K_BCM1480_INT_PERF_CNT
  51. #endif
  52. #include <asm/uaccess.h>
  53. #define SBPROF_TB_MAJOR 240
  54. typedef u64 tb_sample_t[6*256];
  55. enum open_status {
  56. SB_CLOSED,
  57. SB_OPENING,
  58. SB_OPEN
  59. };
  60. struct sbprof_tb {
  61. wait_queue_head_t tb_sync;
  62. wait_queue_head_t tb_read;
  63. struct mutex lock;
  64. enum open_status open;
  65. tb_sample_t *sbprof_tbbuf;
  66. int next_tb_sample;
  67. volatile int tb_enable;
  68. volatile int tb_armed;
  69. };
  70. static struct sbprof_tb sbp;
  71. #define MAX_SAMPLE_BYTES (24*1024*1024)
  72. #define MAX_TBSAMPLE_BYTES (12*1024*1024)
  73. #define MAX_SAMPLES (MAX_SAMPLE_BYTES/sizeof(u_int32_t))
  74. #define TB_SAMPLE_SIZE (sizeof(tb_sample_t))
  75. #define MAX_TB_SAMPLES (MAX_TBSAMPLE_BYTES/TB_SAMPLE_SIZE)
  76. /* ioctls */
  77. #define SBPROF_ZBSTART _IOW('s', 0, int)
  78. #define SBPROF_ZBSTOP _IOW('s', 1, int)
  79. #define SBPROF_ZBWAITFULL _IOW('s', 2, int)
  80. /*
  81. * Routines for using 40-bit SCD cycle counter
  82. *
  83. * Client responsible for either handling interrupts or making sure
  84. * the cycles counter never saturates, e.g., by doing
  85. * zclk_timer_init(0) at least every 2^40 - 1 ZCLKs.
  86. */
  87. /*
  88. * Configures SCD counter 0 to count ZCLKs starting from val;
  89. * Configures SCD counters1,2,3 to count nothing.
  90. * Must not be called while gathering ZBbus profiles.
  91. */
  92. #define zclk_timer_init(val) \
  93. __asm__ __volatile__ (".set push;" \
  94. ".set mips64;" \
  95. "la $8, 0xb00204c0;" /* SCD perf_cnt_cfg */ \
  96. "sd %0, 0x10($8);" /* write val to counter0 */ \
  97. "sd %1, 0($8);" /* config counter0 for zclks*/ \
  98. ".set pop" \
  99. : /* no outputs */ \
  100. /* enable, counter0 */ \
  101. : /* inputs */ "r"(val), "r" ((1ULL << 33) | 1ULL) \
  102. : /* modifies */ "$8" )
  103. /* Reads SCD counter 0 and puts result in value
  104. unsigned long long val; */
  105. #define zclk_get(val) \
  106. __asm__ __volatile__ (".set push;" \
  107. ".set mips64;" \
  108. "la $8, 0xb00204c0;" /* SCD perf_cnt_cfg */ \
  109. "ld %0, 0x10($8);" /* write val to counter0 */ \
  110. ".set pop" \
  111. : /* outputs */ "=r"(val) \
  112. : /* inputs */ \
  113. : /* modifies */ "$8" )
  114. #define DEVNAME "sb_tbprof"
  115. #define TB_FULL (sbp.next_tb_sample == MAX_TB_SAMPLES)
  116. /*
  117. * Support for ZBbus sampling using the trace buffer
  118. *
  119. * We use the SCD performance counter interrupt, caused by a Zclk counter
  120. * overflow, to trigger the start of tracing.
  121. *
  122. * We set the trace buffer to sample everything and freeze on
  123. * overflow.
  124. *
  125. * We map the interrupt for trace_buffer_freeze to handle it on CPU 0.
  126. *
  127. */
  128. static u64 tb_period;
  129. static void arm_tb(void)
  130. {
  131. u64 scdperfcnt;
  132. u64 next = (1ULL << 40) - tb_period;
  133. u64 tb_options = M_SCD_TRACE_CFG_FREEZE_FULL;
  134. /*
  135. * Generate an SCD_PERFCNT interrupt in TB_PERIOD Zclks to
  136. * trigger start of trace. XXX vary sampling period
  137. */
  138. __raw_writeq(0, IOADDR(A_SCD_PERF_CNT_1));
  139. scdperfcnt = __raw_readq(IOADDR(A_SCD_PERF_CNT_CFG));
  140. /*
  141. * Unfortunately, in Pass 2 we must clear all counters to knock down
  142. * a previous interrupt request. This means that bus profiling
  143. * requires ALL of the SCD perf counters.
  144. */
  145. #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
  146. __raw_writeq((scdperfcnt & ~M_SPC_CFG_SRC1) |
  147. /* keep counters 0,2,3,4,5,6,7 as is */
  148. V_SPC_CFG_SRC1(1), /* counter 1 counts cycles */
  149. IOADDR(A_BCM1480_SCD_PERF_CNT_CFG0));
  150. __raw_writeq(
  151. M_SPC_CFG_ENABLE | /* enable counting */
  152. M_SPC_CFG_CLEAR | /* clear all counters */
  153. V_SPC_CFG_SRC1(1), /* counter 1 counts cycles */
  154. IOADDR(A_BCM1480_SCD_PERF_CNT_CFG1));
  155. #else
  156. __raw_writeq((scdperfcnt & ~M_SPC_CFG_SRC1) |
  157. /* keep counters 0,2,3 as is */
  158. M_SPC_CFG_ENABLE | /* enable counting */
  159. M_SPC_CFG_CLEAR | /* clear all counters */
  160. V_SPC_CFG_SRC1(1), /* counter 1 counts cycles */
  161. IOADDR(A_SCD_PERF_CNT_CFG));
  162. #endif
  163. __raw_writeq(next, IOADDR(A_SCD_PERF_CNT_1));
  164. /* Reset the trace buffer */
  165. __raw_writeq(M_SCD_TRACE_CFG_RESET, IOADDR(A_SCD_TRACE_CFG));
  166. #if 0 && defined(M_SCD_TRACE_CFG_FORCECNT)
  167. /* XXXKW may want to expose control to the data-collector */
  168. tb_options |= M_SCD_TRACE_CFG_FORCECNT;
  169. #endif
  170. __raw_writeq(tb_options, IOADDR(A_SCD_TRACE_CFG));
  171. sbp.tb_armed = 1;
  172. }
  173. static irqreturn_t sbprof_tb_intr(int irq, void *dev_id)
  174. {
  175. int i;
  176. pr_debug(DEVNAME ": tb_intr\n");
  177. if (sbp.next_tb_sample < MAX_TB_SAMPLES) {
  178. /* XXX should use XKPHYS to make writes bypass L2 */
  179. u64 *p = sbp.sbprof_tbbuf[sbp.next_tb_sample++];
  180. /* Read out trace */
  181. __raw_writeq(M_SCD_TRACE_CFG_START_READ,
  182. IOADDR(A_SCD_TRACE_CFG));
  183. __asm__ __volatile__ ("sync" : : : "memory");
  184. /* Loop runs backwards because bundles are read out in reverse order */
  185. for (i = 256 * 6; i > 0; i -= 6) {
  186. /* Subscripts decrease to put bundle in the order */
  187. /* t0 lo, t0 hi, t1 lo, t1 hi, t2 lo, t2 hi */
  188. p[i - 1] = __raw_readq(IOADDR(A_SCD_TRACE_READ));
  189. /* read t2 hi */
  190. p[i - 2] = __raw_readq(IOADDR(A_SCD_TRACE_READ));
  191. /* read t2 lo */
  192. p[i - 3] = __raw_readq(IOADDR(A_SCD_TRACE_READ));
  193. /* read t1 hi */
  194. p[i - 4] = __raw_readq(IOADDR(A_SCD_TRACE_READ));
  195. /* read t1 lo */
  196. p[i - 5] = __raw_readq(IOADDR(A_SCD_TRACE_READ));
  197. /* read t0 hi */
  198. p[i - 6] = __raw_readq(IOADDR(A_SCD_TRACE_READ));
  199. /* read t0 lo */
  200. }
  201. if (!sbp.tb_enable) {
  202. pr_debug(DEVNAME ": tb_intr shutdown\n");
  203. __raw_writeq(M_SCD_TRACE_CFG_RESET,
  204. IOADDR(A_SCD_TRACE_CFG));
  205. sbp.tb_armed = 0;
  206. wake_up_interruptible(&sbp.tb_sync);
  207. } else {
  208. /* knock down current interrupt and get another one later */
  209. arm_tb();
  210. }
  211. } else {
  212. /* No more trace buffer samples */
  213. pr_debug(DEVNAME ": tb_intr full\n");
  214. __raw_writeq(M_SCD_TRACE_CFG_RESET, IOADDR(A_SCD_TRACE_CFG));
  215. sbp.tb_armed = 0;
  216. if (!sbp.tb_enable)
  217. wake_up_interruptible(&sbp.tb_sync);
  218. wake_up_interruptible(&sbp.tb_read);
  219. }
  220. return IRQ_HANDLED;
  221. }
  222. static irqreturn_t sbprof_pc_intr(int irq, void *dev_id)
  223. {
  224. printk(DEVNAME ": unexpected pc_intr");
  225. return IRQ_NONE;
  226. }
  227. /*
  228. * Requires: Already called zclk_timer_init with a value that won't
  229. * saturate 40 bits. No subsequent use of SCD performance counters
  230. * or trace buffer.
  231. */
  232. static int sbprof_zbprof_start(struct file *filp)
  233. {
  234. u64 scdperfcnt;
  235. int err;
  236. if (xchg(&sbp.tb_enable, 1))
  237. return -EBUSY;
  238. pr_debug(DEVNAME ": starting\n");
  239. sbp.next_tb_sample = 0;
  240. filp->f_pos = 0;
  241. err = request_irq(K_INT_TRACE_FREEZE, sbprof_tb_intr, 0,
  242. DEVNAME " trace freeze", &sbp);
  243. if (err)
  244. return -EBUSY;
  245. /* Make sure there isn't a perf-cnt interrupt waiting */
  246. scdperfcnt = __raw_readq(IOADDR(A_SCD_PERF_CNT_CFG));
  247. /* Disable and clear counters, override SRC_1 */
  248. __raw_writeq((scdperfcnt & ~(M_SPC_CFG_SRC1 | M_SPC_CFG_ENABLE)) |
  249. M_SPC_CFG_ENABLE | M_SPC_CFG_CLEAR | V_SPC_CFG_SRC1(1),
  250. IOADDR(A_SCD_PERF_CNT_CFG));
  251. /*
  252. * We grab this interrupt to prevent others from trying to use
  253. * it, even though we don't want to service the interrupts
  254. * (they only feed into the trace-on-interrupt mechanism)
  255. */
  256. if (request_irq(K_INT_PERF_CNT, sbprof_pc_intr, 0, DEVNAME " scd perfcnt", &sbp)) {
  257. free_irq(K_INT_TRACE_FREEZE, &sbp);
  258. return -EBUSY;
  259. }
  260. /*
  261. * I need the core to mask these, but the interrupt mapper to
  262. * pass them through. I am exploiting my knowledge that
  263. * cp0_status masks out IP[5]. krw
  264. */
  265. #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
  266. __raw_writeq(K_BCM1480_INT_MAP_I3,
  267. IOADDR(A_BCM1480_IMR_REGISTER(0, R_BCM1480_IMR_INTERRUPT_MAP_BASE_L) +
  268. ((K_BCM1480_INT_PERF_CNT & 0x3f) << 3)));
  269. #else
  270. __raw_writeq(K_INT_MAP_I3,
  271. IOADDR(A_IMR_REGISTER(0, R_IMR_INTERRUPT_MAP_BASE) +
  272. (K_INT_PERF_CNT << 3)));
  273. #endif
  274. /* Initialize address traps */
  275. __raw_writeq(0, IOADDR(A_ADDR_TRAP_UP_0));
  276. __raw_writeq(0, IOADDR(A_ADDR_TRAP_UP_1));
  277. __raw_writeq(0, IOADDR(A_ADDR_TRAP_UP_2));
  278. __raw_writeq(0, IOADDR(A_ADDR_TRAP_UP_3));
  279. __raw_writeq(0, IOADDR(A_ADDR_TRAP_DOWN_0));
  280. __raw_writeq(0, IOADDR(A_ADDR_TRAP_DOWN_1));
  281. __raw_writeq(0, IOADDR(A_ADDR_TRAP_DOWN_2));
  282. __raw_writeq(0, IOADDR(A_ADDR_TRAP_DOWN_3));
  283. __raw_writeq(0, IOADDR(A_ADDR_TRAP_CFG_0));
  284. __raw_writeq(0, IOADDR(A_ADDR_TRAP_CFG_1));
  285. __raw_writeq(0, IOADDR(A_ADDR_TRAP_CFG_2));
  286. __raw_writeq(0, IOADDR(A_ADDR_TRAP_CFG_3));
  287. /* Initialize Trace Event 0-7 */
  288. /* when interrupt */
  289. __raw_writeq(M_SCD_TREVT_INTERRUPT, IOADDR(A_SCD_TRACE_EVENT_0));
  290. __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_1));
  291. __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_2));
  292. __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_3));
  293. __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_4));
  294. __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_5));
  295. __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_6));
  296. __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_7));
  297. /* Initialize Trace Sequence 0-7 */
  298. /* Start on event 0 (interrupt) */
  299. __raw_writeq(V_SCD_TRSEQ_FUNC_START | 0x0fff,
  300. IOADDR(A_SCD_TRACE_SEQUENCE_0));
  301. /* dsamp when d used | asamp when a used */
  302. __raw_writeq(M_SCD_TRSEQ_ASAMPLE | M_SCD_TRSEQ_DSAMPLE |
  303. K_SCD_TRSEQ_TRIGGER_ALL,
  304. IOADDR(A_SCD_TRACE_SEQUENCE_1));
  305. __raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_2));
  306. __raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_3));
  307. __raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_4));
  308. __raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_5));
  309. __raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_6));
  310. __raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_7));
  311. /* Now indicate the PERF_CNT interrupt as a trace-relevant interrupt */
  312. #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
  313. __raw_writeq(1ULL << (K_BCM1480_INT_PERF_CNT & 0x3f),
  314. IOADDR(A_BCM1480_IMR_REGISTER(0, R_BCM1480_IMR_INTERRUPT_TRACE_L)));
  315. #else
  316. __raw_writeq(1ULL << K_INT_PERF_CNT,
  317. IOADDR(A_IMR_REGISTER(0, R_IMR_INTERRUPT_TRACE)));
  318. #endif
  319. arm_tb();
  320. pr_debug(DEVNAME ": done starting\n");
  321. return 0;
  322. }
  323. static int sbprof_zbprof_stop(void)
  324. {
  325. int err = 0;
  326. pr_debug(DEVNAME ": stopping\n");
  327. if (sbp.tb_enable) {
  328. /*
  329. * XXXKW there is a window here where the intr handler may run,
  330. * see the disable, and do the wake_up before this sleep
  331. * happens.
  332. */
  333. pr_debug(DEVNAME ": wait for disarm\n");
  334. err = wait_event_interruptible(sbp.tb_sync, !sbp.tb_armed);
  335. pr_debug(DEVNAME ": disarm complete, stat %d\n", err);
  336. if (err)
  337. return err;
  338. sbp.tb_enable = 0;
  339. free_irq(K_INT_TRACE_FREEZE, &sbp);
  340. free_irq(K_INT_PERF_CNT, &sbp);
  341. }
  342. pr_debug(DEVNAME ": done stopping\n");
  343. return err;
  344. }
  345. static int sbprof_tb_open(struct inode *inode, struct file *filp)
  346. {
  347. int minor;
  348. minor = iminor(inode);
  349. if (minor != 0)
  350. return -ENODEV;
  351. if (xchg(&sbp.open, SB_OPENING) != SB_CLOSED)
  352. return -EBUSY;
  353. memset(&sbp, 0, sizeof(struct sbprof_tb));
  354. sbp.sbprof_tbbuf = vzalloc(MAX_TBSAMPLE_BYTES);
  355. if (!sbp.sbprof_tbbuf) {
  356. sbp.open = SB_CLOSED;
  357. wmb();
  358. return -ENOMEM;
  359. }
  360. init_waitqueue_head(&sbp.tb_sync);
  361. init_waitqueue_head(&sbp.tb_read);
  362. mutex_init(&sbp.lock);
  363. sbp.open = SB_OPEN;
  364. wmb();
  365. return 0;
  366. }
  367. static int sbprof_tb_release(struct inode *inode, struct file *filp)
  368. {
  369. int minor;
  370. minor = iminor(inode);
  371. if (minor != 0 || sbp.open != SB_CLOSED)
  372. return -ENODEV;
  373. mutex_lock(&sbp.lock);
  374. if (sbp.tb_armed || sbp.tb_enable)
  375. sbprof_zbprof_stop();
  376. vfree(sbp.sbprof_tbbuf);
  377. sbp.open = SB_CLOSED;
  378. wmb();
  379. mutex_unlock(&sbp.lock);
  380. return 0;
  381. }
  382. static ssize_t sbprof_tb_read(struct file *filp, char *buf,
  383. size_t size, loff_t *offp)
  384. {
  385. int cur_sample, sample_off, cur_count, sample_left;
  386. char *src;
  387. int count = 0;
  388. char *dest = buf;
  389. long cur_off = *offp;
  390. if (!access_ok(VERIFY_WRITE, buf, size))
  391. return -EFAULT;
  392. mutex_lock(&sbp.lock);
  393. count = 0;
  394. cur_sample = cur_off / TB_SAMPLE_SIZE;
  395. sample_off = cur_off % TB_SAMPLE_SIZE;
  396. sample_left = TB_SAMPLE_SIZE - sample_off;
  397. while (size && (cur_sample < sbp.next_tb_sample)) {
  398. int err;
  399. cur_count = size < sample_left ? size : sample_left;
  400. src = (char *)(((long)sbp.sbprof_tbbuf[cur_sample])+sample_off);
  401. err = __copy_to_user(dest, src, cur_count);
  402. if (err) {
  403. *offp = cur_off + cur_count - err;
  404. mutex_unlock(&sbp.lock);
  405. return err;
  406. }
  407. pr_debug(DEVNAME ": read from sample %d, %d bytes\n",
  408. cur_sample, cur_count);
  409. size -= cur_count;
  410. sample_left -= cur_count;
  411. if (!sample_left) {
  412. cur_sample++;
  413. sample_off = 0;
  414. sample_left = TB_SAMPLE_SIZE;
  415. } else {
  416. sample_off += cur_count;
  417. }
  418. cur_off += cur_count;
  419. dest += cur_count;
  420. count += cur_count;
  421. }
  422. *offp = cur_off;
  423. mutex_unlock(&sbp.lock);
  424. return count;
  425. }
  426. static long sbprof_tb_ioctl(struct file *filp,
  427. unsigned int command,
  428. unsigned long arg)
  429. {
  430. int err = 0;
  431. switch (command) {
  432. case SBPROF_ZBSTART:
  433. mutex_lock(&sbp.lock);
  434. err = sbprof_zbprof_start(filp);
  435. mutex_unlock(&sbp.lock);
  436. break;
  437. case SBPROF_ZBSTOP:
  438. mutex_lock(&sbp.lock);
  439. err = sbprof_zbprof_stop();
  440. mutex_unlock(&sbp.lock);
  441. break;
  442. case SBPROF_ZBWAITFULL: {
  443. err = wait_event_interruptible(sbp.tb_read, TB_FULL);
  444. if (err)
  445. break;
  446. err = put_user(TB_FULL, (int *) arg);
  447. break;
  448. }
  449. default:
  450. err = -EINVAL;
  451. break;
  452. }
  453. return err;
  454. }
  455. static const struct file_operations sbprof_tb_fops = {
  456. .owner = THIS_MODULE,
  457. .open = sbprof_tb_open,
  458. .release = sbprof_tb_release,
  459. .read = sbprof_tb_read,
  460. .unlocked_ioctl = sbprof_tb_ioctl,
  461. .compat_ioctl = sbprof_tb_ioctl,
  462. .mmap = NULL,
  463. .llseek = default_llseek,
  464. };
  465. static struct class *tb_class;
  466. static struct device *tb_dev;
  467. static int __init sbprof_tb_init(void)
  468. {
  469. struct device *dev;
  470. struct class *tbc;
  471. int err;
  472. if (register_chrdev(SBPROF_TB_MAJOR, DEVNAME, &sbprof_tb_fops)) {
  473. printk(KERN_WARNING DEVNAME ": initialization failed (dev %d)\n",
  474. SBPROF_TB_MAJOR);
  475. return -EIO;
  476. }
  477. tbc = class_create(THIS_MODULE, "sb_tracebuffer");
  478. if (IS_ERR(tbc)) {
  479. err = PTR_ERR(tbc);
  480. goto out_chrdev;
  481. }
  482. tb_class = tbc;
  483. dev = device_create(tbc, NULL, MKDEV(SBPROF_TB_MAJOR, 0), NULL, "tb");
  484. if (IS_ERR(dev)) {
  485. err = PTR_ERR(dev);
  486. goto out_class;
  487. }
  488. tb_dev = dev;
  489. sbp.open = SB_CLOSED;
  490. wmb();
  491. tb_period = zbbus_mhz * 10000LL;
  492. pr_info(DEVNAME ": initialized - tb_period = %lld\n",
  493. (long long) tb_period);
  494. return 0;
  495. out_class:
  496. class_destroy(tb_class);
  497. out_chrdev:
  498. unregister_chrdev(SBPROF_TB_MAJOR, DEVNAME);
  499. return err;
  500. }
  501. static void __exit sbprof_tb_cleanup(void)
  502. {
  503. device_destroy(tb_class, MKDEV(SBPROF_TB_MAJOR, 0));
  504. unregister_chrdev(SBPROF_TB_MAJOR, DEVNAME);
  505. class_destroy(tb_class);
  506. }
  507. module_init(sbprof_tb_init);
  508. module_exit(sbprof_tb_cleanup);
  509. MODULE_ALIAS_CHARDEV_MAJOR(SBPROF_TB_MAJOR);
  510. MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");
  511. MODULE_LICENSE("GPL");