sb_tbprof.c 16 KB

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