blob: abb46fb4d1a5d237c380ba1e0e247ad702daedd0 [file] [log] [blame]
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -06001/*
2 * Copyright(C) 2016 Linaro Limited. All rights reserved.
3 * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
Mathieu Poirier2e499bb2016-05-03 11:33:59 -060018#include <linux/circ_buf.h>
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -060019#include <linux/coresight.h>
Mathieu Poirier2e499bb2016-05-03 11:33:59 -060020#include <linux/perf_event.h>
Mathieu Poirierde546192016-05-03 11:33:52 -060021#include <linux/slab.h>
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -060022#include "coresight-priv.h"
23#include "coresight-tmc.h"
24
Baoyou Xie0ef75282016-09-08 16:50:39 -060025static void tmc_etb_enable_hw(struct tmc_drvdata *drvdata)
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -060026{
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -060027 CS_UNLOCK(drvdata->base);
28
29 /* Wait for TMCSReady bit to be set */
30 tmc_wait_for_tmcready(drvdata);
31
32 writel_relaxed(TMC_MODE_CIRCULAR_BUFFER, drvdata->base + TMC_MODE);
33 writel_relaxed(TMC_FFCR_EN_FMT | TMC_FFCR_EN_TI |
34 TMC_FFCR_FON_FLIN | TMC_FFCR_FON_TRIG_EVT |
35 TMC_FFCR_TRIGON_TRIGIN,
36 drvdata->base + TMC_FFCR);
37
38 writel_relaxed(drvdata->trigger_cntr, drvdata->base + TMC_TRG);
39 tmc_enable_hw(drvdata);
40
41 CS_LOCK(drvdata->base);
42}
43
44static void tmc_etb_dump_hw(struct tmc_drvdata *drvdata)
45{
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -060046 char *bufp;
47 u32 read_data;
48 int i;
49
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -060050 bufp = drvdata->buf;
Suzuki K Poulose8505fea2016-08-25 15:18:57 -060051 drvdata->len = 0;
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -060052 while (1) {
Mathieu Poirier4f1ff3d2016-05-03 11:33:57 -060053 for (i = 0; i < drvdata->memwidth; i++) {
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -060054 read_data = readl_relaxed(drvdata->base + TMC_RRD);
55 if (read_data == 0xFFFFFFFF)
56 return;
57 memcpy(bufp, &read_data, 4);
58 bufp += 4;
Suzuki K Poulose8505fea2016-08-25 15:18:57 -060059 drvdata->len += 4;
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -060060 }
61 }
62}
63
Mathieu Poirier45254122016-05-03 11:33:51 -060064static void tmc_etb_disable_hw(struct tmc_drvdata *drvdata)
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -060065{
66 CS_UNLOCK(drvdata->base);
67
68 tmc_flush_and_stop(drvdata);
Mathieu Poiriera40318f2016-05-03 11:33:55 -060069 /*
70 * When operating in sysFS mode the content of the buffer needs to be
71 * read before the TMC is disabled.
72 */
Suzuki K. Pouloseef8490f2016-11-29 09:47:15 -070073 if (drvdata->mode == CS_MODE_SYSFS)
Mathieu Poiriera40318f2016-05-03 11:33:55 -060074 tmc_etb_dump_hw(drvdata);
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -060075 tmc_disable_hw(drvdata);
76
77 CS_LOCK(drvdata->base);
78}
79
80static void tmc_etf_enable_hw(struct tmc_drvdata *drvdata)
81{
82 CS_UNLOCK(drvdata->base);
83
84 /* Wait for TMCSReady bit to be set */
85 tmc_wait_for_tmcready(drvdata);
86
87 writel_relaxed(TMC_MODE_HARDWARE_FIFO, drvdata->base + TMC_MODE);
88 writel_relaxed(TMC_FFCR_EN_FMT | TMC_FFCR_EN_TI,
89 drvdata->base + TMC_FFCR);
90 writel_relaxed(0x0, drvdata->base + TMC_BUFWM);
91 tmc_enable_hw(drvdata);
92
93 CS_LOCK(drvdata->base);
94}
95
96static void tmc_etf_disable_hw(struct tmc_drvdata *drvdata)
97{
98 CS_UNLOCK(drvdata->base);
99
100 tmc_flush_and_stop(drvdata);
101 tmc_disable_hw(drvdata);
102
103 CS_LOCK(drvdata->base);
104}
105
Suzuki K. Poulosed61eabc2016-11-29 09:47:16 -0700106static int tmc_enable_etf_sink_sysfs(struct coresight_device *csdev)
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -0600107{
Mathieu Poirierde546192016-05-03 11:33:52 -0600108 int ret = 0;
109 bool used = false;
110 char *buf = NULL;
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -0600111 unsigned long flags;
112 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
113
Mathieu Poirierde546192016-05-03 11:33:52 -0600114 /*
115 * If we don't have a buffer release the lock and allocate memory.
116 * Otherwise keep the lock and move along.
117 */
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -0600118 spin_lock_irqsave(&drvdata->spinlock, flags);
Mathieu Poirierde546192016-05-03 11:33:52 -0600119 if (!drvdata->buf) {
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -0600120 spin_unlock_irqrestore(&drvdata->spinlock, flags);
Mathieu Poirierde546192016-05-03 11:33:52 -0600121
122 /* Allocating the memory here while outside of the spinlock */
123 buf = kzalloc(drvdata->size, GFP_KERNEL);
124 if (!buf)
125 return -ENOMEM;
126
127 /* Let's try again */
128 spin_lock_irqsave(&drvdata->spinlock, flags);
129 }
130
131 if (drvdata->reading) {
132 ret = -EBUSY;
133 goto out;
134 }
135
Mathieu Poirierf2facc32016-05-03 11:33:54 -0600136 /*
137 * In sysFS mode we can have multiple writers per sink. Since this
138 * sink is already enabled no memory is needed and the HW need not be
139 * touched.
140 */
Suzuki K. Pouloseef8490f2016-11-29 09:47:15 -0700141 if (drvdata->mode == CS_MODE_SYSFS)
Mathieu Poirierf2facc32016-05-03 11:33:54 -0600142 goto out;
143
Mathieu Poirierde546192016-05-03 11:33:52 -0600144 /*
145 * If drvdata::buf isn't NULL, memory was allocated for a previous
146 * trace run but wasn't read. If so simply zero-out the memory.
147 * Otherwise use the memory allocated above.
148 *
149 * The memory is freed when users read the buffer using the
150 * /dev/xyz.{etf|etb} interface. See tmc_read_unprepare_etf() for
151 * details.
152 */
153 if (drvdata->buf) {
154 memset(drvdata->buf, 0, drvdata->size);
155 } else {
156 used = true;
157 drvdata->buf = buf;
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -0600158 }
159
Suzuki K. Pouloseef8490f2016-11-29 09:47:15 -0700160 drvdata->mode = CS_MODE_SYSFS;
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -0600161 tmc_etb_enable_hw(drvdata);
Mathieu Poirierde546192016-05-03 11:33:52 -0600162out:
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -0600163 spin_unlock_irqrestore(&drvdata->spinlock, flags);
164
Mathieu Poirierde546192016-05-03 11:33:52 -0600165 /* Free memory outside the spinlock if need be */
Markus Elfring1d37ae52016-08-25 15:19:07 -0600166 if (!used)
Mathieu Poirierde546192016-05-03 11:33:52 -0600167 kfree(buf);
168
Satyajit Desai9e279d42017-07-18 11:30:53 -0700169 if (!ret) {
170 coresight_cti_map_trigin(drvdata->cti_reset, 2, 0);
171 coresight_cti_map_trigout(drvdata->cti_flush, 1, 0);
Mathieu Poirierde546192016-05-03 11:33:52 -0600172 dev_info(drvdata->dev, "TMC-ETB/ETF enabled\n");
Satyajit Desai9e279d42017-07-18 11:30:53 -0700173 }
Mathieu Poirierde546192016-05-03 11:33:52 -0600174
175 return ret;
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -0600176}
177
Suzuki K. Poulosed61eabc2016-11-29 09:47:16 -0700178static int tmc_enable_etf_sink_perf(struct coresight_device *csdev)
Mathieu Poirierb2176012016-05-03 11:33:56 -0600179{
180 int ret = 0;
Mathieu Poirierb2176012016-05-03 11:33:56 -0600181 unsigned long flags;
182 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
183
Mathieu Poirierb2176012016-05-03 11:33:56 -0600184 spin_lock_irqsave(&drvdata->spinlock, flags);
185 if (drvdata->reading) {
186 ret = -EINVAL;
187 goto out;
188 }
189
Mathieu Poirierb2176012016-05-03 11:33:56 -0600190 /*
191 * In Perf mode there can be only one writer per sink. There
192 * is also no need to continue if the ETB/ETR is already operated
193 * from sysFS.
194 */
Suzuki K. Pouloseef8490f2016-11-29 09:47:15 -0700195 if (drvdata->mode != CS_MODE_DISABLED) {
Mathieu Poirierb2176012016-05-03 11:33:56 -0600196 ret = -EINVAL;
197 goto out;
198 }
199
Suzuki K. Poulosed61eabc2016-11-29 09:47:16 -0700200 drvdata->mode = CS_MODE_PERF;
Mathieu Poirierb2176012016-05-03 11:33:56 -0600201 tmc_etb_enable_hw(drvdata);
202out:
203 spin_unlock_irqrestore(&drvdata->spinlock, flags);
204
205 return ret;
206}
207
208static int tmc_enable_etf_sink(struct coresight_device *csdev, u32 mode)
209{
210 switch (mode) {
211 case CS_MODE_SYSFS:
Suzuki K. Poulosed61eabc2016-11-29 09:47:16 -0700212 return tmc_enable_etf_sink_sysfs(csdev);
Mathieu Poirierb2176012016-05-03 11:33:56 -0600213 case CS_MODE_PERF:
Suzuki K. Poulosed61eabc2016-11-29 09:47:16 -0700214 return tmc_enable_etf_sink_perf(csdev);
Mathieu Poirierb2176012016-05-03 11:33:56 -0600215 }
216
217 /* We shouldn't be here */
218 return -EINVAL;
219}
220
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -0600221static void tmc_disable_etf_sink(struct coresight_device *csdev)
222{
223 unsigned long flags;
224 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
225
226 spin_lock_irqsave(&drvdata->spinlock, flags);
227 if (drvdata->reading) {
228 spin_unlock_irqrestore(&drvdata->spinlock, flags);
229 return;
230 }
231
Mathieu Poirierf2facc32016-05-03 11:33:54 -0600232 /* Disable the TMC only if it needs to */
Suzuki K. Pouloseef8490f2016-11-29 09:47:15 -0700233 if (drvdata->mode != CS_MODE_DISABLED) {
Mathieu Poirierf2facc32016-05-03 11:33:54 -0600234 tmc_etb_disable_hw(drvdata);
Suzuki K. Pouloseef8490f2016-11-29 09:47:15 -0700235 drvdata->mode = CS_MODE_DISABLED;
236 }
Mathieu Poirierf2facc32016-05-03 11:33:54 -0600237
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -0600238 spin_unlock_irqrestore(&drvdata->spinlock, flags);
239
Satyajit Desai9e279d42017-07-18 11:30:53 -0700240 coresight_cti_unmap_trigin(drvdata->cti_reset, 2, 0);
241 coresight_cti_unmap_trigout(drvdata->cti_flush, 1, 0);
242
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -0600243 dev_info(drvdata->dev, "TMC-ETB/ETF disabled\n");
244}
245
246static int tmc_enable_etf_link(struct coresight_device *csdev,
247 int inport, int outport)
248{
249 unsigned long flags;
250 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
251
252 spin_lock_irqsave(&drvdata->spinlock, flags);
253 if (drvdata->reading) {
254 spin_unlock_irqrestore(&drvdata->spinlock, flags);
255 return -EBUSY;
256 }
257
258 tmc_etf_enable_hw(drvdata);
Suzuki K. Pouloseef8490f2016-11-29 09:47:15 -0700259 drvdata->mode = CS_MODE_SYSFS;
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -0600260 spin_unlock_irqrestore(&drvdata->spinlock, flags);
261
262 dev_info(drvdata->dev, "TMC-ETF enabled\n");
263 return 0;
264}
265
266static void tmc_disable_etf_link(struct coresight_device *csdev,
267 int inport, int outport)
268{
269 unsigned long flags;
270 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
271
272 spin_lock_irqsave(&drvdata->spinlock, flags);
273 if (drvdata->reading) {
274 spin_unlock_irqrestore(&drvdata->spinlock, flags);
275 return;
276 }
277
278 tmc_etf_disable_hw(drvdata);
Suzuki K. Pouloseef8490f2016-11-29 09:47:15 -0700279 drvdata->mode = CS_MODE_DISABLED;
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -0600280 spin_unlock_irqrestore(&drvdata->spinlock, flags);
281
282 dev_info(drvdata->dev, "TMC disabled\n");
283}
284
Mathieu Poirier2e499bb2016-05-03 11:33:59 -0600285static void *tmc_alloc_etf_buffer(struct coresight_device *csdev, int cpu,
286 void **pages, int nr_pages, bool overwrite)
287{
288 int node;
289 struct cs_buffers *buf;
290
Suzuki K Poulosec26777a2019-06-20 16:12:35 -0600291 node = (cpu == -1) ? NUMA_NO_NODE : cpu_to_node(cpu);
Mathieu Poirier2e499bb2016-05-03 11:33:59 -0600292
293 /* Allocate memory structure for interaction with Perf */
294 buf = kzalloc_node(sizeof(struct cs_buffers), GFP_KERNEL, node);
295 if (!buf)
296 return NULL;
297
298 buf->snapshot = overwrite;
299 buf->nr_pages = nr_pages;
300 buf->data_pages = pages;
301
302 return buf;
303}
304
305static void tmc_free_etf_buffer(void *config)
306{
307 struct cs_buffers *buf = config;
308
309 kfree(buf);
310}
311
312static int tmc_set_etf_buffer(struct coresight_device *csdev,
313 struct perf_output_handle *handle,
314 void *sink_config)
315{
316 int ret = 0;
317 unsigned long head;
318 struct cs_buffers *buf = sink_config;
319
320 /* wrap head around to the amount of space we have */
321 head = handle->head & ((buf->nr_pages << PAGE_SHIFT) - 1);
322
323 /* find the page to write to */
324 buf->cur = head / PAGE_SIZE;
325
326 /* and offset within that page */
327 buf->offset = head % PAGE_SIZE;
328
329 local_set(&buf->data_size, 0);
330
331 return ret;
332}
333
334static unsigned long tmc_reset_etf_buffer(struct coresight_device *csdev,
335 struct perf_output_handle *handle,
336 void *sink_config, bool *lost)
337{
338 long size = 0;
339 struct cs_buffers *buf = sink_config;
340
341 if (buf) {
342 /*
343 * In snapshot mode ->data_size holds the new address of the
344 * ring buffer's head. The size itself is the whole address
345 * range since we want the latest information.
346 */
347 if (buf->snapshot)
348 handle->head = local_xchg(&buf->data_size,
349 buf->nr_pages << PAGE_SHIFT);
350 /*
351 * Tell the tracer PMU how much we got in this run and if
352 * something went wrong along the way. Nobody else can use
353 * this cs_buffers instance until we are done. As such
354 * resetting parameters here and squaring off with the ring
355 * buffer API in the tracer PMU is fine.
356 */
357 *lost = !!local_xchg(&buf->lost, 0);
358 size = local_xchg(&buf->data_size, 0);
359 }
360
361 return size;
362}
363
364static void tmc_update_etf_buffer(struct coresight_device *csdev,
365 struct perf_output_handle *handle,
366 void *sink_config)
367{
368 int i, cur;
369 u32 *buf_ptr;
370 u32 read_ptr, write_ptr;
371 u32 status, to_read;
372 unsigned long offset;
373 struct cs_buffers *buf = sink_config;
374 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
375
376 if (!buf)
377 return;
378
379 /* This shouldn't happen */
Suzuki K. Pouloseef8490f2016-11-29 09:47:15 -0700380 if (WARN_ON_ONCE(drvdata->mode != CS_MODE_PERF))
Mathieu Poirier2e499bb2016-05-03 11:33:59 -0600381 return;
382
383 CS_UNLOCK(drvdata->base);
384
385 tmc_flush_and_stop(drvdata);
386
387 read_ptr = readl_relaxed(drvdata->base + TMC_RRP);
388 write_ptr = readl_relaxed(drvdata->base + TMC_RWP);
389
390 /*
391 * Get a hold of the status register and see if a wrap around
392 * has occurred. If so adjust things accordingly.
393 */
394 status = readl_relaxed(drvdata->base + TMC_STS);
395 if (status & TMC_STS_FULL) {
396 local_inc(&buf->lost);
397 to_read = drvdata->size;
398 } else {
399 to_read = CIRC_CNT(write_ptr, read_ptr, drvdata->size);
400 }
401
402 /*
403 * The TMC RAM buffer may be bigger than the space available in the
404 * perf ring buffer (handle->size). If so advance the RRP so that we
405 * get the latest trace data.
406 */
407 if (to_read > handle->size) {
408 u32 mask = 0;
409
410 /*
411 * The value written to RRP must be byte-address aligned to
412 * the width of the trace memory databus _and_ to a frame
413 * boundary (16 byte), whichever is the biggest. For example,
414 * for 32-bit, 64-bit and 128-bit wide trace memory, the four
415 * LSBs must be 0s. For 256-bit wide trace memory, the five
416 * LSBs must be 0s.
417 */
418 switch (drvdata->memwidth) {
419 case TMC_MEM_INTF_WIDTH_32BITS:
420 case TMC_MEM_INTF_WIDTH_64BITS:
421 case TMC_MEM_INTF_WIDTH_128BITS:
Leo Yan8c3d23b2018-09-20 13:18:02 -0600422 mask = GENMASK(31, 4);
Mathieu Poirier2e499bb2016-05-03 11:33:59 -0600423 break;
424 case TMC_MEM_INTF_WIDTH_256BITS:
Leo Yan8c3d23b2018-09-20 13:18:02 -0600425 mask = GENMASK(31, 5);
Mathieu Poirier2e499bb2016-05-03 11:33:59 -0600426 break;
427 }
428
429 /*
430 * Make sure the new size is aligned in accordance with the
431 * requirement explained above.
432 */
433 to_read = handle->size & mask;
434 /* Move the RAM read pointer up */
435 read_ptr = (write_ptr + drvdata->size) - to_read;
436 /* Make sure we are still within our limits */
437 if (read_ptr > (drvdata->size - 1))
438 read_ptr -= drvdata->size;
439 /* Tell the HW */
440 writel_relaxed(read_ptr, drvdata->base + TMC_RRP);
441 local_inc(&buf->lost);
442 }
443
444 cur = buf->cur;
445 offset = buf->offset;
446
447 /* for every byte to read */
448 for (i = 0; i < to_read; i += 4) {
449 buf_ptr = buf->data_pages[cur] + offset;
450 *buf_ptr = readl_relaxed(drvdata->base + TMC_RRD);
451
452 offset += 4;
453 if (offset >= PAGE_SIZE) {
454 offset = 0;
455 cur++;
456 /* wrap around at the end of the buffer */
457 cur &= buf->nr_pages - 1;
458 }
459 }
460
461 /*
462 * In snapshot mode all we have to do is communicate to
463 * perf_aux_output_end() the address of the current head. In full
464 * trace mode the same function expects a size to move rb->aux_head
465 * forward.
466 */
467 if (buf->snapshot)
468 local_set(&buf->data_size, (cur * PAGE_SIZE) + offset);
469 else
470 local_add(to_read, &buf->data_size);
471
472 CS_LOCK(drvdata->base);
473}
474
Rama Aparna Mallavarapud16ae5d2017-06-21 17:49:37 -0700475static void tmc_abort_etf_sink(struct coresight_device *csdev)
476{
477 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
478 unsigned long flags;
479 enum tmc_mode mode;
480
481 spin_lock_irqsave(&drvdata->spinlock, flags);
482 if (drvdata->reading)
483 goto out0;
484
485 if (drvdata->config_type == TMC_CONFIG_TYPE_ETB) {
486 tmc_etb_disable_hw(drvdata);
487 } else {
488
489 mode = readl_relaxed(drvdata->base + TMC_MODE);
490 if (mode == TMC_MODE_CIRCULAR_BUFFER)
491 tmc_etb_disable_hw(drvdata);
492 else
493 goto out1;
494 }
495out0:
496 drvdata->enable = false;
497 spin_unlock_irqrestore(&drvdata->spinlock, flags);
498
499 dev_info(drvdata->dev, "TMC aborted\n");
500 return;
501out1:
502 spin_unlock_irqrestore(&drvdata->spinlock, flags);
503}
504
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -0600505static const struct coresight_ops_sink tmc_etf_sink_ops = {
506 .enable = tmc_enable_etf_sink,
507 .disable = tmc_disable_etf_sink,
Mathieu Poirier2e499bb2016-05-03 11:33:59 -0600508 .alloc_buffer = tmc_alloc_etf_buffer,
509 .free_buffer = tmc_free_etf_buffer,
510 .set_buffer = tmc_set_etf_buffer,
511 .reset_buffer = tmc_reset_etf_buffer,
512 .update_buffer = tmc_update_etf_buffer,
Rama Aparna Mallavarapud16ae5d2017-06-21 17:49:37 -0700513 .abort = tmc_abort_etf_sink,
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -0600514};
515
516static const struct coresight_ops_link tmc_etf_link_ops = {
517 .enable = tmc_enable_etf_link,
518 .disable = tmc_disable_etf_link,
519};
520
521const struct coresight_ops tmc_etb_cs_ops = {
522 .sink_ops = &tmc_etf_sink_ops,
523};
524
525const struct coresight_ops tmc_etf_cs_ops = {
526 .sink_ops = &tmc_etf_sink_ops,
527 .link_ops = &tmc_etf_link_ops,
528};
Mathieu Poirier45254122016-05-03 11:33:51 -0600529
530int tmc_read_prepare_etb(struct tmc_drvdata *drvdata)
531{
532 enum tmc_mode mode;
533 int ret = 0;
534 unsigned long flags;
535
536 /* config types are set a boot time and never change */
537 if (WARN_ON_ONCE(drvdata->config_type != TMC_CONFIG_TYPE_ETB &&
538 drvdata->config_type != TMC_CONFIG_TYPE_ETF))
539 return -EINVAL;
540
541 spin_lock_irqsave(&drvdata->spinlock, flags);
542
Mathieu Poirierf74debb2016-05-03 11:33:53 -0600543 if (drvdata->reading) {
544 ret = -EBUSY;
545 goto out;
546 }
547
Rama Aparna Mallavarapu47d9cae2017-08-01 17:00:12 -0700548 if (drvdata->enable) {
549 /* There is no point in reading a TMC in HW FIFO mode */
550 mode = readl_relaxed(drvdata->base + TMC_MODE);
551 if (mode != TMC_MODE_CIRCULAR_BUFFER) {
552 ret = -EINVAL;
553 goto out;
554 }
Mathieu Poirier45254122016-05-03 11:33:51 -0600555 }
556
Mathieu Poirierb2176012016-05-03 11:33:56 -0600557 /* Don't interfere if operated from Perf */
Suzuki K. Pouloseef8490f2016-11-29 09:47:15 -0700558 if (drvdata->mode == CS_MODE_PERF) {
Mathieu Poirierb2176012016-05-03 11:33:56 -0600559 ret = -EINVAL;
560 goto out;
561 }
562
Mathieu Poirierde546192016-05-03 11:33:52 -0600563 /* If drvdata::buf is NULL the trace data has been read already */
564 if (drvdata->buf == NULL) {
565 ret = -EINVAL;
566 goto out;
567 }
568
Mathieu Poirier45254122016-05-03 11:33:51 -0600569 /* Disable the TMC if need be */
Suzuki K. Pouloseef8490f2016-11-29 09:47:15 -0700570 if (drvdata->mode == CS_MODE_SYSFS)
Mathieu Poirier45254122016-05-03 11:33:51 -0600571 tmc_etb_disable_hw(drvdata);
572
573 drvdata->reading = true;
574out:
575 spin_unlock_irqrestore(&drvdata->spinlock, flags);
576
577 return ret;
578}
579
580int tmc_read_unprepare_etb(struct tmc_drvdata *drvdata)
581{
Mathieu Poirierde546192016-05-03 11:33:52 -0600582 char *buf = NULL;
Mathieu Poirier45254122016-05-03 11:33:51 -0600583 enum tmc_mode mode;
584 unsigned long flags;
585
586 /* config types are set a boot time and never change */
587 if (WARN_ON_ONCE(drvdata->config_type != TMC_CONFIG_TYPE_ETB &&
588 drvdata->config_type != TMC_CONFIG_TYPE_ETF))
589 return -EINVAL;
590
591 spin_lock_irqsave(&drvdata->spinlock, flags);
592
Rama Aparna Mallavarapu47d9cae2017-08-01 17:00:12 -0700593 if (drvdata->enable) {
594 /* There is no point in reading a TMC in HW FIFO mode */
595 mode = readl_relaxed(drvdata->base + TMC_MODE);
596 if (mode != TMC_MODE_CIRCULAR_BUFFER) {
597 spin_unlock_irqrestore(&drvdata->spinlock, flags);
598 return -EINVAL;
599 }
Mathieu Poirier45254122016-05-03 11:33:51 -0600600 }
601
602 /* Re-enable the TMC if need be */
Suzuki K. Pouloseef8490f2016-11-29 09:47:15 -0700603 if (drvdata->mode == CS_MODE_SYSFS) {
Mathieu Poirierde546192016-05-03 11:33:52 -0600604 /*
605 * The trace run will continue with the same allocated trace
606 * buffer. As such zero-out the buffer so that we don't end
607 * up with stale data.
608 *
609 * Since the tracer is still enabled drvdata::buf
610 * can't be NULL.
611 */
612 memset(drvdata->buf, 0, drvdata->size);
Mathieu Poirier45254122016-05-03 11:33:51 -0600613 tmc_etb_enable_hw(drvdata);
Mathieu Poirierde546192016-05-03 11:33:52 -0600614 } else {
615 /*
616 * The ETB/ETF is not tracing and the buffer was just read.
617 * As such prepare to free the trace buffer.
618 */
619 buf = drvdata->buf;
620 drvdata->buf = NULL;
621 }
Mathieu Poirier45254122016-05-03 11:33:51 -0600622
623 drvdata->reading = false;
624 spin_unlock_irqrestore(&drvdata->spinlock, flags);
625
Mathieu Poirierde546192016-05-03 11:33:52 -0600626 /*
627 * Free allocated memory outside of the spinlock. There is no need
628 * to assert the validity of 'buf' since calling kfree(NULL) is safe.
629 */
630 kfree(buf);
631
Mathieu Poirier45254122016-05-03 11:33:51 -0600632 return 0;
633}