sb_tbprof.c 15 KB

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