coresight-etb10.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
  4. *
  5. * Description: CoreSight Embedded Trace Buffer driver
  6. */
  7. #include <asm/local.h>
  8. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/types.h>
  11. #include <linux/device.h>
  12. #include <linux/io.h>
  13. #include <linux/err.h>
  14. #include <linux/fs.h>
  15. #include <linux/miscdevice.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/slab.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/coresight.h>
  22. #include <linux/amba/bus.h>
  23. #include <linux/clk.h>
  24. #include <linux/circ_buf.h>
  25. #include <linux/mm.h>
  26. #include <linux/perf_event.h>
  27. #include "coresight-priv.h"
  28. #define ETB_RAM_DEPTH_REG 0x004
  29. #define ETB_STATUS_REG 0x00c
  30. #define ETB_RAM_READ_DATA_REG 0x010
  31. #define ETB_RAM_READ_POINTER 0x014
  32. #define ETB_RAM_WRITE_POINTER 0x018
  33. #define ETB_TRG 0x01c
  34. #define ETB_CTL_REG 0x020
  35. #define ETB_RWD_REG 0x024
  36. #define ETB_FFSR 0x300
  37. #define ETB_FFCR 0x304
  38. #define ETB_ITMISCOP0 0xee0
  39. #define ETB_ITTRFLINACK 0xee4
  40. #define ETB_ITTRFLIN 0xee8
  41. #define ETB_ITATBDATA0 0xeeC
  42. #define ETB_ITATBCTR2 0xef0
  43. #define ETB_ITATBCTR1 0xef4
  44. #define ETB_ITATBCTR0 0xef8
  45. /* register description */
  46. /* STS - 0x00C */
  47. #define ETB_STATUS_RAM_FULL BIT(0)
  48. /* CTL - 0x020 */
  49. #define ETB_CTL_CAPT_EN BIT(0)
  50. /* FFCR - 0x304 */
  51. #define ETB_FFCR_EN_FTC BIT(0)
  52. #define ETB_FFCR_FON_MAN BIT(6)
  53. #define ETB_FFCR_STOP_FI BIT(12)
  54. #define ETB_FFCR_STOP_TRIGGER BIT(13)
  55. #define ETB_FFCR_BIT 6
  56. #define ETB_FFSR_BIT 1
  57. #define ETB_FRAME_SIZE_WORDS 4
  58. /**
  59. * struct etb_drvdata - specifics associated to an ETB component
  60. * @base: memory mapped base address for this component.
  61. * @dev: the device entity associated to this component.
  62. * @atclk: optional clock for the core parts of the ETB.
  63. * @csdev: component vitals needed by the framework.
  64. * @miscdev: specifics to handle "/dev/xyz.etb" entry.
  65. * @spinlock: only one at a time pls.
  66. * @reading: synchronise user space access to etb buffer.
  67. * @mode: this ETB is being used.
  68. * @buf: area of memory where ETB buffer content gets sent.
  69. * @buffer_depth: size of @buf.
  70. * @trigger_cntr: amount of words to store after a trigger.
  71. */
  72. struct etb_drvdata {
  73. void __iomem *base;
  74. struct device *dev;
  75. struct clk *atclk;
  76. struct coresight_device *csdev;
  77. struct miscdevice miscdev;
  78. spinlock_t spinlock;
  79. local_t reading;
  80. local_t mode;
  81. u8 *buf;
  82. u32 buffer_depth;
  83. u32 trigger_cntr;
  84. };
  85. static unsigned int etb_get_buffer_depth(struct etb_drvdata *drvdata)
  86. {
  87. u32 depth = 0;
  88. pm_runtime_get_sync(drvdata->dev);
  89. /* RO registers don't need locking */
  90. depth = readl_relaxed(drvdata->base + ETB_RAM_DEPTH_REG);
  91. pm_runtime_put(drvdata->dev);
  92. return depth;
  93. }
  94. static void etb_enable_hw(struct etb_drvdata *drvdata)
  95. {
  96. int i;
  97. u32 depth;
  98. CS_UNLOCK(drvdata->base);
  99. depth = drvdata->buffer_depth;
  100. /* reset write RAM pointer address */
  101. writel_relaxed(0x0, drvdata->base + ETB_RAM_WRITE_POINTER);
  102. /* clear entire RAM buffer */
  103. for (i = 0; i < depth; i++)
  104. writel_relaxed(0x0, drvdata->base + ETB_RWD_REG);
  105. /* reset write RAM pointer address */
  106. writel_relaxed(0x0, drvdata->base + ETB_RAM_WRITE_POINTER);
  107. /* reset read RAM pointer address */
  108. writel_relaxed(0x0, drvdata->base + ETB_RAM_READ_POINTER);
  109. writel_relaxed(drvdata->trigger_cntr, drvdata->base + ETB_TRG);
  110. writel_relaxed(ETB_FFCR_EN_FTC | ETB_FFCR_STOP_TRIGGER,
  111. drvdata->base + ETB_FFCR);
  112. /* ETB trace capture enable */
  113. writel_relaxed(ETB_CTL_CAPT_EN, drvdata->base + ETB_CTL_REG);
  114. CS_LOCK(drvdata->base);
  115. }
  116. static int etb_enable(struct coresight_device *csdev, u32 mode)
  117. {
  118. u32 val;
  119. unsigned long flags;
  120. struct etb_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  121. val = local_cmpxchg(&drvdata->mode,
  122. CS_MODE_DISABLED, mode);
  123. /*
  124. * When accessing from Perf, a HW buffer can be handled
  125. * by a single trace entity. In sysFS mode many tracers
  126. * can be logging to the same HW buffer.
  127. */
  128. if (val == CS_MODE_PERF)
  129. return -EBUSY;
  130. /* Don't let perf disturb sysFS sessions */
  131. if (val == CS_MODE_SYSFS && mode == CS_MODE_PERF)
  132. return -EBUSY;
  133. /* Nothing to do, the tracer is already enabled. */
  134. if (val == CS_MODE_SYSFS)
  135. goto out;
  136. spin_lock_irqsave(&drvdata->spinlock, flags);
  137. etb_enable_hw(drvdata);
  138. spin_unlock_irqrestore(&drvdata->spinlock, flags);
  139. out:
  140. dev_info(drvdata->dev, "ETB enabled\n");
  141. return 0;
  142. }
  143. static void etb_disable_hw(struct etb_drvdata *drvdata)
  144. {
  145. u32 ffcr;
  146. CS_UNLOCK(drvdata->base);
  147. ffcr = readl_relaxed(drvdata->base + ETB_FFCR);
  148. /* stop formatter when a stop has completed */
  149. ffcr |= ETB_FFCR_STOP_FI;
  150. writel_relaxed(ffcr, drvdata->base + ETB_FFCR);
  151. /* manually generate a flush of the system */
  152. ffcr |= ETB_FFCR_FON_MAN;
  153. writel_relaxed(ffcr, drvdata->base + ETB_FFCR);
  154. if (coresight_timeout(drvdata->base, ETB_FFCR, ETB_FFCR_BIT, 0)) {
  155. dev_err(drvdata->dev,
  156. "timeout while waiting for completion of Manual Flush\n");
  157. }
  158. /* disable trace capture */
  159. writel_relaxed(0x0, drvdata->base + ETB_CTL_REG);
  160. if (coresight_timeout(drvdata->base, ETB_FFSR, ETB_FFSR_BIT, 1)) {
  161. dev_err(drvdata->dev,
  162. "timeout while waiting for Formatter to Stop\n");
  163. }
  164. CS_LOCK(drvdata->base);
  165. }
  166. static void etb_dump_hw(struct etb_drvdata *drvdata)
  167. {
  168. bool lost = false;
  169. int i;
  170. u8 *buf_ptr;
  171. u32 read_data, depth;
  172. u32 read_ptr, write_ptr;
  173. u32 frame_off, frame_endoff;
  174. CS_UNLOCK(drvdata->base);
  175. read_ptr = readl_relaxed(drvdata->base + ETB_RAM_READ_POINTER);
  176. write_ptr = readl_relaxed(drvdata->base + ETB_RAM_WRITE_POINTER);
  177. frame_off = write_ptr % ETB_FRAME_SIZE_WORDS;
  178. frame_endoff = ETB_FRAME_SIZE_WORDS - frame_off;
  179. if (frame_off) {
  180. dev_err(drvdata->dev,
  181. "write_ptr: %lu not aligned to formatter frame size\n",
  182. (unsigned long)write_ptr);
  183. dev_err(drvdata->dev, "frameoff: %lu, frame_endoff: %lu\n",
  184. (unsigned long)frame_off, (unsigned long)frame_endoff);
  185. write_ptr += frame_endoff;
  186. }
  187. if ((readl_relaxed(drvdata->base + ETB_STATUS_REG)
  188. & ETB_STATUS_RAM_FULL) == 0) {
  189. writel_relaxed(0x0, drvdata->base + ETB_RAM_READ_POINTER);
  190. } else {
  191. writel_relaxed(write_ptr, drvdata->base + ETB_RAM_READ_POINTER);
  192. lost = true;
  193. }
  194. depth = drvdata->buffer_depth;
  195. buf_ptr = drvdata->buf;
  196. for (i = 0; i < depth; i++) {
  197. read_data = readl_relaxed(drvdata->base +
  198. ETB_RAM_READ_DATA_REG);
  199. *(u32 *)buf_ptr = read_data;
  200. buf_ptr += 4;
  201. }
  202. if (lost)
  203. coresight_insert_barrier_packet(drvdata->buf);
  204. if (frame_off) {
  205. buf_ptr -= (frame_endoff * 4);
  206. for (i = 0; i < frame_endoff; i++) {
  207. *buf_ptr++ = 0x0;
  208. *buf_ptr++ = 0x0;
  209. *buf_ptr++ = 0x0;
  210. *buf_ptr++ = 0x0;
  211. }
  212. }
  213. writel_relaxed(read_ptr, drvdata->base + ETB_RAM_READ_POINTER);
  214. CS_LOCK(drvdata->base);
  215. }
  216. static void etb_disable(struct coresight_device *csdev)
  217. {
  218. struct etb_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  219. unsigned long flags;
  220. spin_lock_irqsave(&drvdata->spinlock, flags);
  221. etb_disable_hw(drvdata);
  222. etb_dump_hw(drvdata);
  223. spin_unlock_irqrestore(&drvdata->spinlock, flags);
  224. local_set(&drvdata->mode, CS_MODE_DISABLED);
  225. dev_info(drvdata->dev, "ETB disabled\n");
  226. }
  227. static void *etb_alloc_buffer(struct coresight_device *csdev, int cpu,
  228. void **pages, int nr_pages, bool overwrite)
  229. {
  230. int node;
  231. struct cs_buffers *buf;
  232. if (cpu == -1)
  233. cpu = smp_processor_id();
  234. node = cpu_to_node(cpu);
  235. buf = kzalloc_node(sizeof(struct cs_buffers), GFP_KERNEL, node);
  236. if (!buf)
  237. return NULL;
  238. buf->snapshot = overwrite;
  239. buf->nr_pages = nr_pages;
  240. buf->data_pages = pages;
  241. return buf;
  242. }
  243. static void etb_free_buffer(void *config)
  244. {
  245. struct cs_buffers *buf = config;
  246. kfree(buf);
  247. }
  248. static int etb_set_buffer(struct coresight_device *csdev,
  249. struct perf_output_handle *handle,
  250. void *sink_config)
  251. {
  252. int ret = 0;
  253. unsigned long head;
  254. struct cs_buffers *buf = sink_config;
  255. /* wrap head around to the amount of space we have */
  256. head = handle->head & ((buf->nr_pages << PAGE_SHIFT) - 1);
  257. /* find the page to write to */
  258. buf->cur = head / PAGE_SIZE;
  259. /* and offset within that page */
  260. buf->offset = head % PAGE_SIZE;
  261. local_set(&buf->data_size, 0);
  262. return ret;
  263. }
  264. static unsigned long etb_reset_buffer(struct coresight_device *csdev,
  265. struct perf_output_handle *handle,
  266. void *sink_config)
  267. {
  268. unsigned long size = 0;
  269. struct cs_buffers *buf = sink_config;
  270. if (buf) {
  271. /*
  272. * In snapshot mode ->data_size holds the new address of the
  273. * ring buffer's head. The size itself is the whole address
  274. * range since we want the latest information.
  275. */
  276. if (buf->snapshot)
  277. handle->head = local_xchg(&buf->data_size,
  278. buf->nr_pages << PAGE_SHIFT);
  279. /*
  280. * Tell the tracer PMU how much we got in this run and if
  281. * something went wrong along the way. Nobody else can use
  282. * this cs_buffers instance until we are done. As such
  283. * resetting parameters here and squaring off with the ring
  284. * buffer API in the tracer PMU is fine.
  285. */
  286. size = local_xchg(&buf->data_size, 0);
  287. }
  288. return size;
  289. }
  290. static void etb_update_buffer(struct coresight_device *csdev,
  291. struct perf_output_handle *handle,
  292. void *sink_config)
  293. {
  294. bool lost = false;
  295. int i, cur;
  296. u8 *buf_ptr;
  297. const u32 *barrier;
  298. u32 read_ptr, write_ptr, capacity;
  299. u32 status, read_data, to_read;
  300. unsigned long offset;
  301. struct cs_buffers *buf = sink_config;
  302. struct etb_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  303. if (!buf)
  304. return;
  305. capacity = drvdata->buffer_depth * ETB_FRAME_SIZE_WORDS;
  306. etb_disable_hw(drvdata);
  307. CS_UNLOCK(drvdata->base);
  308. /* unit is in words, not bytes */
  309. read_ptr = readl_relaxed(drvdata->base + ETB_RAM_READ_POINTER);
  310. write_ptr = readl_relaxed(drvdata->base + ETB_RAM_WRITE_POINTER);
  311. /*
  312. * Entries should be aligned to the frame size. If they are not
  313. * go back to the last alignment point to give decoding tools a
  314. * chance to fix things.
  315. */
  316. if (write_ptr % ETB_FRAME_SIZE_WORDS) {
  317. dev_err(drvdata->dev,
  318. "write_ptr: %lu not aligned to formatter frame size\n",
  319. (unsigned long)write_ptr);
  320. write_ptr &= ~(ETB_FRAME_SIZE_WORDS - 1);
  321. lost = true;
  322. }
  323. /*
  324. * Get a hold of the status register and see if a wrap around
  325. * has occurred. If so adjust things accordingly. Otherwise
  326. * start at the beginning and go until the write pointer has
  327. * been reached.
  328. */
  329. status = readl_relaxed(drvdata->base + ETB_STATUS_REG);
  330. if (status & ETB_STATUS_RAM_FULL) {
  331. lost = true;
  332. to_read = capacity;
  333. read_ptr = write_ptr;
  334. } else {
  335. to_read = CIRC_CNT(write_ptr, read_ptr, drvdata->buffer_depth);
  336. to_read *= ETB_FRAME_SIZE_WORDS;
  337. }
  338. /*
  339. * Make sure we don't overwrite data that hasn't been consumed yet.
  340. * It is entirely possible that the HW buffer has more data than the
  341. * ring buffer can currently handle. If so adjust the start address
  342. * to take only the last traces.
  343. *
  344. * In snapshot mode we are looking to get the latest traces only and as
  345. * such, we don't care about not overwriting data that hasn't been
  346. * processed by user space.
  347. */
  348. if (!buf->snapshot && to_read > handle->size) {
  349. u32 mask = ~(ETB_FRAME_SIZE_WORDS - 1);
  350. /* The new read pointer must be frame size aligned */
  351. to_read = handle->size & mask;
  352. /*
  353. * Move the RAM read pointer up, keeping in mind that
  354. * everything is in frame size units.
  355. */
  356. read_ptr = (write_ptr + drvdata->buffer_depth) -
  357. to_read / ETB_FRAME_SIZE_WORDS;
  358. /* Wrap around if need be*/
  359. if (read_ptr > (drvdata->buffer_depth - 1))
  360. read_ptr -= drvdata->buffer_depth;
  361. /* let the decoder know we've skipped ahead */
  362. lost = true;
  363. }
  364. if (lost)
  365. perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED);
  366. /* finally tell HW where we want to start reading from */
  367. writel_relaxed(read_ptr, drvdata->base + ETB_RAM_READ_POINTER);
  368. cur = buf->cur;
  369. offset = buf->offset;
  370. barrier = barrier_pkt;
  371. for (i = 0; i < to_read; i += 4) {
  372. buf_ptr = buf->data_pages[cur] + offset;
  373. read_data = readl_relaxed(drvdata->base +
  374. ETB_RAM_READ_DATA_REG);
  375. if (lost && i < CORESIGHT_BARRIER_PKT_SIZE) {
  376. read_data = *barrier;
  377. barrier++;
  378. }
  379. *(u32 *)buf_ptr = read_data;
  380. buf_ptr += 4;
  381. offset += 4;
  382. if (offset >= PAGE_SIZE) {
  383. offset = 0;
  384. cur++;
  385. /* wrap around at the end of the buffer */
  386. cur &= buf->nr_pages - 1;
  387. }
  388. }
  389. /* reset ETB buffer for next run */
  390. writel_relaxed(0x0, drvdata->base + ETB_RAM_READ_POINTER);
  391. writel_relaxed(0x0, drvdata->base + ETB_RAM_WRITE_POINTER);
  392. /*
  393. * In snapshot mode all we have to do is communicate to
  394. * perf_aux_output_end() the address of the current head. In full
  395. * trace mode the same function expects a size to move rb->aux_head
  396. * forward.
  397. */
  398. if (buf->snapshot)
  399. local_set(&buf->data_size, (cur * PAGE_SIZE) + offset);
  400. else
  401. local_add(to_read, &buf->data_size);
  402. etb_enable_hw(drvdata);
  403. CS_LOCK(drvdata->base);
  404. }
  405. static const struct coresight_ops_sink etb_sink_ops = {
  406. .enable = etb_enable,
  407. .disable = etb_disable,
  408. .alloc_buffer = etb_alloc_buffer,
  409. .free_buffer = etb_free_buffer,
  410. .set_buffer = etb_set_buffer,
  411. .reset_buffer = etb_reset_buffer,
  412. .update_buffer = etb_update_buffer,
  413. };
  414. static const struct coresight_ops etb_cs_ops = {
  415. .sink_ops = &etb_sink_ops,
  416. };
  417. static void etb_dump(struct etb_drvdata *drvdata)
  418. {
  419. unsigned long flags;
  420. spin_lock_irqsave(&drvdata->spinlock, flags);
  421. if (local_read(&drvdata->mode) == CS_MODE_SYSFS) {
  422. etb_disable_hw(drvdata);
  423. etb_dump_hw(drvdata);
  424. etb_enable_hw(drvdata);
  425. }
  426. spin_unlock_irqrestore(&drvdata->spinlock, flags);
  427. dev_info(drvdata->dev, "ETB dumped\n");
  428. }
  429. static int etb_open(struct inode *inode, struct file *file)
  430. {
  431. struct etb_drvdata *drvdata = container_of(file->private_data,
  432. struct etb_drvdata, miscdev);
  433. if (local_cmpxchg(&drvdata->reading, 0, 1))
  434. return -EBUSY;
  435. dev_dbg(drvdata->dev, "%s: successfully opened\n", __func__);
  436. return 0;
  437. }
  438. static ssize_t etb_read(struct file *file, char __user *data,
  439. size_t len, loff_t *ppos)
  440. {
  441. u32 depth;
  442. struct etb_drvdata *drvdata = container_of(file->private_data,
  443. struct etb_drvdata, miscdev);
  444. etb_dump(drvdata);
  445. depth = drvdata->buffer_depth;
  446. if (*ppos + len > depth * 4)
  447. len = depth * 4 - *ppos;
  448. if (copy_to_user(data, drvdata->buf + *ppos, len)) {
  449. dev_dbg(drvdata->dev, "%s: copy_to_user failed\n", __func__);
  450. return -EFAULT;
  451. }
  452. *ppos += len;
  453. dev_dbg(drvdata->dev, "%s: %zu bytes copied, %d bytes left\n",
  454. __func__, len, (int)(depth * 4 - *ppos));
  455. return len;
  456. }
  457. static int etb_release(struct inode *inode, struct file *file)
  458. {
  459. struct etb_drvdata *drvdata = container_of(file->private_data,
  460. struct etb_drvdata, miscdev);
  461. local_set(&drvdata->reading, 0);
  462. dev_dbg(drvdata->dev, "%s: released\n", __func__);
  463. return 0;
  464. }
  465. static const struct file_operations etb_fops = {
  466. .owner = THIS_MODULE,
  467. .open = etb_open,
  468. .read = etb_read,
  469. .release = etb_release,
  470. .llseek = no_llseek,
  471. };
  472. #define coresight_etb10_reg(name, offset) \
  473. coresight_simple_reg32(struct etb_drvdata, name, offset)
  474. coresight_etb10_reg(rdp, ETB_RAM_DEPTH_REG);
  475. coresight_etb10_reg(sts, ETB_STATUS_REG);
  476. coresight_etb10_reg(rrp, ETB_RAM_READ_POINTER);
  477. coresight_etb10_reg(rwp, ETB_RAM_WRITE_POINTER);
  478. coresight_etb10_reg(trg, ETB_TRG);
  479. coresight_etb10_reg(ctl, ETB_CTL_REG);
  480. coresight_etb10_reg(ffsr, ETB_FFSR);
  481. coresight_etb10_reg(ffcr, ETB_FFCR);
  482. static struct attribute *coresight_etb_mgmt_attrs[] = {
  483. &dev_attr_rdp.attr,
  484. &dev_attr_sts.attr,
  485. &dev_attr_rrp.attr,
  486. &dev_attr_rwp.attr,
  487. &dev_attr_trg.attr,
  488. &dev_attr_ctl.attr,
  489. &dev_attr_ffsr.attr,
  490. &dev_attr_ffcr.attr,
  491. NULL,
  492. };
  493. static ssize_t trigger_cntr_show(struct device *dev,
  494. struct device_attribute *attr, char *buf)
  495. {
  496. struct etb_drvdata *drvdata = dev_get_drvdata(dev->parent);
  497. unsigned long val = drvdata->trigger_cntr;
  498. return sprintf(buf, "%#lx\n", val);
  499. }
  500. static ssize_t trigger_cntr_store(struct device *dev,
  501. struct device_attribute *attr,
  502. const char *buf, size_t size)
  503. {
  504. int ret;
  505. unsigned long val;
  506. struct etb_drvdata *drvdata = dev_get_drvdata(dev->parent);
  507. ret = kstrtoul(buf, 16, &val);
  508. if (ret)
  509. return ret;
  510. drvdata->trigger_cntr = val;
  511. return size;
  512. }
  513. static DEVICE_ATTR_RW(trigger_cntr);
  514. static struct attribute *coresight_etb_attrs[] = {
  515. &dev_attr_trigger_cntr.attr,
  516. NULL,
  517. };
  518. static const struct attribute_group coresight_etb_group = {
  519. .attrs = coresight_etb_attrs,
  520. };
  521. static const struct attribute_group coresight_etb_mgmt_group = {
  522. .attrs = coresight_etb_mgmt_attrs,
  523. .name = "mgmt",
  524. };
  525. const struct attribute_group *coresight_etb_groups[] = {
  526. &coresight_etb_group,
  527. &coresight_etb_mgmt_group,
  528. NULL,
  529. };
  530. static int etb_probe(struct amba_device *adev, const struct amba_id *id)
  531. {
  532. int ret;
  533. void __iomem *base;
  534. struct device *dev = &adev->dev;
  535. struct coresight_platform_data *pdata = NULL;
  536. struct etb_drvdata *drvdata;
  537. struct resource *res = &adev->res;
  538. struct coresight_desc desc = { 0 };
  539. struct device_node *np = adev->dev.of_node;
  540. if (np) {
  541. pdata = of_get_coresight_platform_data(dev, np);
  542. if (IS_ERR(pdata))
  543. return PTR_ERR(pdata);
  544. adev->dev.platform_data = pdata;
  545. }
  546. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  547. if (!drvdata)
  548. return -ENOMEM;
  549. drvdata->dev = &adev->dev;
  550. drvdata->atclk = devm_clk_get(&adev->dev, "atclk"); /* optional */
  551. if (!IS_ERR(drvdata->atclk)) {
  552. ret = clk_prepare_enable(drvdata->atclk);
  553. if (ret)
  554. return ret;
  555. }
  556. dev_set_drvdata(dev, drvdata);
  557. /* validity for the resource is already checked by the AMBA core */
  558. base = devm_ioremap_resource(dev, res);
  559. if (IS_ERR(base))
  560. return PTR_ERR(base);
  561. drvdata->base = base;
  562. spin_lock_init(&drvdata->spinlock);
  563. drvdata->buffer_depth = etb_get_buffer_depth(drvdata);
  564. pm_runtime_put(&adev->dev);
  565. if (drvdata->buffer_depth & 0x80000000)
  566. return -EINVAL;
  567. drvdata->buf = devm_kcalloc(dev,
  568. drvdata->buffer_depth, 4, GFP_KERNEL);
  569. if (!drvdata->buf)
  570. return -ENOMEM;
  571. desc.type = CORESIGHT_DEV_TYPE_SINK;
  572. desc.subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER;
  573. desc.ops = &etb_cs_ops;
  574. desc.pdata = pdata;
  575. desc.dev = dev;
  576. desc.groups = coresight_etb_groups;
  577. drvdata->csdev = coresight_register(&desc);
  578. if (IS_ERR(drvdata->csdev))
  579. return PTR_ERR(drvdata->csdev);
  580. drvdata->miscdev.name = pdata->name;
  581. drvdata->miscdev.minor = MISC_DYNAMIC_MINOR;
  582. drvdata->miscdev.fops = &etb_fops;
  583. ret = misc_register(&drvdata->miscdev);
  584. if (ret)
  585. goto err_misc_register;
  586. return 0;
  587. err_misc_register:
  588. coresight_unregister(drvdata->csdev);
  589. return ret;
  590. }
  591. #ifdef CONFIG_PM
  592. static int etb_runtime_suspend(struct device *dev)
  593. {
  594. struct etb_drvdata *drvdata = dev_get_drvdata(dev);
  595. if (drvdata && !IS_ERR(drvdata->atclk))
  596. clk_disable_unprepare(drvdata->atclk);
  597. return 0;
  598. }
  599. static int etb_runtime_resume(struct device *dev)
  600. {
  601. struct etb_drvdata *drvdata = dev_get_drvdata(dev);
  602. if (drvdata && !IS_ERR(drvdata->atclk))
  603. clk_prepare_enable(drvdata->atclk);
  604. return 0;
  605. }
  606. #endif
  607. static const struct dev_pm_ops etb_dev_pm_ops = {
  608. SET_RUNTIME_PM_OPS(etb_runtime_suspend, etb_runtime_resume, NULL)
  609. };
  610. static const struct amba_id etb_ids[] = {
  611. {
  612. .id = 0x000bb907,
  613. .mask = 0x000fffff,
  614. },
  615. { 0, 0},
  616. };
  617. static struct amba_driver etb_driver = {
  618. .drv = {
  619. .name = "coresight-etb10",
  620. .owner = THIS_MODULE,
  621. .pm = &etb_dev_pm_ops,
  622. .suppress_bind_attrs = true,
  623. },
  624. .probe = etb_probe,
  625. .id_table = etb_ids,
  626. };
  627. builtin_amba_driver(etb_driver);