coresight-etb10.c 19 KB

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