1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright(C) 2016 Linaro Limited. All rights reserved.
4 * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
7 #include <linux/atomic.h>
8 #include <linux/coresight.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/iommu.h>
11 #include <linux/idr.h>
12 #include <linux/mutex.h>
13 #include <linux/refcount.h>
14 #include <linux/slab.h>
15 #include <linux/types.h>
16 #include <linux/vmalloc.h>
17 #include "coresight-catu.h"
18 #include "coresight-etm-perf.h"
19 #include "coresight-priv.h"
20 #include "coresight-tmc.h"
30 * etr_perf_buffer - Perf buffer used for ETR
31 * @drvdata - The ETR drvdaga this buffer has been allocated for.
32 * @etr_buf - Actual buffer used by the ETR
33 * @pid - The PID this etr_perf_buffer belongs to.
34 * @snaphost - Perf session mode
35 * @nr_pages - Number of pages in the ring buffer.
36 * @pages - Array of Pages in the ring buffer.
38 struct etr_perf_buffer {
39 struct tmc_drvdata *drvdata;
40 struct etr_buf *etr_buf;
47 /* Convert the perf index to an offset within the ETR buffer */
48 #define PERF_IDX2OFF(idx, buf) \
49 ((idx) % ((unsigned long)(buf)->nr_pages << PAGE_SHIFT))
51 /* Lower limit for ETR hardware buffer */
52 #define TMC_ETR_PERF_MIN_BUF_SIZE SZ_1M
55 * The TMC ETR SG has a page size of 4K. The SG table contains pointers
56 * to 4KB buffers. However, the OS may use a PAGE_SIZE different from
57 * 4K (i.e, 16KB or 64KB). This implies that a single OS page could
58 * contain more than one SG buffer and tables.
60 * A table entry has the following format:
62 * ---Bit31------------Bit4-------Bit1-----Bit0--
63 * | Address[39:12] | SBZ | Entry Type |
64 * ----------------------------------------------
66 * Address: Bits [39:12] of a physical page address. Bits [11:0] are
71 * b01 - Last entry in the tables, points to 4K page buffer.
72 * b10 - Normal entry, points to 4K page buffer.
73 * b11 - Link. The address points to the base of next table.
78 #define ETR_SG_PAGE_SHIFT 12
79 #define ETR_SG_PAGE_SIZE (1UL << ETR_SG_PAGE_SHIFT)
80 #define ETR_SG_PAGES_PER_SYSPAGE (PAGE_SIZE / ETR_SG_PAGE_SIZE)
81 #define ETR_SG_PTRS_PER_PAGE (ETR_SG_PAGE_SIZE / sizeof(sgte_t))
82 #define ETR_SG_PTRS_PER_SYSPAGE (PAGE_SIZE / sizeof(sgte_t))
84 #define ETR_SG_ET_MASK 0x3
85 #define ETR_SG_ET_LAST 0x1
86 #define ETR_SG_ET_NORMAL 0x2
87 #define ETR_SG_ET_LINK 0x3
89 #define ETR_SG_ADDR_SHIFT 4
91 #define ETR_SG_ENTRY(addr, type) \
92 (sgte_t)((((addr) >> ETR_SG_PAGE_SHIFT) << ETR_SG_ADDR_SHIFT) | \
93 (type & ETR_SG_ET_MASK))
95 #define ETR_SG_ADDR(entry) \
96 (((dma_addr_t)(entry) >> ETR_SG_ADDR_SHIFT) << ETR_SG_PAGE_SHIFT)
97 #define ETR_SG_ET(entry) ((entry) & ETR_SG_ET_MASK)
100 * struct etr_sg_table : ETR SG Table
101 * @sg_table: Generic SG Table holding the data/table pages.
102 * @hwaddr: hwaddress used by the TMC, which is the base
103 * address of the table.
105 struct etr_sg_table {
106 struct tmc_sg_table *sg_table;
111 * tmc_etr_sg_table_entries: Total number of table entries required to map
112 * @nr_pages system pages.
114 * We need to map @nr_pages * ETR_SG_PAGES_PER_SYSPAGE data pages.
115 * Each TMC page can map (ETR_SG_PTRS_PER_PAGE - 1) buffer pointers,
116 * with the last entry pointing to another page of table entries.
117 * If we spill over to a new page for mapping 1 entry, we could as
118 * well replace the link entry of the previous page with the last entry.
120 static inline unsigned long __attribute_const__
121 tmc_etr_sg_table_entries(int nr_pages)
123 unsigned long nr_sgpages = nr_pages * ETR_SG_PAGES_PER_SYSPAGE;
124 unsigned long nr_sglinks = nr_sgpages / (ETR_SG_PTRS_PER_PAGE - 1);
126 * If we spill over to a new page for 1 entry, we could as well
127 * make it the LAST entry in the previous page, skipping the Link
130 if (nr_sglinks && (nr_sgpages % (ETR_SG_PTRS_PER_PAGE - 1) < 2))
132 return nr_sgpages + nr_sglinks;
136 * tmc_pages_get_offset: Go through all the pages in the tmc_pages
137 * and map the device address @addr to an offset within the virtual
141 tmc_pages_get_offset(struct tmc_pages *tmc_pages, dma_addr_t addr)
144 dma_addr_t page_start;
146 for (i = 0; i < tmc_pages->nr_pages; i++) {
147 page_start = tmc_pages->daddrs[i];
148 if (addr >= page_start && addr < (page_start + PAGE_SIZE))
149 return i * PAGE_SIZE + (addr - page_start);
156 * tmc_pages_free : Unmap and free the pages used by tmc_pages.
157 * If the pages were not allocated in tmc_pages_alloc(), we would
158 * simply drop the refcount.
160 static void tmc_pages_free(struct tmc_pages *tmc_pages,
161 struct device *dev, enum dma_data_direction dir)
164 struct device *real_dev = dev->parent;
166 for (i = 0; i < tmc_pages->nr_pages; i++) {
167 if (tmc_pages->daddrs && tmc_pages->daddrs[i])
168 dma_unmap_page(real_dev, tmc_pages->daddrs[i],
170 if (tmc_pages->pages && tmc_pages->pages[i])
171 __free_page(tmc_pages->pages[i]);
174 kfree(tmc_pages->pages);
175 kfree(tmc_pages->daddrs);
176 tmc_pages->pages = NULL;
177 tmc_pages->daddrs = NULL;
178 tmc_pages->nr_pages = 0;
182 * tmc_pages_alloc : Allocate and map pages for a given @tmc_pages.
183 * If @pages is not NULL, the list of page virtual addresses are
184 * used as the data pages. The pages are then dma_map'ed for @dev
185 * with dma_direction @dir.
187 * Returns 0 upon success, else the error number.
189 static int tmc_pages_alloc(struct tmc_pages *tmc_pages,
190 struct device *dev, int node,
191 enum dma_data_direction dir, void **pages)
196 struct device *real_dev = dev->parent;
198 nr_pages = tmc_pages->nr_pages;
199 tmc_pages->daddrs = kcalloc(nr_pages, sizeof(*tmc_pages->daddrs),
201 if (!tmc_pages->daddrs)
203 tmc_pages->pages = kcalloc(nr_pages, sizeof(*tmc_pages->pages),
205 if (!tmc_pages->pages) {
206 kfree(tmc_pages->daddrs);
207 tmc_pages->daddrs = NULL;
211 for (i = 0; i < nr_pages; i++) {
212 if (pages && pages[i]) {
213 page = virt_to_page(pages[i]);
214 /* Hold a refcount on the page */
217 page = alloc_pages_node(node,
218 GFP_KERNEL | __GFP_ZERO, 0);
222 paddr = dma_map_page(real_dev, page, 0, PAGE_SIZE, dir);
223 if (dma_mapping_error(real_dev, paddr))
225 tmc_pages->daddrs[i] = paddr;
226 tmc_pages->pages[i] = page;
230 tmc_pages_free(tmc_pages, dev, dir);
235 tmc_sg_get_data_page_offset(struct tmc_sg_table *sg_table, dma_addr_t addr)
237 return tmc_pages_get_offset(&sg_table->data_pages, addr);
240 static inline void tmc_free_table_pages(struct tmc_sg_table *sg_table)
242 if (sg_table->table_vaddr)
243 vunmap(sg_table->table_vaddr);
244 tmc_pages_free(&sg_table->table_pages, sg_table->dev, DMA_TO_DEVICE);
247 static void tmc_free_data_pages(struct tmc_sg_table *sg_table)
249 if (sg_table->data_vaddr)
250 vunmap(sg_table->data_vaddr);
251 tmc_pages_free(&sg_table->data_pages, sg_table->dev, DMA_FROM_DEVICE);
254 void tmc_free_sg_table(struct tmc_sg_table *sg_table)
256 tmc_free_table_pages(sg_table);
257 tmc_free_data_pages(sg_table);
259 EXPORT_SYMBOL_GPL(tmc_free_sg_table);
262 * Alloc pages for the table. Since this will be used by the device,
263 * allocate the pages closer to the device (i.e, dev_to_node(dev)
264 * rather than the CPU node).
266 static int tmc_alloc_table_pages(struct tmc_sg_table *sg_table)
269 struct tmc_pages *table_pages = &sg_table->table_pages;
271 rc = tmc_pages_alloc(table_pages, sg_table->dev,
272 dev_to_node(sg_table->dev),
273 DMA_TO_DEVICE, NULL);
276 sg_table->table_vaddr = vmap(table_pages->pages,
277 table_pages->nr_pages,
280 if (!sg_table->table_vaddr)
283 sg_table->table_daddr = table_pages->daddrs[0];
287 static int tmc_alloc_data_pages(struct tmc_sg_table *sg_table, void **pages)
291 /* Allocate data pages on the node requested by the caller */
292 rc = tmc_pages_alloc(&sg_table->data_pages,
293 sg_table->dev, sg_table->node,
294 DMA_FROM_DEVICE, pages);
296 sg_table->data_vaddr = vmap(sg_table->data_pages.pages,
297 sg_table->data_pages.nr_pages,
300 if (!sg_table->data_vaddr)
307 * tmc_alloc_sg_table: Allocate and setup dma pages for the TMC SG table
308 * and data buffers. TMC writes to the data buffers and reads from the SG
311 * @dev - Coresight device to which page should be DMA mapped.
312 * @node - Numa node for mem allocations
313 * @nr_tpages - Number of pages for the table entries.
314 * @nr_dpages - Number of pages for Data buffer.
315 * @pages - Optional list of virtual address of pages.
317 struct tmc_sg_table *tmc_alloc_sg_table(struct device *dev,
324 struct tmc_sg_table *sg_table;
326 sg_table = kzalloc(sizeof(*sg_table), GFP_KERNEL);
328 return ERR_PTR(-ENOMEM);
329 sg_table->data_pages.nr_pages = nr_dpages;
330 sg_table->table_pages.nr_pages = nr_tpages;
331 sg_table->node = node;
334 rc = tmc_alloc_data_pages(sg_table, pages);
336 rc = tmc_alloc_table_pages(sg_table);
338 tmc_free_sg_table(sg_table);
345 EXPORT_SYMBOL_GPL(tmc_alloc_sg_table);
348 * tmc_sg_table_sync_data_range: Sync the data buffer written
349 * by the device from @offset upto a @size bytes.
351 void tmc_sg_table_sync_data_range(struct tmc_sg_table *table,
352 u64 offset, u64 size)
355 int npages = DIV_ROUND_UP(size, PAGE_SIZE);
356 struct device *real_dev = table->dev->parent;
357 struct tmc_pages *data = &table->data_pages;
359 start = offset >> PAGE_SHIFT;
360 for (i = start; i < (start + npages); i++) {
361 index = i % data->nr_pages;
362 dma_sync_single_for_cpu(real_dev, data->daddrs[index],
363 PAGE_SIZE, DMA_FROM_DEVICE);
366 EXPORT_SYMBOL_GPL(tmc_sg_table_sync_data_range);
368 /* tmc_sg_sync_table: Sync the page table */
369 void tmc_sg_table_sync_table(struct tmc_sg_table *sg_table)
372 struct device *real_dev = sg_table->dev->parent;
373 struct tmc_pages *table_pages = &sg_table->table_pages;
375 for (i = 0; i < table_pages->nr_pages; i++)
376 dma_sync_single_for_device(real_dev, table_pages->daddrs[i],
377 PAGE_SIZE, DMA_TO_DEVICE);
379 EXPORT_SYMBOL_GPL(tmc_sg_table_sync_table);
382 * tmc_sg_table_get_data: Get the buffer pointer for data @offset
383 * in the SG buffer. The @bufpp is updated to point to the buffer.
385 * the length of linear data available at @offset.
387 * <= 0 if no data is available.
389 ssize_t tmc_sg_table_get_data(struct tmc_sg_table *sg_table,
390 u64 offset, size_t len, char **bufpp)
393 int pg_idx = offset >> PAGE_SHIFT;
394 int pg_offset = offset & (PAGE_SIZE - 1);
395 struct tmc_pages *data_pages = &sg_table->data_pages;
397 size = tmc_sg_table_buf_size(sg_table);
401 /* Make sure we don't go beyond the end */
402 len = (len < (size - offset)) ? len : size - offset;
403 /* Respect the page boundaries */
404 len = (len < (PAGE_SIZE - pg_offset)) ? len : (PAGE_SIZE - pg_offset);
406 *bufpp = page_address(data_pages->pages[pg_idx]) + pg_offset;
409 EXPORT_SYMBOL_GPL(tmc_sg_table_get_data);
412 /* Map a dma address to virtual address */
414 tmc_sg_daddr_to_vaddr(struct tmc_sg_table *sg_table,
415 dma_addr_t addr, bool table)
419 struct tmc_pages *tmc_pages;
422 tmc_pages = &sg_table->table_pages;
423 base = (unsigned long)sg_table->table_vaddr;
425 tmc_pages = &sg_table->data_pages;
426 base = (unsigned long)sg_table->data_vaddr;
429 offset = tmc_pages_get_offset(tmc_pages, addr);
432 return base + offset;
435 /* Dump the given sg_table */
436 static void tmc_etr_sg_table_dump(struct etr_sg_table *etr_table)
441 struct tmc_sg_table *sg_table = etr_table->sg_table;
443 ptr = (sgte_t *)tmc_sg_daddr_to_vaddr(sg_table,
444 etr_table->hwaddr, true);
446 addr = ETR_SG_ADDR(*ptr);
447 switch (ETR_SG_ET(*ptr)) {
448 case ETR_SG_ET_NORMAL:
449 dev_dbg(sg_table->dev,
450 "%05d: %p\t:[N] 0x%llx\n", i, ptr, addr);
454 dev_dbg(sg_table->dev,
455 "%05d: *** %p\t:{L} 0x%llx ***\n",
457 ptr = (sgte_t *)tmc_sg_daddr_to_vaddr(sg_table,
461 dev_dbg(sg_table->dev,
462 "%05d: ### %p\t:[L] 0x%llx ###\n",
466 dev_dbg(sg_table->dev,
467 "%05d: xxx %p\t:[INVALID] 0x%llx xxx\n",
473 dev_dbg(sg_table->dev, "******* End of Table *****\n");
476 static inline void tmc_etr_sg_table_dump(struct etr_sg_table *etr_table) {}
480 * Populate the SG Table page table entries from table/data
481 * pages allocated. Each Data page has ETR_SG_PAGES_PER_SYSPAGE SG pages.
482 * So does a Table page. So we keep track of indices of the tables
483 * in each system page and move the pointers accordingly.
485 #define INC_IDX_ROUND(idx, size) ((idx) = ((idx) + 1) % (size))
486 static void tmc_etr_sg_table_populate(struct etr_sg_table *etr_table)
489 int i, type, nr_entries;
490 int tpidx = 0; /* index to the current system table_page */
491 int sgtidx = 0; /* index to the sg_table within the current syspage */
492 int sgtentry = 0; /* the entry within the sg_table */
493 int dpidx = 0; /* index to the current system data_page */
494 int spidx = 0; /* index to the SG page within the current data page */
495 sgte_t *ptr; /* pointer to the table entry to fill */
496 struct tmc_sg_table *sg_table = etr_table->sg_table;
497 dma_addr_t *table_daddrs = sg_table->table_pages.daddrs;
498 dma_addr_t *data_daddrs = sg_table->data_pages.daddrs;
500 nr_entries = tmc_etr_sg_table_entries(sg_table->data_pages.nr_pages);
502 * Use the contiguous virtual address of the table to update entries.
504 ptr = sg_table->table_vaddr;
506 * Fill all the entries, except the last entry to avoid special
507 * checks within the loop.
509 for (i = 0; i < nr_entries - 1; i++) {
510 if (sgtentry == ETR_SG_PTRS_PER_PAGE - 1) {
512 * Last entry in a sg_table page is a link address to
513 * the next table page. If this sg_table is the last
514 * one in the system page, it links to the first
515 * sg_table in the next system page. Otherwise, it
516 * links to the next sg_table page within the system
519 if (sgtidx == ETR_SG_PAGES_PER_SYSPAGE - 1) {
520 paddr = table_daddrs[tpidx + 1];
522 paddr = table_daddrs[tpidx] +
523 (ETR_SG_PAGE_SIZE * (sgtidx + 1));
525 type = ETR_SG_ET_LINK;
528 * Update the indices to the data_pages to point to the
529 * next sg_page in the data buffer.
531 type = ETR_SG_ET_NORMAL;
532 paddr = data_daddrs[dpidx] + spidx * ETR_SG_PAGE_SIZE;
533 if (!INC_IDX_ROUND(spidx, ETR_SG_PAGES_PER_SYSPAGE))
536 *ptr++ = ETR_SG_ENTRY(paddr, type);
538 * Move to the next table pointer, moving the table page index
541 if (!INC_IDX_ROUND(sgtentry, ETR_SG_PTRS_PER_PAGE)) {
542 if (!INC_IDX_ROUND(sgtidx, ETR_SG_PAGES_PER_SYSPAGE))
547 /* Set up the last entry, which is always a data pointer */
548 paddr = data_daddrs[dpidx] + spidx * ETR_SG_PAGE_SIZE;
549 *ptr++ = ETR_SG_ENTRY(paddr, ETR_SG_ET_LAST);
553 * tmc_init_etr_sg_table: Allocate a TMC ETR SG table, data buffer of @size and
554 * populate the table.
556 * @dev - Device pointer for the TMC
557 * @node - NUMA node where the memory should be allocated
558 * @size - Total size of the data buffer
559 * @pages - Optional list of page virtual address
561 static struct etr_sg_table *
562 tmc_init_etr_sg_table(struct device *dev, int node,
563 unsigned long size, void **pages)
565 int nr_entries, nr_tpages;
566 int nr_dpages = size >> PAGE_SHIFT;
567 struct tmc_sg_table *sg_table;
568 struct etr_sg_table *etr_table;
570 etr_table = kzalloc(sizeof(*etr_table), GFP_KERNEL);
572 return ERR_PTR(-ENOMEM);
573 nr_entries = tmc_etr_sg_table_entries(nr_dpages);
574 nr_tpages = DIV_ROUND_UP(nr_entries, ETR_SG_PTRS_PER_SYSPAGE);
576 sg_table = tmc_alloc_sg_table(dev, node, nr_tpages, nr_dpages, pages);
577 if (IS_ERR(sg_table)) {
579 return ERR_CAST(sg_table);
582 etr_table->sg_table = sg_table;
583 /* TMC should use table base address for DBA */
584 etr_table->hwaddr = sg_table->table_daddr;
585 tmc_etr_sg_table_populate(etr_table);
586 /* Sync the table pages for the HW */
587 tmc_sg_table_sync_table(sg_table);
588 tmc_etr_sg_table_dump(etr_table);
594 * tmc_etr_alloc_flat_buf: Allocate a contiguous DMA buffer.
596 static int tmc_etr_alloc_flat_buf(struct tmc_drvdata *drvdata,
597 struct etr_buf *etr_buf, int node,
600 struct etr_flat_buf *flat_buf;
601 struct device *real_dev = drvdata->csdev->dev.parent;
603 /* We cannot reuse existing pages for flat buf */
607 flat_buf = kzalloc(sizeof(*flat_buf), GFP_KERNEL);
611 flat_buf->vaddr = dma_alloc_noncoherent(real_dev, etr_buf->size,
614 GFP_KERNEL | __GFP_NOWARN);
615 if (!flat_buf->vaddr) {
620 flat_buf->size = etr_buf->size;
621 flat_buf->dev = &drvdata->csdev->dev;
622 etr_buf->hwaddr = flat_buf->daddr;
623 etr_buf->mode = ETR_MODE_FLAT;
624 etr_buf->private = flat_buf;
628 static void tmc_etr_free_flat_buf(struct etr_buf *etr_buf)
630 struct etr_flat_buf *flat_buf = etr_buf->private;
632 if (flat_buf && flat_buf->daddr) {
633 struct device *real_dev = flat_buf->dev->parent;
635 dma_free_noncoherent(real_dev, etr_buf->size,
636 flat_buf->vaddr, flat_buf->daddr,
642 static void tmc_etr_sync_flat_buf(struct etr_buf *etr_buf, u64 rrp, u64 rwp)
644 struct etr_flat_buf *flat_buf = etr_buf->private;
645 struct device *real_dev = flat_buf->dev->parent;
648 * Adjust the buffer to point to the beginning of the trace data
649 * and update the available trace data.
651 etr_buf->offset = rrp - etr_buf->hwaddr;
653 etr_buf->len = etr_buf->size;
655 etr_buf->len = rwp - rrp;
658 * The driver always starts tracing at the beginning of the buffer,
659 * the only reason why we would get a wrap around is when the buffer
660 * is full. Sync the entire buffer in one go for this case.
662 if (etr_buf->offset + etr_buf->len > etr_buf->size)
663 dma_sync_single_for_cpu(real_dev, flat_buf->daddr,
664 etr_buf->size, DMA_FROM_DEVICE);
666 dma_sync_single_for_cpu(real_dev,
667 flat_buf->daddr + etr_buf->offset,
668 etr_buf->len, DMA_FROM_DEVICE);
671 static ssize_t tmc_etr_get_data_flat_buf(struct etr_buf *etr_buf,
672 u64 offset, size_t len, char **bufpp)
674 struct etr_flat_buf *flat_buf = etr_buf->private;
676 *bufpp = (char *)flat_buf->vaddr + offset;
678 * tmc_etr_buf_get_data already adjusts the length to handle
679 * buffer wrapping around.
684 static const struct etr_buf_operations etr_flat_buf_ops = {
685 .alloc = tmc_etr_alloc_flat_buf,
686 .free = tmc_etr_free_flat_buf,
687 .sync = tmc_etr_sync_flat_buf,
688 .get_data = tmc_etr_get_data_flat_buf,
692 * tmc_etr_alloc_sg_buf: Allocate an SG buf @etr_buf. Setup the parameters
695 static int tmc_etr_alloc_sg_buf(struct tmc_drvdata *drvdata,
696 struct etr_buf *etr_buf, int node,
699 struct etr_sg_table *etr_table;
700 struct device *dev = &drvdata->csdev->dev;
702 etr_table = tmc_init_etr_sg_table(dev, node,
703 etr_buf->size, pages);
704 if (IS_ERR(etr_table))
706 etr_buf->hwaddr = etr_table->hwaddr;
707 etr_buf->mode = ETR_MODE_ETR_SG;
708 etr_buf->private = etr_table;
712 static void tmc_etr_free_sg_buf(struct etr_buf *etr_buf)
714 struct etr_sg_table *etr_table = etr_buf->private;
717 tmc_free_sg_table(etr_table->sg_table);
722 static ssize_t tmc_etr_get_data_sg_buf(struct etr_buf *etr_buf, u64 offset,
723 size_t len, char **bufpp)
725 struct etr_sg_table *etr_table = etr_buf->private;
727 return tmc_sg_table_get_data(etr_table->sg_table, offset, len, bufpp);
730 static void tmc_etr_sync_sg_buf(struct etr_buf *etr_buf, u64 rrp, u64 rwp)
732 long r_offset, w_offset;
733 struct etr_sg_table *etr_table = etr_buf->private;
734 struct tmc_sg_table *table = etr_table->sg_table;
736 /* Convert hw address to offset in the buffer */
737 r_offset = tmc_sg_get_data_page_offset(table, rrp);
740 "Unable to map RRP %llx to offset\n", rrp);
745 w_offset = tmc_sg_get_data_page_offset(table, rwp);
748 "Unable to map RWP %llx to offset\n", rwp);
753 etr_buf->offset = r_offset;
755 etr_buf->len = etr_buf->size;
757 etr_buf->len = ((w_offset < r_offset) ? etr_buf->size : 0) +
759 tmc_sg_table_sync_data_range(table, r_offset, etr_buf->len);
762 static const struct etr_buf_operations etr_sg_buf_ops = {
763 .alloc = tmc_etr_alloc_sg_buf,
764 .free = tmc_etr_free_sg_buf,
765 .sync = tmc_etr_sync_sg_buf,
766 .get_data = tmc_etr_get_data_sg_buf,
770 * TMC ETR could be connected to a CATU device, which can provide address
771 * translation service. This is represented by the Output port of the TMC
772 * (ETR) connected to the input port of the CATU.
774 * Returns : coresight_device ptr for the CATU device if a CATU is found.
777 struct coresight_device *
778 tmc_etr_get_catu_device(struct tmc_drvdata *drvdata)
780 struct coresight_device *etr = drvdata->csdev;
781 union coresight_dev_subtype catu_subtype = {
782 .helper_subtype = CORESIGHT_DEV_SUBTYPE_HELPER_CATU
785 if (!IS_ENABLED(CONFIG_CORESIGHT_CATU))
788 return coresight_find_output_type(etr->pdata, CORESIGHT_DEV_TYPE_HELPER,
791 EXPORT_SYMBOL_GPL(tmc_etr_get_catu_device);
793 static const struct etr_buf_operations *etr_buf_ops[] = {
794 [ETR_MODE_FLAT] = &etr_flat_buf_ops,
795 [ETR_MODE_ETR_SG] = &etr_sg_buf_ops,
796 [ETR_MODE_CATU] = NULL,
799 void tmc_etr_set_catu_ops(const struct etr_buf_operations *catu)
801 etr_buf_ops[ETR_MODE_CATU] = catu;
803 EXPORT_SYMBOL_GPL(tmc_etr_set_catu_ops);
805 void tmc_etr_remove_catu_ops(void)
807 etr_buf_ops[ETR_MODE_CATU] = NULL;
809 EXPORT_SYMBOL_GPL(tmc_etr_remove_catu_ops);
811 static inline int tmc_etr_mode_alloc_buf(int mode,
812 struct tmc_drvdata *drvdata,
813 struct etr_buf *etr_buf, int node,
820 case ETR_MODE_ETR_SG:
822 if (etr_buf_ops[mode] && etr_buf_ops[mode]->alloc)
823 rc = etr_buf_ops[mode]->alloc(drvdata, etr_buf,
826 etr_buf->ops = etr_buf_ops[mode];
834 * tmc_alloc_etr_buf: Allocate a buffer use by ETR.
835 * @drvdata : ETR device details.
836 * @size : size of the requested buffer.
837 * @flags : Required properties for the buffer.
838 * @node : Node for memory allocations.
839 * @pages : An optional list of pages.
841 static struct etr_buf *tmc_alloc_etr_buf(struct tmc_drvdata *drvdata,
842 ssize_t size, int flags,
843 int node, void **pages)
846 bool has_etr_sg, has_iommu;
847 bool has_sg, has_catu;
848 struct etr_buf *etr_buf;
849 struct device *dev = &drvdata->csdev->dev;
851 has_etr_sg = tmc_etr_has_cap(drvdata, TMC_ETR_SG);
852 has_iommu = iommu_get_domain_for_dev(dev->parent);
853 has_catu = !!tmc_etr_get_catu_device(drvdata);
855 has_sg = has_catu || has_etr_sg;
857 etr_buf = kzalloc(sizeof(*etr_buf), GFP_KERNEL);
859 return ERR_PTR(-ENOMEM);
861 etr_buf->size = size;
864 * If we have to use an existing list of pages, we cannot reliably
865 * use a contiguous DMA memory (even if we have an IOMMU). Otherwise,
866 * we use the contiguous DMA memory if at least one of the following
867 * conditions is true:
868 * a) The ETR cannot use Scatter-Gather.
869 * b) we have a backing IOMMU
870 * c) The requested memory size is smaller (< 1M).
872 * Fallback to available mechanisms.
876 (!has_sg || has_iommu || size < SZ_1M))
877 rc = tmc_etr_mode_alloc_buf(ETR_MODE_FLAT, drvdata,
878 etr_buf, node, pages);
879 if (rc && has_etr_sg)
880 rc = tmc_etr_mode_alloc_buf(ETR_MODE_ETR_SG, drvdata,
881 etr_buf, node, pages);
883 rc = tmc_etr_mode_alloc_buf(ETR_MODE_CATU, drvdata,
884 etr_buf, node, pages);
890 refcount_set(&etr_buf->refcount, 1);
891 dev_dbg(dev, "allocated buffer of size %ldKB in mode %d\n",
892 (unsigned long)size >> 10, etr_buf->mode);
896 static void tmc_free_etr_buf(struct etr_buf *etr_buf)
898 WARN_ON(!etr_buf->ops || !etr_buf->ops->free);
899 etr_buf->ops->free(etr_buf);
904 * tmc_etr_buf_get_data: Get the pointer the trace data at @offset
905 * with a maximum of @len bytes.
906 * Returns: The size of the linear data available @pos, with *bufpp
907 * updated to point to the buffer.
909 static ssize_t tmc_etr_buf_get_data(struct etr_buf *etr_buf,
910 u64 offset, size_t len, char **bufpp)
912 /* Adjust the length to limit this transaction to end of buffer */
913 len = (len < (etr_buf->size - offset)) ? len : etr_buf->size - offset;
915 return etr_buf->ops->get_data(etr_buf, (u64)offset, len, bufpp);
919 tmc_etr_buf_insert_barrier_packet(struct etr_buf *etr_buf, u64 offset)
924 len = tmc_etr_buf_get_data(etr_buf, offset,
925 CORESIGHT_BARRIER_PKT_SIZE, &bufp);
926 if (WARN_ON(len < 0 || len < CORESIGHT_BARRIER_PKT_SIZE))
928 coresight_insert_barrier_packet(bufp);
929 return offset + CORESIGHT_BARRIER_PKT_SIZE;
933 * tmc_sync_etr_buf: Sync the trace buffer availability with drvdata.
934 * Makes sure the trace data is synced to the memory for consumption.
935 * @etr_buf->offset will hold the offset to the beginning of the trace data
936 * within the buffer, with @etr_buf->len bytes to consume.
938 static void tmc_sync_etr_buf(struct tmc_drvdata *drvdata)
940 struct etr_buf *etr_buf = drvdata->etr_buf;
944 rrp = tmc_read_rrp(drvdata);
945 rwp = tmc_read_rwp(drvdata);
946 status = readl_relaxed(drvdata->base + TMC_STS);
949 * If there were memory errors in the session, truncate the
952 if (WARN_ON_ONCE(status & TMC_STS_MEMERR)) {
953 dev_dbg(&drvdata->csdev->dev,
954 "tmc memory error detected, truncating buffer\n");
956 etr_buf->full = false;
960 etr_buf->full = !!(status & TMC_STS_FULL);
962 WARN_ON(!etr_buf->ops || !etr_buf->ops->sync);
964 etr_buf->ops->sync(etr_buf, rrp, rwp);
967 static int __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
970 struct etr_buf *etr_buf = drvdata->etr_buf;
973 CS_UNLOCK(drvdata->base);
975 /* Wait for TMCSReady bit to be set */
976 rc = tmc_wait_for_tmcready(drvdata);
978 dev_err(&drvdata->csdev->dev,
979 "Failed to enable : TMC not ready\n");
980 CS_LOCK(drvdata->base);
984 writel_relaxed(etr_buf->size / 4, drvdata->base + TMC_RSZ);
985 writel_relaxed(TMC_MODE_CIRCULAR_BUFFER, drvdata->base + TMC_MODE);
987 axictl = readl_relaxed(drvdata->base + TMC_AXICTL);
988 axictl &= ~TMC_AXICTL_CLEAR_MASK;
989 axictl |= TMC_AXICTL_PROT_CTL_B1;
990 axictl |= TMC_AXICTL_WR_BURST(drvdata->max_burst_size);
991 axictl |= TMC_AXICTL_AXCACHE_OS;
993 if (tmc_etr_has_cap(drvdata, TMC_ETR_AXI_ARCACHE)) {
994 axictl &= ~TMC_AXICTL_ARCACHE_MASK;
995 axictl |= TMC_AXICTL_ARCACHE_OS;
998 if (etr_buf->mode == ETR_MODE_ETR_SG)
999 axictl |= TMC_AXICTL_SCT_GAT_MODE;
1001 writel_relaxed(axictl, drvdata->base + TMC_AXICTL);
1002 tmc_write_dba(drvdata, etr_buf->hwaddr);
1004 * If the TMC pointers must be programmed before the session,
1005 * we have to set it properly (i.e, RRP/RWP to base address and
1006 * STS to "not full").
1008 if (tmc_etr_has_cap(drvdata, TMC_ETR_SAVE_RESTORE)) {
1009 tmc_write_rrp(drvdata, etr_buf->hwaddr);
1010 tmc_write_rwp(drvdata, etr_buf->hwaddr);
1011 sts = readl_relaxed(drvdata->base + TMC_STS) & ~TMC_STS_FULL;
1012 writel_relaxed(sts, drvdata->base + TMC_STS);
1015 writel_relaxed(TMC_FFCR_EN_FMT | TMC_FFCR_EN_TI |
1016 TMC_FFCR_FON_FLIN | TMC_FFCR_FON_TRIG_EVT |
1017 TMC_FFCR_TRIGON_TRIGIN,
1018 drvdata->base + TMC_FFCR);
1019 writel_relaxed(drvdata->trigger_cntr, drvdata->base + TMC_TRG);
1020 tmc_enable_hw(drvdata);
1022 CS_LOCK(drvdata->base);
1026 static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
1027 struct etr_buf *etr_buf)
1031 /* Callers should provide an appropriate buffer for use */
1032 if (WARN_ON(!etr_buf))
1035 if ((etr_buf->mode == ETR_MODE_ETR_SG) &&
1036 WARN_ON(!tmc_etr_has_cap(drvdata, TMC_ETR_SG)))
1039 if (WARN_ON(drvdata->etr_buf))
1042 rc = coresight_claim_device(drvdata->csdev);
1044 drvdata->etr_buf = etr_buf;
1045 rc = __tmc_etr_enable_hw(drvdata);
1047 drvdata->etr_buf = NULL;
1048 coresight_disclaim_device(drvdata->csdev);
1056 * Return the available trace data in the buffer (starts at etr_buf->offset,
1057 * limited by etr_buf->len) from @pos, with a maximum limit of @len,
1058 * also updating the @bufpp on where to find it. Since the trace data
1059 * starts at anywhere in the buffer, depending on the RRP, we adjust the
1060 * @len returned to handle buffer wrapping around.
1062 * We are protected here by drvdata->reading != 0, which ensures the
1063 * sysfs_buf stays alive.
1065 ssize_t tmc_etr_get_sysfs_trace(struct tmc_drvdata *drvdata,
1066 loff_t pos, size_t len, char **bufpp)
1069 ssize_t actual = len;
1070 struct etr_buf *etr_buf = drvdata->sysfs_buf;
1072 if (pos + actual > etr_buf->len)
1073 actual = etr_buf->len - pos;
1077 /* Compute the offset from which we read the data */
1078 offset = etr_buf->offset + pos;
1079 if (offset >= etr_buf->size)
1080 offset -= etr_buf->size;
1081 return tmc_etr_buf_get_data(etr_buf, offset, actual, bufpp);
1084 static struct etr_buf *
1085 tmc_etr_setup_sysfs_buf(struct tmc_drvdata *drvdata)
1087 return tmc_alloc_etr_buf(drvdata, drvdata->size,
1088 0, cpu_to_node(0), NULL);
1092 tmc_etr_free_sysfs_buf(struct etr_buf *buf)
1095 tmc_free_etr_buf(buf);
1098 static void tmc_etr_sync_sysfs_buf(struct tmc_drvdata *drvdata)
1100 struct etr_buf *etr_buf = drvdata->etr_buf;
1102 if (WARN_ON(drvdata->sysfs_buf != etr_buf)) {
1103 tmc_etr_free_sysfs_buf(drvdata->sysfs_buf);
1104 drvdata->sysfs_buf = NULL;
1106 tmc_sync_etr_buf(drvdata);
1108 * Insert barrier packets at the beginning, if there was
1112 tmc_etr_buf_insert_barrier_packet(etr_buf,
1117 static void __tmc_etr_disable_hw(struct tmc_drvdata *drvdata)
1119 CS_UNLOCK(drvdata->base);
1121 tmc_flush_and_stop(drvdata);
1123 * When operating in sysFS mode the content of the buffer needs to be
1124 * read before the TMC is disabled.
1126 if (drvdata->mode == CS_MODE_SYSFS)
1127 tmc_etr_sync_sysfs_buf(drvdata);
1129 tmc_disable_hw(drvdata);
1131 CS_LOCK(drvdata->base);
1135 void tmc_etr_disable_hw(struct tmc_drvdata *drvdata)
1137 __tmc_etr_disable_hw(drvdata);
1138 coresight_disclaim_device(drvdata->csdev);
1139 /* Reset the ETR buf used by hardware */
1140 drvdata->etr_buf = NULL;
1143 static struct etr_buf *tmc_etr_get_sysfs_buffer(struct coresight_device *csdev)
1146 unsigned long flags;
1147 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1148 struct etr_buf *sysfs_buf = NULL, *new_buf = NULL, *free_buf = NULL;
1151 * If we are enabling the ETR from disabled state, we need to make
1152 * sure we have a buffer with the right size. The etr_buf is not reset
1153 * immediately after we stop the tracing in SYSFS mode as we wait for
1154 * the user to collect the data. We may be able to reuse the existing
1155 * buffer, provided the size matches. Any allocation has to be done
1156 * with the lock released.
1158 spin_lock_irqsave(&drvdata->spinlock, flags);
1159 sysfs_buf = READ_ONCE(drvdata->sysfs_buf);
1160 if (!sysfs_buf || (sysfs_buf->size != drvdata->size)) {
1161 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1163 /* Allocate memory with the locks released */
1164 free_buf = new_buf = tmc_etr_setup_sysfs_buf(drvdata);
1165 if (IS_ERR(new_buf))
1168 /* Let's try again */
1169 spin_lock_irqsave(&drvdata->spinlock, flags);
1172 if (drvdata->reading || drvdata->mode == CS_MODE_PERF) {
1178 * If we don't have a buffer or it doesn't match the requested size,
1179 * use the buffer allocated above. Otherwise reuse the existing buffer.
1181 sysfs_buf = READ_ONCE(drvdata->sysfs_buf);
1182 if (!sysfs_buf || (new_buf && sysfs_buf->size != new_buf->size)) {
1183 free_buf = sysfs_buf;
1184 drvdata->sysfs_buf = new_buf;
1188 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1190 /* Free memory outside the spinlock if need be */
1192 tmc_etr_free_sysfs_buf(free_buf);
1193 return ret ? ERR_PTR(ret) : drvdata->sysfs_buf;
1196 static int tmc_enable_etr_sink_sysfs(struct coresight_device *csdev)
1199 unsigned long flags;
1200 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1201 struct etr_buf *sysfs_buf = tmc_etr_get_sysfs_buffer(csdev);
1203 if (IS_ERR(sysfs_buf))
1204 return PTR_ERR(sysfs_buf);
1206 spin_lock_irqsave(&drvdata->spinlock, flags);
1209 * In sysFS mode we can have multiple writers per sink. Since this
1210 * sink is already enabled no memory is needed and the HW need not be
1211 * touched, even if the buffer size has changed.
1213 if (drvdata->mode == CS_MODE_SYSFS) {
1214 atomic_inc(&csdev->refcnt);
1218 ret = tmc_etr_enable_hw(drvdata, sysfs_buf);
1220 drvdata->mode = CS_MODE_SYSFS;
1221 atomic_inc(&csdev->refcnt);
1225 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1228 dev_dbg(&csdev->dev, "TMC-ETR enabled\n");
1233 struct etr_buf *tmc_etr_get_buffer(struct coresight_device *csdev,
1234 enum cs_mode mode, void *data)
1236 struct perf_output_handle *handle = data;
1237 struct etr_perf_buffer *etr_perf;
1241 return tmc_etr_get_sysfs_buffer(csdev);
1243 etr_perf = etm_perf_sink_config(handle);
1244 if (WARN_ON(!etr_perf || !etr_perf->etr_buf))
1245 return ERR_PTR(-EINVAL);
1246 return etr_perf->etr_buf;
1248 return ERR_PTR(-EINVAL);
1251 EXPORT_SYMBOL_GPL(tmc_etr_get_buffer);
1254 * alloc_etr_buf: Allocate ETR buffer for use by perf.
1255 * The size of the hardware buffer is dependent on the size configured
1256 * via sysfs and the perf ring buffer size. We prefer to allocate the
1257 * largest possible size, scaling down the size by half until it
1258 * reaches a minimum limit (1M), beyond which we give up.
1260 static struct etr_buf *
1261 alloc_etr_buf(struct tmc_drvdata *drvdata, struct perf_event *event,
1262 int nr_pages, void **pages, bool snapshot)
1265 struct etr_buf *etr_buf;
1268 node = (event->cpu == -1) ? NUMA_NO_NODE : cpu_to_node(event->cpu);
1270 * Try to match the perf ring buffer size if it is larger
1271 * than the size requested via sysfs.
1273 if ((nr_pages << PAGE_SHIFT) > drvdata->size) {
1274 etr_buf = tmc_alloc_etr_buf(drvdata, ((ssize_t)nr_pages << PAGE_SHIFT),
1276 if (!IS_ERR(etr_buf))
1281 * Else switch to configured size for this ETR
1282 * and scale down until we hit the minimum limit.
1284 size = drvdata->size;
1286 etr_buf = tmc_alloc_etr_buf(drvdata, size, 0, node, NULL);
1287 if (!IS_ERR(etr_buf))
1290 } while (size >= TMC_ETR_PERF_MIN_BUF_SIZE);
1292 return ERR_PTR(-ENOMEM);
1298 static struct etr_buf *
1299 get_perf_etr_buf_cpu_wide(struct tmc_drvdata *drvdata,
1300 struct perf_event *event, int nr_pages,
1301 void **pages, bool snapshot)
1304 pid_t pid = task_pid_nr(event->owner);
1305 struct etr_buf *etr_buf;
1309 * An etr_perf_buffer is associated with an event and holds a reference
1310 * to the AUX ring buffer that was created for that event. In CPU-wide
1311 * N:1 mode multiple events (one per CPU), each with its own AUX ring
1312 * buffer, share a sink. As such an etr_perf_buffer is created for each
1313 * event but a single etr_buf associated with the ETR is shared between
1314 * them. The last event in a trace session will copy the content of the
1315 * etr_buf to its AUX ring buffer. Ring buffer associated to other
1316 * events are simply not used an freed as events are destoyed. We still
1317 * need to allocate a ring buffer for each event since we don't know
1318 * which event will be last.
1322 * The first thing to do here is check if an etr_buf has already been
1323 * allocated for this session. If so it is shared with this event,
1324 * otherwise it is created.
1326 mutex_lock(&drvdata->idr_mutex);
1327 etr_buf = idr_find(&drvdata->idr, pid);
1329 refcount_inc(&etr_buf->refcount);
1330 mutex_unlock(&drvdata->idr_mutex);
1334 /* If we made it here no buffer has been allocated, do so now. */
1335 mutex_unlock(&drvdata->idr_mutex);
1337 etr_buf = alloc_etr_buf(drvdata, event, nr_pages, pages, snapshot);
1338 if (IS_ERR(etr_buf))
1341 /* Now that we have a buffer, add it to the IDR. */
1342 mutex_lock(&drvdata->idr_mutex);
1343 ret = idr_alloc(&drvdata->idr, etr_buf, pid, pid + 1, GFP_KERNEL);
1344 mutex_unlock(&drvdata->idr_mutex);
1346 /* Another event with this session ID has allocated this buffer. */
1347 if (ret == -ENOSPC) {
1348 tmc_free_etr_buf(etr_buf);
1352 /* The IDR can't allocate room for a new session, abandon ship. */
1353 if (ret == -ENOMEM) {
1354 tmc_free_etr_buf(etr_buf);
1355 return ERR_PTR(ret);
1362 static struct etr_buf *
1363 get_perf_etr_buf_per_thread(struct tmc_drvdata *drvdata,
1364 struct perf_event *event, int nr_pages,
1365 void **pages, bool snapshot)
1368 * In per-thread mode the etr_buf isn't shared, so just go ahead
1369 * with memory allocation.
1371 return alloc_etr_buf(drvdata, event, nr_pages, pages, snapshot);
1374 static struct etr_buf *
1375 get_perf_etr_buf(struct tmc_drvdata *drvdata, struct perf_event *event,
1376 int nr_pages, void **pages, bool snapshot)
1378 if (event->cpu == -1)
1379 return get_perf_etr_buf_per_thread(drvdata, event, nr_pages,
1382 return get_perf_etr_buf_cpu_wide(drvdata, event, nr_pages,
1386 static struct etr_perf_buffer *
1387 tmc_etr_setup_perf_buf(struct tmc_drvdata *drvdata, struct perf_event *event,
1388 int nr_pages, void **pages, bool snapshot)
1391 struct etr_buf *etr_buf;
1392 struct etr_perf_buffer *etr_perf;
1394 node = (event->cpu == -1) ? NUMA_NO_NODE : cpu_to_node(event->cpu);
1396 etr_perf = kzalloc_node(sizeof(*etr_perf), GFP_KERNEL, node);
1398 return ERR_PTR(-ENOMEM);
1400 etr_buf = get_perf_etr_buf(drvdata, event, nr_pages, pages, snapshot);
1401 if (!IS_ERR(etr_buf))
1405 return ERR_PTR(-ENOMEM);
1409 * Keep a reference to the ETR this buffer has been allocated for
1410 * in order to have access to the IDR in tmc_free_etr_buffer().
1412 etr_perf->drvdata = drvdata;
1413 etr_perf->etr_buf = etr_buf;
1419 static void *tmc_alloc_etr_buffer(struct coresight_device *csdev,
1420 struct perf_event *event, void **pages,
1421 int nr_pages, bool snapshot)
1423 struct etr_perf_buffer *etr_perf;
1424 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1426 etr_perf = tmc_etr_setup_perf_buf(drvdata, event,
1427 nr_pages, pages, snapshot);
1428 if (IS_ERR(etr_perf)) {
1429 dev_dbg(&csdev->dev, "Unable to allocate ETR buffer\n");
1433 etr_perf->pid = task_pid_nr(event->owner);
1434 etr_perf->snapshot = snapshot;
1435 etr_perf->nr_pages = nr_pages;
1436 etr_perf->pages = pages;
1441 static void tmc_free_etr_buffer(void *config)
1443 struct etr_perf_buffer *etr_perf = config;
1444 struct tmc_drvdata *drvdata = etr_perf->drvdata;
1445 struct etr_buf *buf, *etr_buf = etr_perf->etr_buf;
1448 goto free_etr_perf_buffer;
1450 mutex_lock(&drvdata->idr_mutex);
1451 /* If we are not the last one to use the buffer, don't touch it. */
1452 if (!refcount_dec_and_test(&etr_buf->refcount)) {
1453 mutex_unlock(&drvdata->idr_mutex);
1454 goto free_etr_perf_buffer;
1457 /* We are the last one, remove from the IDR and free the buffer. */
1458 buf = idr_remove(&drvdata->idr, etr_perf->pid);
1459 mutex_unlock(&drvdata->idr_mutex);
1462 * Something went very wrong if the buffer associated with this ID
1463 * is not the same in the IDR. Leak to avoid use after free.
1465 if (buf && WARN_ON(buf != etr_buf))
1466 goto free_etr_perf_buffer;
1468 tmc_free_etr_buf(etr_perf->etr_buf);
1470 free_etr_perf_buffer:
1475 * tmc_etr_sync_perf_buffer: Copy the actual trace data from the hardware
1476 * buffer to the perf ring buffer.
1478 static void tmc_etr_sync_perf_buffer(struct etr_perf_buffer *etr_perf,
1480 unsigned long src_offset,
1481 unsigned long to_copy)
1484 long pg_idx, pg_offset;
1485 char **dst_pages, *src_buf;
1486 struct etr_buf *etr_buf = etr_perf->etr_buf;
1488 head = PERF_IDX2OFF(head, etr_perf);
1489 pg_idx = head >> PAGE_SHIFT;
1490 pg_offset = head & (PAGE_SIZE - 1);
1491 dst_pages = (char **)etr_perf->pages;
1493 while (to_copy > 0) {
1495 * In one iteration, we can copy minimum of :
1496 * 1) what is available in the source buffer,
1497 * 2) what is available in the source buffer, before it
1499 * 3) what is available in the destination page.
1502 if (src_offset >= etr_buf->size)
1503 src_offset -= etr_buf->size;
1504 bytes = tmc_etr_buf_get_data(etr_buf, src_offset, to_copy,
1506 if (WARN_ON_ONCE(bytes <= 0))
1508 bytes = min(bytes, (long)(PAGE_SIZE - pg_offset));
1510 memcpy(dst_pages[pg_idx] + pg_offset, src_buf, bytes);
1514 /* Move destination pointers */
1516 if (pg_offset == PAGE_SIZE) {
1518 if (++pg_idx == etr_perf->nr_pages)
1522 /* Move source pointers */
1523 src_offset += bytes;
1528 * tmc_update_etr_buffer : Update the perf ring buffer with the
1529 * available trace data. We use software double buffering at the moment.
1531 * TODO: Add support for reusing the perf ring buffer.
1533 static unsigned long
1534 tmc_update_etr_buffer(struct coresight_device *csdev,
1535 struct perf_output_handle *handle,
1539 unsigned long flags, offset, size = 0;
1540 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1541 struct etr_perf_buffer *etr_perf = config;
1542 struct etr_buf *etr_buf = etr_perf->etr_buf;
1544 spin_lock_irqsave(&drvdata->spinlock, flags);
1546 /* Don't do anything if another tracer is using this sink */
1547 if (atomic_read(&csdev->refcnt) != 1) {
1548 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1552 if (WARN_ON(drvdata->perf_buf != etr_buf)) {
1554 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1558 CS_UNLOCK(drvdata->base);
1560 tmc_flush_and_stop(drvdata);
1561 tmc_sync_etr_buf(drvdata);
1563 CS_LOCK(drvdata->base);
1564 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1566 lost = etr_buf->full;
1567 offset = etr_buf->offset;
1568 size = etr_buf->len;
1571 * The ETR buffer may be bigger than the space available in the
1572 * perf ring buffer (handle->size). If so advance the offset so that we
1573 * get the latest trace data. In snapshot mode none of that matters
1574 * since we are expected to clobber stale data in favour of the latest
1577 if (!etr_perf->snapshot && size > handle->size) {
1578 u32 mask = tmc_get_memwidth_mask(drvdata);
1581 * Make sure the new size is aligned in accordance with the
1582 * requirement explained in function tmc_get_memwidth_mask().
1584 size = handle->size & mask;
1585 offset = etr_buf->offset + etr_buf->len - size;
1587 if (offset >= etr_buf->size)
1588 offset -= etr_buf->size;
1592 /* Insert barrier packets at the beginning, if there was an overflow */
1594 tmc_etr_buf_insert_barrier_packet(etr_buf, offset);
1595 tmc_etr_sync_perf_buffer(etr_perf, handle->head, offset, size);
1598 * In snapshot mode we simply increment the head by the number of byte
1599 * that were written. User space will figure out how many bytes to get
1600 * from the AUX buffer based on the position of the head.
1602 if (etr_perf->snapshot)
1603 handle->head += size;
1606 * Ensure that the AUX trace data is visible before the aux_head
1607 * is updated via perf_aux_output_end(), as expected by the
1614 * Don't set the TRUNCATED flag in snapshot mode because 1) the
1615 * captured buffer is expected to be truncated and 2) a full buffer
1616 * prevents the event from being re-enabled by the perf core,
1617 * resulting in stale data being send to user space.
1619 if (!etr_perf->snapshot && lost)
1620 perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED);
1624 static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data)
1628 unsigned long flags;
1629 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1630 struct perf_output_handle *handle = data;
1631 struct etr_perf_buffer *etr_perf = etm_perf_sink_config(handle);
1633 spin_lock_irqsave(&drvdata->spinlock, flags);
1634 /* Don't use this sink if it is already claimed by sysFS */
1635 if (drvdata->mode == CS_MODE_SYSFS) {
1640 if (WARN_ON(!etr_perf || !etr_perf->etr_buf)) {
1645 /* Get a handle on the pid of the process to monitor */
1646 pid = etr_perf->pid;
1648 /* Do not proceed if this device is associated with another session */
1649 if (drvdata->pid != -1 && drvdata->pid != pid) {
1655 * No HW configuration is needed if the sink is already in
1656 * use for this session.
1658 if (drvdata->pid == pid) {
1659 atomic_inc(&csdev->refcnt);
1663 rc = tmc_etr_enable_hw(drvdata, etr_perf->etr_buf);
1665 /* Associate with monitored process. */
1667 drvdata->mode = CS_MODE_PERF;
1668 drvdata->perf_buf = etr_perf->etr_buf;
1669 atomic_inc(&csdev->refcnt);
1673 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1677 static int tmc_enable_etr_sink(struct coresight_device *csdev,
1678 enum cs_mode mode, void *data)
1682 return tmc_enable_etr_sink_sysfs(csdev);
1684 return tmc_enable_etr_sink_perf(csdev, data);
1690 static int tmc_disable_etr_sink(struct coresight_device *csdev)
1692 unsigned long flags;
1693 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1695 spin_lock_irqsave(&drvdata->spinlock, flags);
1697 if (drvdata->reading) {
1698 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1702 if (atomic_dec_return(&csdev->refcnt)) {
1703 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1707 /* Complain if we (somehow) got out of sync */
1708 WARN_ON_ONCE(drvdata->mode == CS_MODE_DISABLED);
1709 tmc_etr_disable_hw(drvdata);
1710 /* Dissociate from monitored process. */
1712 drvdata->mode = CS_MODE_DISABLED;
1713 /* Reset perf specific data */
1714 drvdata->perf_buf = NULL;
1716 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1718 dev_dbg(&csdev->dev, "TMC-ETR disabled\n");
1722 static const struct coresight_ops_sink tmc_etr_sink_ops = {
1723 .enable = tmc_enable_etr_sink,
1724 .disable = tmc_disable_etr_sink,
1725 .alloc_buffer = tmc_alloc_etr_buffer,
1726 .update_buffer = tmc_update_etr_buffer,
1727 .free_buffer = tmc_free_etr_buffer,
1730 const struct coresight_ops tmc_etr_cs_ops = {
1731 .sink_ops = &tmc_etr_sink_ops,
1734 int tmc_read_prepare_etr(struct tmc_drvdata *drvdata)
1737 unsigned long flags;
1739 /* config types are set a boot time and never change */
1740 if (WARN_ON_ONCE(drvdata->config_type != TMC_CONFIG_TYPE_ETR))
1743 spin_lock_irqsave(&drvdata->spinlock, flags);
1744 if (drvdata->reading) {
1750 * We can safely allow reads even if the ETR is operating in PERF mode,
1751 * since the sysfs session is captured in mode specific data.
1752 * If drvdata::sysfs_data is NULL the trace data has been read already.
1754 if (!drvdata->sysfs_buf) {
1759 /* Disable the TMC if we are trying to read from a running session. */
1760 if (drvdata->mode == CS_MODE_SYSFS)
1761 __tmc_etr_disable_hw(drvdata);
1763 drvdata->reading = true;
1765 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1770 int tmc_read_unprepare_etr(struct tmc_drvdata *drvdata)
1772 unsigned long flags;
1773 struct etr_buf *sysfs_buf = NULL;
1775 /* config types are set a boot time and never change */
1776 if (WARN_ON_ONCE(drvdata->config_type != TMC_CONFIG_TYPE_ETR))
1779 spin_lock_irqsave(&drvdata->spinlock, flags);
1781 /* RE-enable the TMC if need be */
1782 if (drvdata->mode == CS_MODE_SYSFS) {
1784 * The trace run will continue with the same allocated trace
1785 * buffer. Since the tracer is still enabled drvdata::buf can't
1788 __tmc_etr_enable_hw(drvdata);
1791 * The ETR is not tracing and the buffer was just read.
1792 * As such prepare to free the trace buffer.
1794 sysfs_buf = drvdata->sysfs_buf;
1795 drvdata->sysfs_buf = NULL;
1798 drvdata->reading = false;
1799 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1801 /* Free allocated memory out side of the spinlock */
1803 tmc_etr_free_sysfs_buf(sysfs_buf);