coresight-etb10.c 19 KB

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