1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright(C) 2016 Linaro Limited. All rights reserved.
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,
613 DMA_FROM_DEVICE, GFP_KERNEL);
614 if (!flat_buf->vaddr) {
619 flat_buf->size = etr_buf->size;
620 flat_buf->dev = &drvdata->csdev->dev;
621 etr_buf->hwaddr = flat_buf->daddr;
622 etr_buf->mode = ETR_MODE_FLAT;
623 etr_buf->private = flat_buf;
627 static void tmc_etr_free_flat_buf(struct etr_buf *etr_buf)
629 struct etr_flat_buf *flat_buf = etr_buf->private;
631 if (flat_buf && flat_buf->daddr) {
632 struct device *real_dev = flat_buf->dev->parent;
634 dma_free_noncoherent(real_dev, etr_buf->size,
635 flat_buf->vaddr, flat_buf->daddr,
641 static void tmc_etr_sync_flat_buf(struct etr_buf *etr_buf, u64 rrp, u64 rwp)
643 struct etr_flat_buf *flat_buf = etr_buf->private;
644 struct device *real_dev = flat_buf->dev->parent;
647 * Adjust the buffer to point to the beginning of the trace data
648 * and update the available trace data.
650 etr_buf->offset = rrp - etr_buf->hwaddr;
652 etr_buf->len = etr_buf->size;
654 etr_buf->len = rwp - rrp;
657 * The driver always starts tracing at the beginning of the buffer,
658 * the only reason why we would get a wrap around is when the buffer
659 * is full. Sync the entire buffer in one go for this case.
661 if (etr_buf->offset + etr_buf->len > etr_buf->size)
662 dma_sync_single_for_cpu(real_dev, flat_buf->daddr,
663 etr_buf->size, DMA_FROM_DEVICE);
665 dma_sync_single_for_cpu(real_dev,
666 flat_buf->daddr + etr_buf->offset,
667 etr_buf->len, DMA_FROM_DEVICE);
670 static ssize_t tmc_etr_get_data_flat_buf(struct etr_buf *etr_buf,
671 u64 offset, size_t len, char **bufpp)
673 struct etr_flat_buf *flat_buf = etr_buf->private;
675 *bufpp = (char *)flat_buf->vaddr + offset;
677 * tmc_etr_buf_get_data already adjusts the length to handle
678 * buffer wrapping around.
683 static const struct etr_buf_operations etr_flat_buf_ops = {
684 .alloc = tmc_etr_alloc_flat_buf,
685 .free = tmc_etr_free_flat_buf,
686 .sync = tmc_etr_sync_flat_buf,
687 .get_data = tmc_etr_get_data_flat_buf,
691 * tmc_etr_alloc_sg_buf: Allocate an SG buf @etr_buf. Setup the parameters
694 static int tmc_etr_alloc_sg_buf(struct tmc_drvdata *drvdata,
695 struct etr_buf *etr_buf, int node,
698 struct etr_sg_table *etr_table;
699 struct device *dev = &drvdata->csdev->dev;
701 etr_table = tmc_init_etr_sg_table(dev, node,
702 etr_buf->size, pages);
703 if (IS_ERR(etr_table))
705 etr_buf->hwaddr = etr_table->hwaddr;
706 etr_buf->mode = ETR_MODE_ETR_SG;
707 etr_buf->private = etr_table;
711 static void tmc_etr_free_sg_buf(struct etr_buf *etr_buf)
713 struct etr_sg_table *etr_table = etr_buf->private;
716 tmc_free_sg_table(etr_table->sg_table);
721 static ssize_t tmc_etr_get_data_sg_buf(struct etr_buf *etr_buf, u64 offset,
722 size_t len, char **bufpp)
724 struct etr_sg_table *etr_table = etr_buf->private;
726 return tmc_sg_table_get_data(etr_table->sg_table, offset, len, bufpp);
729 static void tmc_etr_sync_sg_buf(struct etr_buf *etr_buf, u64 rrp, u64 rwp)
731 long r_offset, w_offset;
732 struct etr_sg_table *etr_table = etr_buf->private;
733 struct tmc_sg_table *table = etr_table->sg_table;
735 /* Convert hw address to offset in the buffer */
736 r_offset = tmc_sg_get_data_page_offset(table, rrp);
739 "Unable to map RRP %llx to offset\n", rrp);
744 w_offset = tmc_sg_get_data_page_offset(table, rwp);
747 "Unable to map RWP %llx to offset\n", rwp);
752 etr_buf->offset = r_offset;
754 etr_buf->len = etr_buf->size;
756 etr_buf->len = ((w_offset < r_offset) ? etr_buf->size : 0) +
758 tmc_sg_table_sync_data_range(table, r_offset, etr_buf->len);
761 static const struct etr_buf_operations etr_sg_buf_ops = {
762 .alloc = tmc_etr_alloc_sg_buf,
763 .free = tmc_etr_free_sg_buf,
764 .sync = tmc_etr_sync_sg_buf,
765 .get_data = tmc_etr_get_data_sg_buf,
769 * TMC ETR could be connected to a CATU device, which can provide address
770 * translation service. This is represented by the Output port of the TMC
771 * (ETR) connected to the input port of the CATU.
773 * Returns : coresight_device ptr for the CATU device if a CATU is found.
776 struct coresight_device *
777 tmc_etr_get_catu_device(struct tmc_drvdata *drvdata)
779 struct coresight_device *etr = drvdata->csdev;
780 union coresight_dev_subtype catu_subtype = {
781 .helper_subtype = CORESIGHT_DEV_SUBTYPE_HELPER_CATU
784 if (!IS_ENABLED(CONFIG_CORESIGHT_CATU))
787 return coresight_find_output_type(etr->pdata, CORESIGHT_DEV_TYPE_HELPER,
790 EXPORT_SYMBOL_GPL(tmc_etr_get_catu_device);
792 static const struct etr_buf_operations *etr_buf_ops[] = {
793 [ETR_MODE_FLAT] = &etr_flat_buf_ops,
794 [ETR_MODE_ETR_SG] = &etr_sg_buf_ops,
795 [ETR_MODE_CATU] = NULL,
798 void tmc_etr_set_catu_ops(const struct etr_buf_operations *catu)
800 etr_buf_ops[ETR_MODE_CATU] = catu;
802 EXPORT_SYMBOL_GPL(tmc_etr_set_catu_ops);
804 void tmc_etr_remove_catu_ops(void)
806 etr_buf_ops[ETR_MODE_CATU] = NULL;
808 EXPORT_SYMBOL_GPL(tmc_etr_remove_catu_ops);
810 static inline int tmc_etr_mode_alloc_buf(int mode,
811 struct tmc_drvdata *drvdata,
812 struct etr_buf *etr_buf, int node,
819 case ETR_MODE_ETR_SG:
821 if (etr_buf_ops[mode] && etr_buf_ops[mode]->alloc)
822 rc = etr_buf_ops[mode]->alloc(drvdata, etr_buf,
825 etr_buf->ops = etr_buf_ops[mode];
833 * tmc_alloc_etr_buf: Allocate a buffer use by ETR.
834 * @drvdata : ETR device details.
835 * @size : size of the requested buffer.
836 * @flags : Required properties for the buffer.
837 * @node : Node for memory allocations.
838 * @pages : An optional list of pages.
840 static struct etr_buf *tmc_alloc_etr_buf(struct tmc_drvdata *drvdata,
841 ssize_t size, int flags,
842 int node, void **pages)
845 bool has_etr_sg, has_iommu;
846 bool has_sg, has_catu;
847 struct etr_buf *etr_buf;
848 struct device *dev = &drvdata->csdev->dev;
850 has_etr_sg = tmc_etr_has_cap(drvdata, TMC_ETR_SG);
851 has_iommu = iommu_get_domain_for_dev(dev->parent);
852 has_catu = !!tmc_etr_get_catu_device(drvdata);
854 has_sg = has_catu || has_etr_sg;
856 etr_buf = kzalloc(sizeof(*etr_buf), GFP_KERNEL);
858 return ERR_PTR(-ENOMEM);
860 etr_buf->size = size;
863 * If we have to use an existing list of pages, we cannot reliably
864 * use a contiguous DMA memory (even if we have an IOMMU). Otherwise,
865 * we use the contiguous DMA memory if at least one of the following
866 * conditions is true:
867 * a) The ETR cannot use Scatter-Gather.
868 * b) we have a backing IOMMU
869 * c) The requested memory size is smaller (< 1M).
871 * Fallback to available mechanisms.
875 (!has_sg || has_iommu || size < SZ_1M))
876 rc = tmc_etr_mode_alloc_buf(ETR_MODE_FLAT, drvdata,
877 etr_buf, node, pages);
878 if (rc && has_etr_sg)
879 rc = tmc_etr_mode_alloc_buf(ETR_MODE_ETR_SG, drvdata,
880 etr_buf, node, pages);
882 rc = tmc_etr_mode_alloc_buf(ETR_MODE_CATU, drvdata,
883 etr_buf, node, pages);
889 refcount_set(&etr_buf->refcount, 1);
890 dev_dbg(dev, "allocated buffer of size %ldKB in mode %d\n",
891 (unsigned long)size >> 10, etr_buf->mode);
895 static void tmc_free_etr_buf(struct etr_buf *etr_buf)
897 WARN_ON(!etr_buf->ops || !etr_buf->ops->free);
898 etr_buf->ops->free(etr_buf);
903 * tmc_etr_buf_get_data: Get the pointer the trace data at @offset
904 * with a maximum of @len bytes.
905 * Returns: The size of the linear data available @pos, with *bufpp
906 * updated to point to the buffer.
908 static ssize_t tmc_etr_buf_get_data(struct etr_buf *etr_buf,
909 u64 offset, size_t len, char **bufpp)
911 /* Adjust the length to limit this transaction to end of buffer */
912 len = (len < (etr_buf->size - offset)) ? len : etr_buf->size - offset;
914 return etr_buf->ops->get_data(etr_buf, (u64)offset, len, bufpp);
918 tmc_etr_buf_insert_barrier_packet(struct etr_buf *etr_buf, u64 offset)
923 len = tmc_etr_buf_get_data(etr_buf, offset,
924 CORESIGHT_BARRIER_PKT_SIZE, &bufp);
925 if (WARN_ON(len < 0 || len < CORESIGHT_BARRIER_PKT_SIZE))
927 coresight_insert_barrier_packet(bufp);
928 return offset + CORESIGHT_BARRIER_PKT_SIZE;
932 * tmc_sync_etr_buf: Sync the trace buffer availability with drvdata.
933 * Makes sure the trace data is synced to the memory for consumption.
934 * @etr_buf->offset will hold the offset to the beginning of the trace data
935 * within the buffer, with @etr_buf->len bytes to consume.
937 static void tmc_sync_etr_buf(struct tmc_drvdata *drvdata)
939 struct etr_buf *etr_buf = drvdata->etr_buf;
943 rrp = tmc_read_rrp(drvdata);
944 rwp = tmc_read_rwp(drvdata);
945 status = readl_relaxed(drvdata->base + TMC_STS);
948 * If there were memory errors in the session, truncate the
951 if (WARN_ON_ONCE(status & TMC_STS_MEMERR)) {
952 dev_dbg(&drvdata->csdev->dev,
953 "tmc memory error detected, truncating buffer\n");
955 etr_buf->full = false;
959 etr_buf->full = !!(status & TMC_STS_FULL);
961 WARN_ON(!etr_buf->ops || !etr_buf->ops->sync);
963 etr_buf->ops->sync(etr_buf, rrp, rwp);
966 static int __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
969 struct etr_buf *etr_buf = drvdata->etr_buf;
972 CS_UNLOCK(drvdata->base);
974 /* Wait for TMCSReady bit to be set */
975 rc = tmc_wait_for_tmcready(drvdata);
977 dev_err(&drvdata->csdev->dev,
978 "Failed to enable : TMC not ready\n");
979 CS_LOCK(drvdata->base);
983 writel_relaxed(etr_buf->size / 4, drvdata->base + TMC_RSZ);
984 writel_relaxed(TMC_MODE_CIRCULAR_BUFFER, drvdata->base + TMC_MODE);
986 axictl = readl_relaxed(drvdata->base + TMC_AXICTL);
987 axictl &= ~TMC_AXICTL_CLEAR_MASK;
988 axictl |= TMC_AXICTL_PROT_CTL_B1;
989 axictl |= TMC_AXICTL_WR_BURST(drvdata->max_burst_size);
990 axictl |= TMC_AXICTL_AXCACHE_OS;
992 if (tmc_etr_has_cap(drvdata, TMC_ETR_AXI_ARCACHE)) {
993 axictl &= ~TMC_AXICTL_ARCACHE_MASK;
994 axictl |= TMC_AXICTL_ARCACHE_OS;
997 if (etr_buf->mode == ETR_MODE_ETR_SG)
998 axictl |= TMC_AXICTL_SCT_GAT_MODE;
1000 writel_relaxed(axictl, drvdata->base + TMC_AXICTL);
1001 tmc_write_dba(drvdata, etr_buf->hwaddr);
1003 * If the TMC pointers must be programmed before the session,
1004 * we have to set it properly (i.e, RRP/RWP to base address and
1005 * STS to "not full").
1007 if (tmc_etr_has_cap(drvdata, TMC_ETR_SAVE_RESTORE)) {
1008 tmc_write_rrp(drvdata, etr_buf->hwaddr);
1009 tmc_write_rwp(drvdata, etr_buf->hwaddr);
1010 sts = readl_relaxed(drvdata->base + TMC_STS) & ~TMC_STS_FULL;
1011 writel_relaxed(sts, drvdata->base + TMC_STS);
1014 writel_relaxed(TMC_FFCR_EN_FMT | TMC_FFCR_EN_TI |
1015 TMC_FFCR_FON_FLIN | TMC_FFCR_FON_TRIG_EVT |
1016 TMC_FFCR_TRIGON_TRIGIN,
1017 drvdata->base + TMC_FFCR);
1018 writel_relaxed(drvdata->trigger_cntr, drvdata->base + TMC_TRG);
1019 tmc_enable_hw(drvdata);
1021 CS_LOCK(drvdata->base);
1025 static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
1026 struct etr_buf *etr_buf)
1030 /* Callers should provide an appropriate buffer for use */
1031 if (WARN_ON(!etr_buf))
1034 if ((etr_buf->mode == ETR_MODE_ETR_SG) &&
1035 WARN_ON(!tmc_etr_has_cap(drvdata, TMC_ETR_SG)))
1038 if (WARN_ON(drvdata->etr_buf))
1041 rc = coresight_claim_device(drvdata->csdev);
1043 drvdata->etr_buf = etr_buf;
1044 rc = __tmc_etr_enable_hw(drvdata);
1046 drvdata->etr_buf = NULL;
1047 coresight_disclaim_device(drvdata->csdev);
1055 * Return the available trace data in the buffer (starts at etr_buf->offset,
1056 * limited by etr_buf->len) from @pos, with a maximum limit of @len,
1057 * also updating the @bufpp on where to find it. Since the trace data
1058 * starts at anywhere in the buffer, depending on the RRP, we adjust the
1059 * @len returned to handle buffer wrapping around.
1061 * We are protected here by drvdata->reading != 0, which ensures the
1062 * sysfs_buf stays alive.
1064 ssize_t tmc_etr_get_sysfs_trace(struct tmc_drvdata *drvdata,
1065 loff_t pos, size_t len, char **bufpp)
1068 ssize_t actual = len;
1069 struct etr_buf *etr_buf = drvdata->sysfs_buf;
1071 if (pos + actual > etr_buf->len)
1072 actual = etr_buf->len - pos;
1076 /* Compute the offset from which we read the data */
1077 offset = etr_buf->offset + pos;
1078 if (offset >= etr_buf->size)
1079 offset -= etr_buf->size;
1080 return tmc_etr_buf_get_data(etr_buf, offset, actual, bufpp);
1083 static struct etr_buf *
1084 tmc_etr_setup_sysfs_buf(struct tmc_drvdata *drvdata)
1086 return tmc_alloc_etr_buf(drvdata, drvdata->size,
1087 0, cpu_to_node(0), NULL);
1091 tmc_etr_free_sysfs_buf(struct etr_buf *buf)
1094 tmc_free_etr_buf(buf);
1097 static void tmc_etr_sync_sysfs_buf(struct tmc_drvdata *drvdata)
1099 struct etr_buf *etr_buf = drvdata->etr_buf;
1101 if (WARN_ON(drvdata->sysfs_buf != etr_buf)) {
1102 tmc_etr_free_sysfs_buf(drvdata->sysfs_buf);
1103 drvdata->sysfs_buf = NULL;
1105 tmc_sync_etr_buf(drvdata);
1107 * Insert barrier packets at the beginning, if there was
1111 tmc_etr_buf_insert_barrier_packet(etr_buf,
1116 static void __tmc_etr_disable_hw(struct tmc_drvdata *drvdata)
1118 CS_UNLOCK(drvdata->base);
1120 tmc_flush_and_stop(drvdata);
1122 * When operating in sysFS mode the content of the buffer needs to be
1123 * read before the TMC is disabled.
1125 if (drvdata->mode == CS_MODE_SYSFS)
1126 tmc_etr_sync_sysfs_buf(drvdata);
1128 tmc_disable_hw(drvdata);
1130 CS_LOCK(drvdata->base);
1134 void tmc_etr_disable_hw(struct tmc_drvdata *drvdata)
1136 __tmc_etr_disable_hw(drvdata);
1137 coresight_disclaim_device(drvdata->csdev);
1138 /* Reset the ETR buf used by hardware */
1139 drvdata->etr_buf = NULL;
1142 static struct etr_buf *tmc_etr_get_sysfs_buffer(struct coresight_device *csdev)
1145 unsigned long flags;
1146 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1147 struct etr_buf *sysfs_buf = NULL, *new_buf = NULL, *free_buf = NULL;
1150 * If we are enabling the ETR from disabled state, we need to make
1151 * sure we have a buffer with the right size. The etr_buf is not reset
1152 * immediately after we stop the tracing in SYSFS mode as we wait for
1153 * the user to collect the data. We may be able to reuse the existing
1154 * buffer, provided the size matches. Any allocation has to be done
1155 * with the lock released.
1157 spin_lock_irqsave(&drvdata->spinlock, flags);
1158 sysfs_buf = READ_ONCE(drvdata->sysfs_buf);
1159 if (!sysfs_buf || (sysfs_buf->size != drvdata->size)) {
1160 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1162 /* Allocate memory with the locks released */
1163 free_buf = new_buf = tmc_etr_setup_sysfs_buf(drvdata);
1164 if (IS_ERR(new_buf))
1167 /* Let's try again */
1168 spin_lock_irqsave(&drvdata->spinlock, flags);
1171 if (drvdata->reading || drvdata->mode == CS_MODE_PERF) {
1177 * In sysFS mode we can have multiple writers per sink. Since this
1178 * sink is already enabled no memory is needed and the HW need not be
1179 * touched, even if the buffer size has changed.
1181 if (drvdata->mode == CS_MODE_SYSFS) {
1182 atomic_inc(&csdev->refcnt);
1187 * If we don't have a buffer or it doesn't match the requested size,
1188 * use the buffer allocated above. Otherwise reuse the existing buffer.
1190 sysfs_buf = READ_ONCE(drvdata->sysfs_buf);
1191 if (!sysfs_buf || (new_buf && sysfs_buf->size != new_buf->size)) {
1192 free_buf = sysfs_buf;
1193 drvdata->sysfs_buf = new_buf;
1197 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1199 /* Free memory outside the spinlock if need be */
1201 tmc_etr_free_sysfs_buf(free_buf);
1202 return ret ? ERR_PTR(ret) : drvdata->sysfs_buf;
1205 static int tmc_enable_etr_sink_sysfs(struct coresight_device *csdev)
1208 unsigned long flags;
1209 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1210 struct etr_buf *sysfs_buf = tmc_etr_get_sysfs_buffer(csdev);
1212 if (IS_ERR(sysfs_buf))
1213 return PTR_ERR(sysfs_buf);
1215 spin_lock_irqsave(&drvdata->spinlock, flags);
1216 ret = tmc_etr_enable_hw(drvdata, sysfs_buf);
1218 drvdata->mode = CS_MODE_SYSFS;
1219 atomic_inc(&csdev->refcnt);
1222 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1225 dev_dbg(&csdev->dev, "TMC-ETR enabled\n");
1230 struct etr_buf *tmc_etr_get_buffer(struct coresight_device *csdev,
1231 enum cs_mode mode, void *data)
1233 struct perf_output_handle *handle = data;
1234 struct etr_perf_buffer *etr_perf;
1238 return tmc_etr_get_sysfs_buffer(csdev);
1240 etr_perf = etm_perf_sink_config(handle);
1241 if (WARN_ON(!etr_perf || !etr_perf->etr_buf))
1242 return ERR_PTR(-EINVAL);
1243 return etr_perf->etr_buf;
1245 return ERR_PTR(-EINVAL);
1248 EXPORT_SYMBOL_GPL(tmc_etr_get_buffer);
1251 * alloc_etr_buf: Allocate ETR buffer for use by perf.
1252 * The size of the hardware buffer is dependent on the size configured
1253 * via sysfs and the perf ring buffer size. We prefer to allocate the
1254 * largest possible size, scaling down the size by half until it
1255 * reaches a minimum limit (1M), beyond which we give up.
1257 static struct etr_buf *
1258 alloc_etr_buf(struct tmc_drvdata *drvdata, struct perf_event *event,
1259 int nr_pages, void **pages, bool snapshot)
1262 struct etr_buf *etr_buf;
1265 node = (event->cpu == -1) ? NUMA_NO_NODE : cpu_to_node(event->cpu);
1267 * Try to match the perf ring buffer size if it is larger
1268 * than the size requested via sysfs.
1270 if ((nr_pages << PAGE_SHIFT) > drvdata->size) {
1271 etr_buf = tmc_alloc_etr_buf(drvdata, ((ssize_t)nr_pages << PAGE_SHIFT),
1273 if (!IS_ERR(etr_buf))
1278 * Else switch to configured size for this ETR
1279 * and scale down until we hit the minimum limit.
1281 size = drvdata->size;
1283 etr_buf = tmc_alloc_etr_buf(drvdata, size, 0, node, NULL);
1284 if (!IS_ERR(etr_buf))
1287 } while (size >= TMC_ETR_PERF_MIN_BUF_SIZE);
1289 return ERR_PTR(-ENOMEM);
1295 static struct etr_buf *
1296 get_perf_etr_buf_cpu_wide(struct tmc_drvdata *drvdata,
1297 struct perf_event *event, int nr_pages,
1298 void **pages, bool snapshot)
1301 pid_t pid = task_pid_nr(event->owner);
1302 struct etr_buf *etr_buf;
1306 * An etr_perf_buffer is associated with an event and holds a reference
1307 * to the AUX ring buffer that was created for that event. In CPU-wide
1308 * N:1 mode multiple events (one per CPU), each with its own AUX ring
1309 * buffer, share a sink. As such an etr_perf_buffer is created for each
1310 * event but a single etr_buf associated with the ETR is shared between
1311 * them. The last event in a trace session will copy the content of the
1312 * etr_buf to its AUX ring buffer. Ring buffer associated to other
1313 * events are simply not used an freed as events are destoyed. We still
1314 * need to allocate a ring buffer for each event since we don't know
1315 * which event will be last.
1319 * The first thing to do here is check if an etr_buf has already been
1320 * allocated for this session. If so it is shared with this event,
1321 * otherwise it is created.
1323 mutex_lock(&drvdata->idr_mutex);
1324 etr_buf = idr_find(&drvdata->idr, pid);
1326 refcount_inc(&etr_buf->refcount);
1327 mutex_unlock(&drvdata->idr_mutex);
1331 /* If we made it here no buffer has been allocated, do so now. */
1332 mutex_unlock(&drvdata->idr_mutex);
1334 etr_buf = alloc_etr_buf(drvdata, event, nr_pages, pages, snapshot);
1335 if (IS_ERR(etr_buf))
1338 /* Now that we have a buffer, add it to the IDR. */
1339 mutex_lock(&drvdata->idr_mutex);
1340 ret = idr_alloc(&drvdata->idr, etr_buf, pid, pid + 1, GFP_KERNEL);
1341 mutex_unlock(&drvdata->idr_mutex);
1343 /* Another event with this session ID has allocated this buffer. */
1344 if (ret == -ENOSPC) {
1345 tmc_free_etr_buf(etr_buf);
1349 /* The IDR can't allocate room for a new session, abandon ship. */
1350 if (ret == -ENOMEM) {
1351 tmc_free_etr_buf(etr_buf);
1352 return ERR_PTR(ret);
1359 static struct etr_buf *
1360 get_perf_etr_buf_per_thread(struct tmc_drvdata *drvdata,
1361 struct perf_event *event, int nr_pages,
1362 void **pages, bool snapshot)
1365 * In per-thread mode the etr_buf isn't shared, so just go ahead
1366 * with memory allocation.
1368 return alloc_etr_buf(drvdata, event, nr_pages, pages, snapshot);
1371 static struct etr_buf *
1372 get_perf_etr_buf(struct tmc_drvdata *drvdata, struct perf_event *event,
1373 int nr_pages, void **pages, bool snapshot)
1375 if (event->cpu == -1)
1376 return get_perf_etr_buf_per_thread(drvdata, event, nr_pages,
1379 return get_perf_etr_buf_cpu_wide(drvdata, event, nr_pages,
1383 static struct etr_perf_buffer *
1384 tmc_etr_setup_perf_buf(struct tmc_drvdata *drvdata, struct perf_event *event,
1385 int nr_pages, void **pages, bool snapshot)
1388 struct etr_buf *etr_buf;
1389 struct etr_perf_buffer *etr_perf;
1391 node = (event->cpu == -1) ? NUMA_NO_NODE : cpu_to_node(event->cpu);
1393 etr_perf = kzalloc_node(sizeof(*etr_perf), GFP_KERNEL, node);
1395 return ERR_PTR(-ENOMEM);
1397 etr_buf = get_perf_etr_buf(drvdata, event, nr_pages, pages, snapshot);
1398 if (!IS_ERR(etr_buf))
1402 return ERR_PTR(-ENOMEM);
1406 * Keep a reference to the ETR this buffer has been allocated for
1407 * in order to have access to the IDR in tmc_free_etr_buffer().
1409 etr_perf->drvdata = drvdata;
1410 etr_perf->etr_buf = etr_buf;
1416 static void *tmc_alloc_etr_buffer(struct coresight_device *csdev,
1417 struct perf_event *event, void **pages,
1418 int nr_pages, bool snapshot)
1420 struct etr_perf_buffer *etr_perf;
1421 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1423 etr_perf = tmc_etr_setup_perf_buf(drvdata, event,
1424 nr_pages, pages, snapshot);
1425 if (IS_ERR(etr_perf)) {
1426 dev_dbg(&csdev->dev, "Unable to allocate ETR buffer\n");
1430 etr_perf->pid = task_pid_nr(event->owner);
1431 etr_perf->snapshot = snapshot;
1432 etr_perf->nr_pages = nr_pages;
1433 etr_perf->pages = pages;
1438 static void tmc_free_etr_buffer(void *config)
1440 struct etr_perf_buffer *etr_perf = config;
1441 struct tmc_drvdata *drvdata = etr_perf->drvdata;
1442 struct etr_buf *buf, *etr_buf = etr_perf->etr_buf;
1445 goto free_etr_perf_buffer;
1447 mutex_lock(&drvdata->idr_mutex);
1448 /* If we are not the last one to use the buffer, don't touch it. */
1449 if (!refcount_dec_and_test(&etr_buf->refcount)) {
1450 mutex_unlock(&drvdata->idr_mutex);
1451 goto free_etr_perf_buffer;
1454 /* We are the last one, remove from the IDR and free the buffer. */
1455 buf = idr_remove(&drvdata->idr, etr_perf->pid);
1456 mutex_unlock(&drvdata->idr_mutex);
1459 * Something went very wrong if the buffer associated with this ID
1460 * is not the same in the IDR. Leak to avoid use after free.
1462 if (buf && WARN_ON(buf != etr_buf))
1463 goto free_etr_perf_buffer;
1465 tmc_free_etr_buf(etr_perf->etr_buf);
1467 free_etr_perf_buffer:
1472 * tmc_etr_sync_perf_buffer: Copy the actual trace data from the hardware
1473 * buffer to the perf ring buffer.
1475 static void tmc_etr_sync_perf_buffer(struct etr_perf_buffer *etr_perf,
1477 unsigned long src_offset,
1478 unsigned long to_copy)
1481 long pg_idx, pg_offset;
1482 char **dst_pages, *src_buf;
1483 struct etr_buf *etr_buf = etr_perf->etr_buf;
1485 head = PERF_IDX2OFF(head, etr_perf);
1486 pg_idx = head >> PAGE_SHIFT;
1487 pg_offset = head & (PAGE_SIZE - 1);
1488 dst_pages = (char **)etr_perf->pages;
1490 while (to_copy > 0) {
1492 * In one iteration, we can copy minimum of :
1493 * 1) what is available in the source buffer,
1494 * 2) what is available in the source buffer, before it
1496 * 3) what is available in the destination page.
1499 if (src_offset >= etr_buf->size)
1500 src_offset -= etr_buf->size;
1501 bytes = tmc_etr_buf_get_data(etr_buf, src_offset, to_copy,
1503 if (WARN_ON_ONCE(bytes <= 0))
1505 bytes = min(bytes, (long)(PAGE_SIZE - pg_offset));
1507 memcpy(dst_pages[pg_idx] + pg_offset, src_buf, bytes);
1511 /* Move destination pointers */
1513 if (pg_offset == PAGE_SIZE) {
1515 if (++pg_idx == etr_perf->nr_pages)
1519 /* Move source pointers */
1520 src_offset += bytes;
1525 * tmc_update_etr_buffer : Update the perf ring buffer with the
1526 * available trace data. We use software double buffering at the moment.
1528 * TODO: Add support for reusing the perf ring buffer.
1530 static unsigned long
1531 tmc_update_etr_buffer(struct coresight_device *csdev,
1532 struct perf_output_handle *handle,
1536 unsigned long flags, offset, size = 0;
1537 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1538 struct etr_perf_buffer *etr_perf = config;
1539 struct etr_buf *etr_buf = etr_perf->etr_buf;
1541 spin_lock_irqsave(&drvdata->spinlock, flags);
1543 /* Don't do anything if another tracer is using this sink */
1544 if (atomic_read(&csdev->refcnt) != 1) {
1545 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1549 if (WARN_ON(drvdata->perf_buf != etr_buf)) {
1551 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1555 CS_UNLOCK(drvdata->base);
1557 tmc_flush_and_stop(drvdata);
1558 tmc_sync_etr_buf(drvdata);
1560 CS_LOCK(drvdata->base);
1561 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1563 lost = etr_buf->full;
1564 offset = etr_buf->offset;
1565 size = etr_buf->len;
1568 * The ETR buffer may be bigger than the space available in the
1569 * perf ring buffer (handle->size). If so advance the offset so that we
1570 * get the latest trace data. In snapshot mode none of that matters
1571 * since we are expected to clobber stale data in favour of the latest
1574 if (!etr_perf->snapshot && size > handle->size) {
1575 u32 mask = tmc_get_memwidth_mask(drvdata);
1578 * Make sure the new size is aligned in accordance with the
1579 * requirement explained in function tmc_get_memwidth_mask().
1581 size = handle->size & mask;
1582 offset = etr_buf->offset + etr_buf->len - size;
1584 if (offset >= etr_buf->size)
1585 offset -= etr_buf->size;
1589 /* Insert barrier packets at the beginning, if there was an overflow */
1591 tmc_etr_buf_insert_barrier_packet(etr_buf, offset);
1592 tmc_etr_sync_perf_buffer(etr_perf, handle->head, offset, size);
1595 * In snapshot mode we simply increment the head by the number of byte
1596 * that were written. User space will figure out how many bytes to get
1597 * from the AUX buffer based on the position of the head.
1599 if (etr_perf->snapshot)
1600 handle->head += size;
1603 * Ensure that the AUX trace data is visible before the aux_head
1604 * is updated via perf_aux_output_end(), as expected by the
1611 * Don't set the TRUNCATED flag in snapshot mode because 1) the
1612 * captured buffer is expected to be truncated and 2) a full buffer
1613 * prevents the event from being re-enabled by the perf core,
1614 * resulting in stale data being send to user space.
1616 if (!etr_perf->snapshot && lost)
1617 perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED);
1621 static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data)
1625 unsigned long flags;
1626 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1627 struct perf_output_handle *handle = data;
1628 struct etr_perf_buffer *etr_perf = etm_perf_sink_config(handle);
1630 spin_lock_irqsave(&drvdata->spinlock, flags);
1631 /* Don't use this sink if it is already claimed by sysFS */
1632 if (drvdata->mode == CS_MODE_SYSFS) {
1637 if (WARN_ON(!etr_perf || !etr_perf->etr_buf)) {
1642 /* Get a handle on the pid of the process to monitor */
1643 pid = etr_perf->pid;
1645 /* Do not proceed if this device is associated with another session */
1646 if (drvdata->pid != -1 && drvdata->pid != pid) {
1652 * No HW configuration is needed if the sink is already in
1653 * use for this session.
1655 if (drvdata->pid == pid) {
1656 atomic_inc(&csdev->refcnt);
1660 rc = tmc_etr_enable_hw(drvdata, etr_perf->etr_buf);
1662 /* Associate with monitored process. */
1664 drvdata->mode = CS_MODE_PERF;
1665 drvdata->perf_buf = etr_perf->etr_buf;
1666 atomic_inc(&csdev->refcnt);
1670 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1674 static int tmc_enable_etr_sink(struct coresight_device *csdev,
1675 enum cs_mode mode, void *data)
1679 return tmc_enable_etr_sink_sysfs(csdev);
1681 return tmc_enable_etr_sink_perf(csdev, data);
1687 static int tmc_disable_etr_sink(struct coresight_device *csdev)
1689 unsigned long flags;
1690 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1692 spin_lock_irqsave(&drvdata->spinlock, flags);
1694 if (drvdata->reading) {
1695 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1699 if (atomic_dec_return(&csdev->refcnt)) {
1700 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1704 /* Complain if we (somehow) got out of sync */
1705 WARN_ON_ONCE(drvdata->mode == CS_MODE_DISABLED);
1706 tmc_etr_disable_hw(drvdata);
1707 /* Dissociate from monitored process. */
1709 drvdata->mode = CS_MODE_DISABLED;
1710 /* Reset perf specific data */
1711 drvdata->perf_buf = NULL;
1713 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1715 dev_dbg(&csdev->dev, "TMC-ETR disabled\n");
1719 static const struct coresight_ops_sink tmc_etr_sink_ops = {
1720 .enable = tmc_enable_etr_sink,
1721 .disable = tmc_disable_etr_sink,
1722 .alloc_buffer = tmc_alloc_etr_buffer,
1723 .update_buffer = tmc_update_etr_buffer,
1724 .free_buffer = tmc_free_etr_buffer,
1727 const struct coresight_ops tmc_etr_cs_ops = {
1728 .sink_ops = &tmc_etr_sink_ops,
1731 int tmc_read_prepare_etr(struct tmc_drvdata *drvdata)
1734 unsigned long flags;
1736 /* config types are set a boot time and never change */
1737 if (WARN_ON_ONCE(drvdata->config_type != TMC_CONFIG_TYPE_ETR))
1740 spin_lock_irqsave(&drvdata->spinlock, flags);
1741 if (drvdata->reading) {
1747 * We can safely allow reads even if the ETR is operating in PERF mode,
1748 * since the sysfs session is captured in mode specific data.
1749 * If drvdata::sysfs_data is NULL the trace data has been read already.
1751 if (!drvdata->sysfs_buf) {
1756 /* Disable the TMC if we are trying to read from a running session. */
1757 if (drvdata->mode == CS_MODE_SYSFS)
1758 __tmc_etr_disable_hw(drvdata);
1760 drvdata->reading = true;
1762 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1767 int tmc_read_unprepare_etr(struct tmc_drvdata *drvdata)
1769 unsigned long flags;
1770 struct etr_buf *sysfs_buf = NULL;
1772 /* config types are set a boot time and never change */
1773 if (WARN_ON_ONCE(drvdata->config_type != TMC_CONFIG_TYPE_ETR))
1776 spin_lock_irqsave(&drvdata->spinlock, flags);
1778 /* RE-enable the TMC if need be */
1779 if (drvdata->mode == CS_MODE_SYSFS) {
1781 * The trace run will continue with the same allocated trace
1782 * buffer. Since the tracer is still enabled drvdata::buf can't
1785 __tmc_etr_enable_hw(drvdata);
1788 * The ETR is not tracing and the buffer was just read.
1789 * As such prepare to free the trace buffer.
1791 sysfs_buf = drvdata->sysfs_buf;
1792 drvdata->sysfs_buf = NULL;
1795 drvdata->reading = false;
1796 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1798 /* Free allocated memory out side of the spinlock */
1800 tmc_etr_free_sysfs_buf(sysfs_buf);