1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright(C) 2016 Linaro Limited. All rights reserved.
4 * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
5 */
6
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"
21
22 struct etr_flat_buf {
23 struct device *dev;
24 dma_addr_t daddr;
25 void *vaddr;
26 size_t size;
27 };
28
29 struct etr_buf_hw {
30 bool has_iommu;
31 bool has_etr_sg;
32 bool has_catu;
33 bool has_resrv;
34 };
35
36 /*
37 * etr_perf_buffer - Perf buffer used for ETR
38 * @drvdata - The ETR drvdaga this buffer has been allocated for.
39 * @etr_buf - Actual buffer used by the ETR
40 * @pid - The PID of the session owner that etr_perf_buffer
41 * belongs to.
42 * @snaphost - Perf session mode
43 * @nr_pages - Number of pages in the ring buffer.
44 * @pages - Array of Pages in the ring buffer.
45 */
46 struct etr_perf_buffer {
47 struct tmc_drvdata *drvdata;
48 struct etr_buf *etr_buf;
49 pid_t pid;
50 bool snapshot;
51 int nr_pages;
52 void **pages;
53 };
54
55 /* Convert the perf index to an offset within the ETR buffer */
56 #define PERF_IDX2OFF(idx, buf) \
57 ((idx) % ((unsigned long)(buf)->nr_pages << PAGE_SHIFT))
58
59 /* Lower limit for ETR hardware buffer */
60 #define TMC_ETR_PERF_MIN_BUF_SIZE SZ_1M
61
62 /*
63 * The TMC ETR SG has a page size of 4K. The SG table contains pointers
64 * to 4KB buffers. However, the OS may use a PAGE_SIZE different from
65 * 4K (i.e, 16KB or 64KB). This implies that a single OS page could
66 * contain more than one SG buffer and tables.
67 *
68 * A table entry has the following format:
69 *
70 * ---Bit31------------Bit4-------Bit1-----Bit0--
71 * | Address[39:12] | SBZ | Entry Type |
72 * ----------------------------------------------
73 *
74 * Address: Bits [39:12] of a physical page address. Bits [11:0] are
75 * always zero.
76 *
77 * Entry type:
78 * b00 - Reserved.
79 * b01 - Last entry in the tables, points to 4K page buffer.
80 * b10 - Normal entry, points to 4K page buffer.
81 * b11 - Link. The address points to the base of next table.
82 */
83
84 typedef u32 sgte_t;
85
86 #define ETR_SG_PAGE_SHIFT 12
87 #define ETR_SG_PAGE_SIZE (1UL << ETR_SG_PAGE_SHIFT)
88 #define ETR_SG_PAGES_PER_SYSPAGE (PAGE_SIZE / ETR_SG_PAGE_SIZE)
89 #define ETR_SG_PTRS_PER_PAGE (ETR_SG_PAGE_SIZE / sizeof(sgte_t))
90 #define ETR_SG_PTRS_PER_SYSPAGE (PAGE_SIZE / sizeof(sgte_t))
91
92 #define ETR_SG_ET_MASK 0x3
93 #define ETR_SG_ET_LAST 0x1
94 #define ETR_SG_ET_NORMAL 0x2
95 #define ETR_SG_ET_LINK 0x3
96
97 #define ETR_SG_ADDR_SHIFT 4
98
99 #define ETR_SG_ENTRY(addr, type) \
100 (sgte_t)((((addr) >> ETR_SG_PAGE_SHIFT) << ETR_SG_ADDR_SHIFT) | \
101 (type & ETR_SG_ET_MASK))
102
103 #define ETR_SG_ADDR(entry) \
104 (((dma_addr_t)(entry) >> ETR_SG_ADDR_SHIFT) << ETR_SG_PAGE_SHIFT)
105 #define ETR_SG_ET(entry) ((entry) & ETR_SG_ET_MASK)
106
107 /*
108 * struct etr_sg_table : ETR SG Table
109 * @sg_table: Generic SG Table holding the data/table pages.
110 * @hwaddr: hwaddress used by the TMC, which is the base
111 * address of the table.
112 */
113 struct etr_sg_table {
114 struct tmc_sg_table *sg_table;
115 dma_addr_t hwaddr;
116 };
117
118 /*
119 * tmc_etr_sg_table_entries: Total number of table entries required to map
120 * @nr_pages system pages.
121 *
122 * We need to map @nr_pages * ETR_SG_PAGES_PER_SYSPAGE data pages.
123 * Each TMC page can map (ETR_SG_PTRS_PER_PAGE - 1) buffer pointers,
124 * with the last entry pointing to another page of table entries.
125 * If we spill over to a new page for mapping 1 entry, we could as
126 * well replace the link entry of the previous page with the last entry.
127 */
128 static inline unsigned long __attribute_const__
tmc_etr_sg_table_entries(int nr_pages)129 tmc_etr_sg_table_entries(int nr_pages)
130 {
131 unsigned long nr_sgpages = nr_pages * ETR_SG_PAGES_PER_SYSPAGE;
132 unsigned long nr_sglinks = nr_sgpages / (ETR_SG_PTRS_PER_PAGE - 1);
133 /*
134 * If we spill over to a new page for 1 entry, we could as well
135 * make it the LAST entry in the previous page, skipping the Link
136 * address.
137 */
138 if (nr_sglinks && (nr_sgpages % (ETR_SG_PTRS_PER_PAGE - 1) < 2))
139 nr_sglinks--;
140 return nr_sgpages + nr_sglinks;
141 }
142
143 /*
144 * tmc_pages_get_offset: Go through all the pages in the tmc_pages
145 * and map the device address @addr to an offset within the virtual
146 * contiguous buffer.
147 */
148 static long
tmc_pages_get_offset(struct tmc_pages * tmc_pages,dma_addr_t addr)149 tmc_pages_get_offset(struct tmc_pages *tmc_pages, dma_addr_t addr)
150 {
151 int i;
152 dma_addr_t page_start;
153
154 for (i = 0; i < tmc_pages->nr_pages; i++) {
155 page_start = tmc_pages->daddrs[i];
156 if (addr >= page_start && addr < (page_start + PAGE_SIZE))
157 return i * PAGE_SIZE + (addr - page_start);
158 }
159
160 return -EINVAL;
161 }
162
163 /*
164 * tmc_pages_free : Unmap and free the pages used by tmc_pages.
165 * If the pages were not allocated in tmc_pages_alloc(), we would
166 * simply drop the refcount.
167 */
tmc_pages_free(struct tmc_pages * tmc_pages,struct device * dev,enum dma_data_direction dir)168 static void tmc_pages_free(struct tmc_pages *tmc_pages,
169 struct device *dev, enum dma_data_direction dir)
170 {
171 int i;
172 struct device *real_dev = dev->parent;
173
174 for (i = 0; i < tmc_pages->nr_pages; i++) {
175 if (tmc_pages->daddrs && tmc_pages->daddrs[i])
176 dma_unmap_page(real_dev, tmc_pages->daddrs[i],
177 PAGE_SIZE, dir);
178 if (tmc_pages->pages && tmc_pages->pages[i])
179 __free_page(tmc_pages->pages[i]);
180 }
181
182 kfree(tmc_pages->pages);
183 kfree(tmc_pages->daddrs);
184 tmc_pages->pages = NULL;
185 tmc_pages->daddrs = NULL;
186 tmc_pages->nr_pages = 0;
187 }
188
189 /*
190 * tmc_pages_alloc : Allocate and map pages for a given @tmc_pages.
191 * If @pages is not NULL, the list of page virtual addresses are
192 * used as the data pages. The pages are then dma_map'ed for @dev
193 * with dma_direction @dir.
194 *
195 * Returns 0 upon success, else the error number.
196 */
tmc_pages_alloc(struct tmc_pages * tmc_pages,struct device * dev,int node,enum dma_data_direction dir,void ** pages)197 static int tmc_pages_alloc(struct tmc_pages *tmc_pages,
198 struct device *dev, int node,
199 enum dma_data_direction dir, void **pages)
200 {
201 int i, nr_pages;
202 dma_addr_t paddr;
203 struct page *page;
204 struct device *real_dev = dev->parent;
205
206 nr_pages = tmc_pages->nr_pages;
207 tmc_pages->daddrs = kcalloc(nr_pages, sizeof(*tmc_pages->daddrs),
208 GFP_KERNEL);
209 if (!tmc_pages->daddrs)
210 return -ENOMEM;
211 tmc_pages->pages = kcalloc(nr_pages, sizeof(*tmc_pages->pages),
212 GFP_KERNEL);
213 if (!tmc_pages->pages) {
214 kfree(tmc_pages->daddrs);
215 tmc_pages->daddrs = NULL;
216 return -ENOMEM;
217 }
218
219 for (i = 0; i < nr_pages; i++) {
220 if (pages && pages[i]) {
221 page = virt_to_page(pages[i]);
222 /* Hold a refcount on the page */
223 get_page(page);
224 } else {
225 page = alloc_pages_node(node,
226 GFP_KERNEL | __GFP_ZERO, 0);
227 if (!page)
228 goto err;
229 }
230 paddr = dma_map_page(real_dev, page, 0, PAGE_SIZE, dir);
231 if (dma_mapping_error(real_dev, paddr))
232 goto err;
233 tmc_pages->daddrs[i] = paddr;
234 tmc_pages->pages[i] = page;
235 }
236 return 0;
237 err:
238 tmc_pages_free(tmc_pages, dev, dir);
239 return -ENOMEM;
240 }
241
242 static inline long
tmc_sg_get_data_page_offset(struct tmc_sg_table * sg_table,dma_addr_t addr)243 tmc_sg_get_data_page_offset(struct tmc_sg_table *sg_table, dma_addr_t addr)
244 {
245 return tmc_pages_get_offset(&sg_table->data_pages, addr);
246 }
247
tmc_free_table_pages(struct tmc_sg_table * sg_table)248 static inline void tmc_free_table_pages(struct tmc_sg_table *sg_table)
249 {
250 if (sg_table->table_vaddr)
251 vunmap(sg_table->table_vaddr);
252 tmc_pages_free(&sg_table->table_pages, sg_table->dev, DMA_TO_DEVICE);
253 }
254
tmc_free_data_pages(struct tmc_sg_table * sg_table)255 static void tmc_free_data_pages(struct tmc_sg_table *sg_table)
256 {
257 if (sg_table->data_vaddr)
258 vunmap(sg_table->data_vaddr);
259 tmc_pages_free(&sg_table->data_pages, sg_table->dev, DMA_FROM_DEVICE);
260 }
261
tmc_free_sg_table(struct tmc_sg_table * sg_table)262 void tmc_free_sg_table(struct tmc_sg_table *sg_table)
263 {
264 tmc_free_table_pages(sg_table);
265 tmc_free_data_pages(sg_table);
266 kfree(sg_table);
267 }
268 EXPORT_SYMBOL_GPL(tmc_free_sg_table);
269
270 /*
271 * Alloc pages for the table. Since this will be used by the device,
272 * allocate the pages closer to the device (i.e, dev_to_node(dev)
273 * rather than the CPU node).
274 */
tmc_alloc_table_pages(struct tmc_sg_table * sg_table)275 static int tmc_alloc_table_pages(struct tmc_sg_table *sg_table)
276 {
277 int rc;
278 struct tmc_pages *table_pages = &sg_table->table_pages;
279
280 rc = tmc_pages_alloc(table_pages, sg_table->dev,
281 dev_to_node(sg_table->dev),
282 DMA_TO_DEVICE, NULL);
283 if (rc)
284 return rc;
285 sg_table->table_vaddr = vmap(table_pages->pages,
286 table_pages->nr_pages,
287 VM_MAP,
288 PAGE_KERNEL);
289 if (!sg_table->table_vaddr)
290 rc = -ENOMEM;
291 else
292 sg_table->table_daddr = table_pages->daddrs[0];
293 return rc;
294 }
295
tmc_alloc_data_pages(struct tmc_sg_table * sg_table,void ** pages)296 static int tmc_alloc_data_pages(struct tmc_sg_table *sg_table, void **pages)
297 {
298 int rc;
299
300 /* Allocate data pages on the node requested by the caller */
301 rc = tmc_pages_alloc(&sg_table->data_pages,
302 sg_table->dev, sg_table->node,
303 DMA_FROM_DEVICE, pages);
304 if (!rc) {
305 sg_table->data_vaddr = vmap(sg_table->data_pages.pages,
306 sg_table->data_pages.nr_pages,
307 VM_MAP,
308 PAGE_KERNEL);
309 if (!sg_table->data_vaddr)
310 rc = -ENOMEM;
311 }
312 return rc;
313 }
314
315 /*
316 * tmc_alloc_sg_table: Allocate and setup dma pages for the TMC SG table
317 * and data buffers. TMC writes to the data buffers and reads from the SG
318 * Table pages.
319 *
320 * @dev - Coresight device to which page should be DMA mapped.
321 * @node - Numa node for mem allocations
322 * @nr_tpages - Number of pages for the table entries.
323 * @nr_dpages - Number of pages for Data buffer.
324 * @pages - Optional list of virtual address of pages.
325 */
tmc_alloc_sg_table(struct device * dev,int node,int nr_tpages,int nr_dpages,void ** pages)326 struct tmc_sg_table *tmc_alloc_sg_table(struct device *dev,
327 int node,
328 int nr_tpages,
329 int nr_dpages,
330 void **pages)
331 {
332 long rc;
333 struct tmc_sg_table *sg_table;
334
335 sg_table = kzalloc(sizeof(*sg_table), GFP_KERNEL);
336 if (!sg_table)
337 return ERR_PTR(-ENOMEM);
338 sg_table->data_pages.nr_pages = nr_dpages;
339 sg_table->table_pages.nr_pages = nr_tpages;
340 sg_table->node = node;
341 sg_table->dev = dev;
342
343 rc = tmc_alloc_data_pages(sg_table, pages);
344 if (!rc)
345 rc = tmc_alloc_table_pages(sg_table);
346 if (rc) {
347 tmc_free_sg_table(sg_table);
348 return ERR_PTR(rc);
349 }
350
351 return sg_table;
352 }
353 EXPORT_SYMBOL_GPL(tmc_alloc_sg_table);
354
355 /*
356 * tmc_sg_table_sync_data_range: Sync the data buffer written
357 * by the device from @offset upto a @size bytes.
358 */
tmc_sg_table_sync_data_range(struct tmc_sg_table * table,u64 offset,u64 size)359 void tmc_sg_table_sync_data_range(struct tmc_sg_table *table,
360 u64 offset, u64 size)
361 {
362 int i, index, start;
363 int npages = DIV_ROUND_UP(size, PAGE_SIZE);
364 struct device *real_dev = table->dev->parent;
365 struct tmc_pages *data = &table->data_pages;
366
367 start = offset >> PAGE_SHIFT;
368 for (i = start; i < (start + npages); i++) {
369 index = i % data->nr_pages;
370 dma_sync_single_for_cpu(real_dev, data->daddrs[index],
371 PAGE_SIZE, DMA_FROM_DEVICE);
372 }
373 }
374 EXPORT_SYMBOL_GPL(tmc_sg_table_sync_data_range);
375
376 /* tmc_sg_sync_table: Sync the page table */
tmc_sg_table_sync_table(struct tmc_sg_table * sg_table)377 void tmc_sg_table_sync_table(struct tmc_sg_table *sg_table)
378 {
379 int i;
380 struct device *real_dev = sg_table->dev->parent;
381 struct tmc_pages *table_pages = &sg_table->table_pages;
382
383 for (i = 0; i < table_pages->nr_pages; i++)
384 dma_sync_single_for_device(real_dev, table_pages->daddrs[i],
385 PAGE_SIZE, DMA_TO_DEVICE);
386 }
387 EXPORT_SYMBOL_GPL(tmc_sg_table_sync_table);
388
389 /*
390 * tmc_sg_table_get_data: Get the buffer pointer for data @offset
391 * in the SG buffer. The @bufpp is updated to point to the buffer.
392 * Returns :
393 * the length of linear data available at @offset.
394 * or
395 * <= 0 if no data is available.
396 */
tmc_sg_table_get_data(struct tmc_sg_table * sg_table,u64 offset,size_t len,char ** bufpp)397 ssize_t tmc_sg_table_get_data(struct tmc_sg_table *sg_table,
398 u64 offset, size_t len, char **bufpp)
399 {
400 size_t size;
401 int pg_idx = offset >> PAGE_SHIFT;
402 int pg_offset = offset & (PAGE_SIZE - 1);
403 struct tmc_pages *data_pages = &sg_table->data_pages;
404
405 size = tmc_sg_table_buf_size(sg_table);
406 if (offset >= size)
407 return -EINVAL;
408
409 /* Make sure we don't go beyond the end */
410 len = (len < (size - offset)) ? len : size - offset;
411 /* Respect the page boundaries */
412 len = (len < (PAGE_SIZE - pg_offset)) ? len : (PAGE_SIZE - pg_offset);
413 if (len > 0)
414 *bufpp = page_address(data_pages->pages[pg_idx]) + pg_offset;
415 return len;
416 }
417 EXPORT_SYMBOL_GPL(tmc_sg_table_get_data);
418
419 #ifdef ETR_SG_DEBUG
420 /* Map a dma address to virtual address */
421 static unsigned long
tmc_sg_daddr_to_vaddr(struct tmc_sg_table * sg_table,dma_addr_t addr,bool table)422 tmc_sg_daddr_to_vaddr(struct tmc_sg_table *sg_table,
423 dma_addr_t addr, bool table)
424 {
425 long offset;
426 unsigned long base;
427 struct tmc_pages *tmc_pages;
428
429 if (table) {
430 tmc_pages = &sg_table->table_pages;
431 base = (unsigned long)sg_table->table_vaddr;
432 } else {
433 tmc_pages = &sg_table->data_pages;
434 base = (unsigned long)sg_table->data_vaddr;
435 }
436
437 offset = tmc_pages_get_offset(tmc_pages, addr);
438 if (offset < 0)
439 return 0;
440 return base + offset;
441 }
442
443 /* Dump the given sg_table */
tmc_etr_sg_table_dump(struct etr_sg_table * etr_table)444 static void tmc_etr_sg_table_dump(struct etr_sg_table *etr_table)
445 {
446 sgte_t *ptr;
447 int i = 0;
448 dma_addr_t addr;
449 struct tmc_sg_table *sg_table = etr_table->sg_table;
450
451 ptr = (sgte_t *)tmc_sg_daddr_to_vaddr(sg_table,
452 etr_table->hwaddr, true);
453 while (ptr) {
454 addr = ETR_SG_ADDR(*ptr);
455 switch (ETR_SG_ET(*ptr)) {
456 case ETR_SG_ET_NORMAL:
457 dev_dbg(sg_table->dev,
458 "%05d: %p\t:[N] 0x%llx\n", i, ptr, addr);
459 ptr++;
460 break;
461 case ETR_SG_ET_LINK:
462 dev_dbg(sg_table->dev,
463 "%05d: *** %p\t:{L} 0x%llx ***\n",
464 i, ptr, addr);
465 ptr = (sgte_t *)tmc_sg_daddr_to_vaddr(sg_table,
466 addr, true);
467 break;
468 case ETR_SG_ET_LAST:
469 dev_dbg(sg_table->dev,
470 "%05d: ### %p\t:[L] 0x%llx ###\n",
471 i, ptr, addr);
472 return;
473 default:
474 dev_dbg(sg_table->dev,
475 "%05d: xxx %p\t:[INVALID] 0x%llx xxx\n",
476 i, ptr, addr);
477 return;
478 }
479 i++;
480 }
481 dev_dbg(sg_table->dev, "******* End of Table *****\n");
482 }
483 #else
tmc_etr_sg_table_dump(struct etr_sg_table * etr_table)484 static inline void tmc_etr_sg_table_dump(struct etr_sg_table *etr_table) {}
485 #endif
486
487 /*
488 * Populate the SG Table page table entries from table/data
489 * pages allocated. Each Data page has ETR_SG_PAGES_PER_SYSPAGE SG pages.
490 * So does a Table page. So we keep track of indices of the tables
491 * in each system page and move the pointers accordingly.
492 */
493 #define INC_IDX_ROUND(idx, size) ((idx) = ((idx) + 1) % (size))
tmc_etr_sg_table_populate(struct etr_sg_table * etr_table)494 static void tmc_etr_sg_table_populate(struct etr_sg_table *etr_table)
495 {
496 dma_addr_t paddr;
497 int i, type, nr_entries;
498 int tpidx = 0; /* index to the current system table_page */
499 int sgtidx = 0; /* index to the sg_table within the current syspage */
500 int sgtentry = 0; /* the entry within the sg_table */
501 int dpidx = 0; /* index to the current system data_page */
502 int spidx = 0; /* index to the SG page within the current data page */
503 sgte_t *ptr; /* pointer to the table entry to fill */
504 struct tmc_sg_table *sg_table = etr_table->sg_table;
505 dma_addr_t *table_daddrs = sg_table->table_pages.daddrs;
506 dma_addr_t *data_daddrs = sg_table->data_pages.daddrs;
507
508 nr_entries = tmc_etr_sg_table_entries(sg_table->data_pages.nr_pages);
509 /*
510 * Use the contiguous virtual address of the table to update entries.
511 */
512 ptr = sg_table->table_vaddr;
513 /*
514 * Fill all the entries, except the last entry to avoid special
515 * checks within the loop.
516 */
517 for (i = 0; i < nr_entries - 1; i++) {
518 if (sgtentry == ETR_SG_PTRS_PER_PAGE - 1) {
519 /*
520 * Last entry in a sg_table page is a link address to
521 * the next table page. If this sg_table is the last
522 * one in the system page, it links to the first
523 * sg_table in the next system page. Otherwise, it
524 * links to the next sg_table page within the system
525 * page.
526 */
527 if (sgtidx == ETR_SG_PAGES_PER_SYSPAGE - 1) {
528 paddr = table_daddrs[tpidx + 1];
529 } else {
530 paddr = table_daddrs[tpidx] +
531 (ETR_SG_PAGE_SIZE * (sgtidx + 1));
532 }
533 type = ETR_SG_ET_LINK;
534 } else {
535 /*
536 * Update the indices to the data_pages to point to the
537 * next sg_page in the data buffer.
538 */
539 type = ETR_SG_ET_NORMAL;
540 paddr = data_daddrs[dpidx] + spidx * ETR_SG_PAGE_SIZE;
541 if (!INC_IDX_ROUND(spidx, ETR_SG_PAGES_PER_SYSPAGE))
542 dpidx++;
543 }
544 *ptr++ = ETR_SG_ENTRY(paddr, type);
545 /*
546 * Move to the next table pointer, moving the table page index
547 * if necessary
548 */
549 if (!INC_IDX_ROUND(sgtentry, ETR_SG_PTRS_PER_PAGE)) {
550 if (!INC_IDX_ROUND(sgtidx, ETR_SG_PAGES_PER_SYSPAGE))
551 tpidx++;
552 }
553 }
554
555 /* Set up the last entry, which is always a data pointer */
556 paddr = data_daddrs[dpidx] + spidx * ETR_SG_PAGE_SIZE;
557 *ptr++ = ETR_SG_ENTRY(paddr, ETR_SG_ET_LAST);
558 }
559
560 /*
561 * tmc_init_etr_sg_table: Allocate a TMC ETR SG table, data buffer of @size and
562 * populate the table.
563 *
564 * @dev - Device pointer for the TMC
565 * @node - NUMA node where the memory should be allocated
566 * @size - Total size of the data buffer
567 * @pages - Optional list of page virtual address
568 */
569 static struct etr_sg_table *
tmc_init_etr_sg_table(struct device * dev,int node,unsigned long size,void ** pages)570 tmc_init_etr_sg_table(struct device *dev, int node,
571 unsigned long size, void **pages)
572 {
573 int nr_entries, nr_tpages;
574 int nr_dpages = size >> PAGE_SHIFT;
575 struct tmc_sg_table *sg_table;
576 struct etr_sg_table *etr_table;
577
578 etr_table = kzalloc(sizeof(*etr_table), GFP_KERNEL);
579 if (!etr_table)
580 return ERR_PTR(-ENOMEM);
581 nr_entries = tmc_etr_sg_table_entries(nr_dpages);
582 nr_tpages = DIV_ROUND_UP(nr_entries, ETR_SG_PTRS_PER_SYSPAGE);
583
584 sg_table = tmc_alloc_sg_table(dev, node, nr_tpages, nr_dpages, pages);
585 if (IS_ERR(sg_table)) {
586 kfree(etr_table);
587 return ERR_CAST(sg_table);
588 }
589
590 etr_table->sg_table = sg_table;
591 /* TMC should use table base address for DBA */
592 etr_table->hwaddr = sg_table->table_daddr;
593 tmc_etr_sg_table_populate(etr_table);
594 /* Sync the table pages for the HW */
595 tmc_sg_table_sync_table(sg_table);
596 tmc_etr_sg_table_dump(etr_table);
597
598 return etr_table;
599 }
600
601 /*
602 * tmc_etr_alloc_flat_buf: Allocate a contiguous DMA buffer.
603 */
tmc_etr_alloc_flat_buf(struct tmc_drvdata * drvdata,struct etr_buf * etr_buf,int node,void ** pages)604 static int tmc_etr_alloc_flat_buf(struct tmc_drvdata *drvdata,
605 struct etr_buf *etr_buf, int node,
606 void **pages)
607 {
608 struct etr_flat_buf *flat_buf;
609 struct device *real_dev = drvdata->csdev->dev.parent;
610
611 /* We cannot reuse existing pages for flat buf */
612 if (pages)
613 return -EINVAL;
614
615 flat_buf = kzalloc(sizeof(*flat_buf), GFP_KERNEL);
616 if (!flat_buf)
617 return -ENOMEM;
618
619 flat_buf->vaddr = dma_alloc_noncoherent(real_dev, etr_buf->size,
620 &flat_buf->daddr,
621 DMA_FROM_DEVICE,
622 GFP_KERNEL | __GFP_NOWARN);
623 if (!flat_buf->vaddr) {
624 kfree(flat_buf);
625 return -ENOMEM;
626 }
627
628 flat_buf->size = etr_buf->size;
629 flat_buf->dev = &drvdata->csdev->dev;
630 etr_buf->hwaddr = flat_buf->daddr;
631 etr_buf->mode = ETR_MODE_FLAT;
632 etr_buf->private = flat_buf;
633 return 0;
634 }
635
tmc_etr_free_flat_buf(struct etr_buf * etr_buf)636 static void tmc_etr_free_flat_buf(struct etr_buf *etr_buf)
637 {
638 struct etr_flat_buf *flat_buf = etr_buf->private;
639
640 if (flat_buf && flat_buf->daddr) {
641 struct device *real_dev = flat_buf->dev->parent;
642
643 dma_free_noncoherent(real_dev, etr_buf->size,
644 flat_buf->vaddr, flat_buf->daddr,
645 DMA_FROM_DEVICE);
646 }
647 kfree(flat_buf);
648 }
649
tmc_etr_sync_flat_buf(struct etr_buf * etr_buf,u64 rrp,u64 rwp)650 static void tmc_etr_sync_flat_buf(struct etr_buf *etr_buf, u64 rrp, u64 rwp)
651 {
652 struct etr_flat_buf *flat_buf = etr_buf->private;
653 struct device *real_dev = flat_buf->dev->parent;
654
655 /*
656 * Adjust the buffer to point to the beginning of the trace data
657 * and update the available trace data.
658 */
659 etr_buf->offset = rrp - etr_buf->hwaddr;
660 if (etr_buf->full)
661 etr_buf->len = etr_buf->size;
662 else
663 etr_buf->len = rwp - rrp;
664
665 /*
666 * The driver always starts tracing at the beginning of the buffer,
667 * the only reason why we would get a wrap around is when the buffer
668 * is full. Sync the entire buffer in one go for this case.
669 */
670 if (etr_buf->offset + etr_buf->len > etr_buf->size)
671 dma_sync_single_for_cpu(real_dev, flat_buf->daddr,
672 etr_buf->size, DMA_FROM_DEVICE);
673 else
674 dma_sync_single_for_cpu(real_dev,
675 flat_buf->daddr + etr_buf->offset,
676 etr_buf->len, DMA_FROM_DEVICE);
677 }
678
tmc_etr_get_data_flat_buf(struct etr_buf * etr_buf,u64 offset,size_t len,char ** bufpp)679 static ssize_t tmc_etr_get_data_flat_buf(struct etr_buf *etr_buf,
680 u64 offset, size_t len, char **bufpp)
681 {
682 struct etr_flat_buf *flat_buf = etr_buf->private;
683
684 *bufpp = (char *)flat_buf->vaddr + offset;
685 /*
686 * tmc_etr_buf_get_data already adjusts the length to handle
687 * buffer wrapping around.
688 */
689 return len;
690 }
691
692 static const struct etr_buf_operations etr_flat_buf_ops = {
693 .alloc = tmc_etr_alloc_flat_buf,
694 .free = tmc_etr_free_flat_buf,
695 .sync = tmc_etr_sync_flat_buf,
696 .get_data = tmc_etr_get_data_flat_buf,
697 };
698
699 /*
700 * tmc_etr_alloc_resrv_buf: Allocate a contiguous DMA buffer from reserved region.
701 */
tmc_etr_alloc_resrv_buf(struct tmc_drvdata * drvdata,struct etr_buf * etr_buf,int node,void ** pages)702 static int tmc_etr_alloc_resrv_buf(struct tmc_drvdata *drvdata,
703 struct etr_buf *etr_buf, int node,
704 void **pages)
705 {
706 struct etr_flat_buf *resrv_buf;
707 struct device *real_dev = drvdata->csdev->dev.parent;
708
709 /* We cannot reuse existing pages for resrv buf */
710 if (pages)
711 return -EINVAL;
712
713 resrv_buf = kzalloc(sizeof(*resrv_buf), GFP_KERNEL);
714 if (!resrv_buf)
715 return -ENOMEM;
716
717 resrv_buf->daddr = dma_map_resource(real_dev, drvdata->resrv_buf.paddr,
718 drvdata->resrv_buf.size,
719 DMA_FROM_DEVICE, 0);
720 if (dma_mapping_error(real_dev, resrv_buf->daddr)) {
721 dev_err(real_dev, "failed to map source buffer address\n");
722 kfree(resrv_buf);
723 return -ENOMEM;
724 }
725
726 resrv_buf->vaddr = drvdata->resrv_buf.vaddr;
727 resrv_buf->size = etr_buf->size = drvdata->resrv_buf.size;
728 resrv_buf->dev = &drvdata->csdev->dev;
729 etr_buf->hwaddr = resrv_buf->daddr;
730 etr_buf->mode = ETR_MODE_RESRV;
731 etr_buf->private = resrv_buf;
732 return 0;
733 }
734
tmc_etr_free_resrv_buf(struct etr_buf * etr_buf)735 static void tmc_etr_free_resrv_buf(struct etr_buf *etr_buf)
736 {
737 struct etr_flat_buf *resrv_buf = etr_buf->private;
738
739 if (resrv_buf && resrv_buf->daddr) {
740 struct device *real_dev = resrv_buf->dev->parent;
741
742 dma_unmap_resource(real_dev, resrv_buf->daddr,
743 resrv_buf->size, DMA_FROM_DEVICE, 0);
744 }
745 kfree(resrv_buf);
746 }
747
tmc_etr_sync_resrv_buf(struct etr_buf * etr_buf,u64 rrp,u64 rwp)748 static void tmc_etr_sync_resrv_buf(struct etr_buf *etr_buf, u64 rrp, u64 rwp)
749 {
750 /*
751 * Adjust the buffer to point to the beginning of the trace data
752 * and update the available trace data.
753 */
754 etr_buf->offset = rrp - etr_buf->hwaddr;
755 if (etr_buf->full)
756 etr_buf->len = etr_buf->size;
757 else
758 etr_buf->len = rwp - rrp;
759 }
760
761 static const struct etr_buf_operations etr_resrv_buf_ops = {
762 .alloc = tmc_etr_alloc_resrv_buf,
763 .free = tmc_etr_free_resrv_buf,
764 .sync = tmc_etr_sync_resrv_buf,
765 .get_data = tmc_etr_get_data_flat_buf,
766 };
767
768 /*
769 * tmc_etr_alloc_sg_buf: Allocate an SG buf @etr_buf. Setup the parameters
770 * appropriately.
771 */
tmc_etr_alloc_sg_buf(struct tmc_drvdata * drvdata,struct etr_buf * etr_buf,int node,void ** pages)772 static int tmc_etr_alloc_sg_buf(struct tmc_drvdata *drvdata,
773 struct etr_buf *etr_buf, int node,
774 void **pages)
775 {
776 struct etr_sg_table *etr_table;
777 struct device *dev = &drvdata->csdev->dev;
778
779 etr_table = tmc_init_etr_sg_table(dev, node,
780 etr_buf->size, pages);
781 if (IS_ERR(etr_table))
782 return -ENOMEM;
783 etr_buf->hwaddr = etr_table->hwaddr;
784 etr_buf->mode = ETR_MODE_ETR_SG;
785 etr_buf->private = etr_table;
786 return 0;
787 }
788
tmc_etr_free_sg_buf(struct etr_buf * etr_buf)789 static void tmc_etr_free_sg_buf(struct etr_buf *etr_buf)
790 {
791 struct etr_sg_table *etr_table = etr_buf->private;
792
793 if (etr_table) {
794 tmc_free_sg_table(etr_table->sg_table);
795 kfree(etr_table);
796 }
797 }
798
tmc_etr_get_data_sg_buf(struct etr_buf * etr_buf,u64 offset,size_t len,char ** bufpp)799 static ssize_t tmc_etr_get_data_sg_buf(struct etr_buf *etr_buf, u64 offset,
800 size_t len, char **bufpp)
801 {
802 struct etr_sg_table *etr_table = etr_buf->private;
803
804 return tmc_sg_table_get_data(etr_table->sg_table, offset, len, bufpp);
805 }
806
tmc_etr_sync_sg_buf(struct etr_buf * etr_buf,u64 rrp,u64 rwp)807 static void tmc_etr_sync_sg_buf(struct etr_buf *etr_buf, u64 rrp, u64 rwp)
808 {
809 long r_offset, w_offset;
810 struct etr_sg_table *etr_table = etr_buf->private;
811 struct tmc_sg_table *table = etr_table->sg_table;
812
813 /* Convert hw address to offset in the buffer */
814 r_offset = tmc_sg_get_data_page_offset(table, rrp);
815 if (r_offset < 0) {
816 dev_warn(table->dev,
817 "Unable to map RRP %llx to offset\n", rrp);
818 etr_buf->len = 0;
819 return;
820 }
821
822 w_offset = tmc_sg_get_data_page_offset(table, rwp);
823 if (w_offset < 0) {
824 dev_warn(table->dev,
825 "Unable to map RWP %llx to offset\n", rwp);
826 etr_buf->len = 0;
827 return;
828 }
829
830 etr_buf->offset = r_offset;
831 if (etr_buf->full)
832 etr_buf->len = etr_buf->size;
833 else
834 etr_buf->len = ((w_offset < r_offset) ? etr_buf->size : 0) +
835 w_offset - r_offset;
836 tmc_sg_table_sync_data_range(table, r_offset, etr_buf->len);
837 }
838
839 static const struct etr_buf_operations etr_sg_buf_ops = {
840 .alloc = tmc_etr_alloc_sg_buf,
841 .free = tmc_etr_free_sg_buf,
842 .sync = tmc_etr_sync_sg_buf,
843 .get_data = tmc_etr_get_data_sg_buf,
844 };
845
846 /*
847 * TMC ETR could be connected to a CATU device, which can provide address
848 * translation service. This is represented by the Output port of the TMC
849 * (ETR) connected to the input port of the CATU.
850 *
851 * Returns : coresight_device ptr for the CATU device if a CATU is found.
852 * : NULL otherwise.
853 */
854 struct coresight_device *
tmc_etr_get_catu_device(struct tmc_drvdata * drvdata)855 tmc_etr_get_catu_device(struct tmc_drvdata *drvdata)
856 {
857 struct coresight_device *etr = drvdata->csdev;
858 union coresight_dev_subtype catu_subtype = {
859 .helper_subtype = CORESIGHT_DEV_SUBTYPE_HELPER_CATU
860 };
861
862 if (!IS_ENABLED(CONFIG_CORESIGHT_CATU))
863 return NULL;
864
865 return coresight_find_output_type(etr->pdata, CORESIGHT_DEV_TYPE_HELPER,
866 catu_subtype);
867 }
868 EXPORT_SYMBOL_GPL(tmc_etr_get_catu_device);
869
870 static const struct etr_buf_operations *etr_buf_ops[] = {
871 [ETR_MODE_FLAT] = &etr_flat_buf_ops,
872 [ETR_MODE_ETR_SG] = &etr_sg_buf_ops,
873 [ETR_MODE_CATU] = NULL,
874 [ETR_MODE_RESRV] = &etr_resrv_buf_ops
875 };
876
tmc_etr_set_catu_ops(const struct etr_buf_operations * catu)877 void tmc_etr_set_catu_ops(const struct etr_buf_operations *catu)
878 {
879 etr_buf_ops[ETR_MODE_CATU] = catu;
880 }
881 EXPORT_SYMBOL_GPL(tmc_etr_set_catu_ops);
882
tmc_etr_remove_catu_ops(void)883 void tmc_etr_remove_catu_ops(void)
884 {
885 etr_buf_ops[ETR_MODE_CATU] = NULL;
886 }
887 EXPORT_SYMBOL_GPL(tmc_etr_remove_catu_ops);
888
tmc_etr_mode_alloc_buf(int mode,struct tmc_drvdata * drvdata,struct etr_buf * etr_buf,int node,void ** pages)889 static inline int tmc_etr_mode_alloc_buf(int mode,
890 struct tmc_drvdata *drvdata,
891 struct etr_buf *etr_buf, int node,
892 void **pages)
893 {
894 int rc = -EINVAL;
895
896 switch (mode) {
897 case ETR_MODE_FLAT:
898 case ETR_MODE_ETR_SG:
899 case ETR_MODE_CATU:
900 case ETR_MODE_RESRV:
901 if (etr_buf_ops[mode] && etr_buf_ops[mode]->alloc)
902 rc = etr_buf_ops[mode]->alloc(drvdata, etr_buf,
903 node, pages);
904 if (!rc)
905 etr_buf->ops = etr_buf_ops[mode];
906 return rc;
907 default:
908 return -EINVAL;
909 }
910 }
911
get_etr_buf_hw(struct device * dev,struct etr_buf_hw * buf_hw)912 static void get_etr_buf_hw(struct device *dev, struct etr_buf_hw *buf_hw)
913 {
914 struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
915
916 buf_hw->has_iommu = iommu_get_domain_for_dev(dev->parent);
917 buf_hw->has_etr_sg = tmc_etr_has_cap(drvdata, TMC_ETR_SG);
918 buf_hw->has_catu = !!tmc_etr_get_catu_device(drvdata);
919 buf_hw->has_resrv = tmc_has_reserved_buffer(drvdata);
920 }
921
etr_can_use_flat_mode(struct etr_buf_hw * buf_hw,ssize_t etr_buf_size)922 static bool etr_can_use_flat_mode(struct etr_buf_hw *buf_hw, ssize_t etr_buf_size)
923 {
924 bool has_sg = buf_hw->has_catu || buf_hw->has_etr_sg;
925
926 return !has_sg || buf_hw->has_iommu || etr_buf_size < SZ_1M;
927 }
928
929 /*
930 * tmc_alloc_etr_buf: Allocate a buffer use by ETR.
931 * @drvdata : ETR device details.
932 * @size : size of the requested buffer.
933 * @flags : Required properties for the buffer.
934 * @node : Node for memory allocations.
935 * @pages : An optional list of pages.
936 */
tmc_alloc_etr_buf(struct tmc_drvdata * drvdata,ssize_t size,int flags,int node,void ** pages)937 static struct etr_buf *tmc_alloc_etr_buf(struct tmc_drvdata *drvdata,
938 ssize_t size, int flags,
939 int node, void **pages)
940 {
941 int rc = -ENOMEM;
942 struct etr_buf *etr_buf;
943 struct etr_buf_hw buf_hw;
944 struct device *dev = &drvdata->csdev->dev;
945
946 get_etr_buf_hw(dev, &buf_hw);
947 etr_buf = kzalloc(sizeof(*etr_buf), GFP_KERNEL);
948 if (!etr_buf)
949 return ERR_PTR(-ENOMEM);
950
951 etr_buf->size = size;
952
953 /* If there is user directive for buffer mode, try that first */
954 if (drvdata->etr_mode != ETR_MODE_AUTO)
955 rc = tmc_etr_mode_alloc_buf(drvdata->etr_mode, drvdata,
956 etr_buf, node, pages);
957
958 /*
959 * If we have to use an existing list of pages, we cannot reliably
960 * use a contiguous DMA memory (even if we have an IOMMU). Otherwise,
961 * we use the contiguous DMA memory if at least one of the following
962 * conditions is true:
963 * a) The ETR cannot use Scatter-Gather.
964 * b) we have a backing IOMMU
965 * c) The requested memory size is smaller (< 1M).
966 *
967 * Fallback to available mechanisms.
968 *
969 */
970 if (rc && !pages && etr_can_use_flat_mode(&buf_hw, size))
971 rc = tmc_etr_mode_alloc_buf(ETR_MODE_FLAT, drvdata,
972 etr_buf, node, pages);
973 if (rc && buf_hw.has_etr_sg)
974 rc = tmc_etr_mode_alloc_buf(ETR_MODE_ETR_SG, drvdata,
975 etr_buf, node, pages);
976 if (rc && buf_hw.has_catu)
977 rc = tmc_etr_mode_alloc_buf(ETR_MODE_CATU, drvdata,
978 etr_buf, node, pages);
979 if (rc) {
980 kfree(etr_buf);
981 return ERR_PTR(rc);
982 }
983
984 refcount_set(&etr_buf->refcount, 1);
985 dev_dbg(dev, "allocated buffer of size %ldKB in mode %d\n",
986 (unsigned long)size >> 10, etr_buf->mode);
987 return etr_buf;
988 }
989
tmc_free_etr_buf(struct etr_buf * etr_buf)990 static void tmc_free_etr_buf(struct etr_buf *etr_buf)
991 {
992 WARN_ON(!etr_buf->ops || !etr_buf->ops->free);
993 etr_buf->ops->free(etr_buf);
994 kfree(etr_buf);
995 }
996
997 /*
998 * tmc_etr_buf_get_data: Get the pointer the trace data at @offset
999 * with a maximum of @len bytes.
1000 * Returns: The size of the linear data available @pos, with *bufpp
1001 * updated to point to the buffer.
1002 */
tmc_etr_buf_get_data(struct etr_buf * etr_buf,u64 offset,size_t len,char ** bufpp)1003 static ssize_t tmc_etr_buf_get_data(struct etr_buf *etr_buf,
1004 u64 offset, size_t len, char **bufpp)
1005 {
1006 /* Adjust the length to limit this transaction to end of buffer */
1007 len = (len < (etr_buf->size - offset)) ? len : etr_buf->size - offset;
1008
1009 return etr_buf->ops->get_data(etr_buf, (u64)offset, len, bufpp);
1010 }
1011
1012 static inline s64
tmc_etr_buf_insert_barrier_packet(struct etr_buf * etr_buf,u64 offset)1013 tmc_etr_buf_insert_barrier_packet(struct etr_buf *etr_buf, u64 offset)
1014 {
1015 ssize_t len;
1016 char *bufp;
1017
1018 len = tmc_etr_buf_get_data(etr_buf, offset,
1019 CORESIGHT_BARRIER_PKT_SIZE, &bufp);
1020 if (WARN_ON(len < 0 || len < CORESIGHT_BARRIER_PKT_SIZE))
1021 return -EINVAL;
1022 coresight_insert_barrier_packet(bufp);
1023 return offset + CORESIGHT_BARRIER_PKT_SIZE;
1024 }
1025
1026 /*
1027 * tmc_sync_etr_buf: Sync the trace buffer availability with drvdata.
1028 * Makes sure the trace data is synced to the memory for consumption.
1029 * @etr_buf->offset will hold the offset to the beginning of the trace data
1030 * within the buffer, with @etr_buf->len bytes to consume.
1031 */
tmc_sync_etr_buf(struct tmc_drvdata * drvdata)1032 static void tmc_sync_etr_buf(struct tmc_drvdata *drvdata)
1033 {
1034 struct etr_buf *etr_buf = drvdata->etr_buf;
1035 u64 rrp, rwp;
1036 u32 status;
1037
1038 rrp = tmc_read_rrp(drvdata);
1039 rwp = tmc_read_rwp(drvdata);
1040 status = readl_relaxed(drvdata->base + TMC_STS);
1041
1042 /*
1043 * If there were memory errors in the session, truncate the
1044 * buffer.
1045 */
1046 if (WARN_ON_ONCE(status & TMC_STS_MEMERR)) {
1047 dev_dbg(&drvdata->csdev->dev,
1048 "tmc memory error detected, truncating buffer\n");
1049 etr_buf->len = 0;
1050 etr_buf->full = false;
1051 return;
1052 }
1053
1054 etr_buf->full = !!(status & TMC_STS_FULL);
1055
1056 WARN_ON(!etr_buf->ops || !etr_buf->ops->sync);
1057
1058 etr_buf->ops->sync(etr_buf, rrp, rwp);
1059 }
1060
__tmc_etr_enable_hw(struct tmc_drvdata * drvdata)1061 static int __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
1062 {
1063 u32 axictl, sts, ffcr;
1064 struct etr_buf *etr_buf = drvdata->etr_buf;
1065 int rc = 0;
1066
1067 CS_UNLOCK(drvdata->base);
1068
1069 /* Wait for TMCSReady bit to be set */
1070 rc = tmc_wait_for_tmcready(drvdata);
1071 if (rc) {
1072 dev_err(&drvdata->csdev->dev,
1073 "Failed to enable : TMC not ready\n");
1074 CS_LOCK(drvdata->base);
1075 return rc;
1076 }
1077
1078 writel_relaxed(etr_buf->size / 4, drvdata->base + TMC_RSZ);
1079 writel_relaxed(TMC_MODE_CIRCULAR_BUFFER, drvdata->base + TMC_MODE);
1080
1081 axictl = readl_relaxed(drvdata->base + TMC_AXICTL);
1082 axictl &= ~TMC_AXICTL_CLEAR_MASK;
1083 axictl |= TMC_AXICTL_PROT_CTL_B1;
1084 axictl |= TMC_AXICTL_WR_BURST(drvdata->max_burst_size);
1085 axictl |= TMC_AXICTL_AXCACHE_OS;
1086
1087 if (tmc_etr_has_cap(drvdata, TMC_ETR_AXI_ARCACHE)) {
1088 axictl &= ~TMC_AXICTL_ARCACHE_MASK;
1089 axictl |= TMC_AXICTL_ARCACHE_OS;
1090 }
1091
1092 if (etr_buf->mode == ETR_MODE_ETR_SG)
1093 axictl |= TMC_AXICTL_SCT_GAT_MODE;
1094
1095 writel_relaxed(axictl, drvdata->base + TMC_AXICTL);
1096 tmc_write_dba(drvdata, etr_buf->hwaddr);
1097 /*
1098 * If the TMC pointers must be programmed before the session,
1099 * we have to set it properly (i.e, RRP/RWP to base address and
1100 * STS to "not full").
1101 */
1102 if (tmc_etr_has_cap(drvdata, TMC_ETR_SAVE_RESTORE)) {
1103 tmc_write_rrp(drvdata, etr_buf->hwaddr);
1104 tmc_write_rwp(drvdata, etr_buf->hwaddr);
1105 sts = readl_relaxed(drvdata->base + TMC_STS) & ~TMC_STS_FULL;
1106 writel_relaxed(sts, drvdata->base + TMC_STS);
1107 }
1108
1109 ffcr = TMC_FFCR_EN_FMT | TMC_FFCR_EN_TI | TMC_FFCR_FON_FLIN |
1110 TMC_FFCR_FON_TRIG_EVT | TMC_FFCR_TRIGON_TRIGIN;
1111 if (drvdata->stop_on_flush)
1112 ffcr |= TMC_FFCR_STOP_ON_FLUSH;
1113 writel_relaxed(ffcr, drvdata->base + TMC_FFCR);
1114
1115 writel_relaxed(drvdata->trigger_cntr, drvdata->base + TMC_TRG);
1116 tmc_enable_hw(drvdata);
1117
1118 CS_LOCK(drvdata->base);
1119 return rc;
1120 }
1121
tmc_etr_enable_hw(struct tmc_drvdata * drvdata,struct etr_buf * etr_buf)1122 static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
1123 struct etr_buf *etr_buf)
1124 {
1125 int rc;
1126
1127 /* Callers should provide an appropriate buffer for use */
1128 if (WARN_ON(!etr_buf))
1129 return -EINVAL;
1130
1131 if ((etr_buf->mode == ETR_MODE_ETR_SG) &&
1132 WARN_ON(!tmc_etr_has_cap(drvdata, TMC_ETR_SG)))
1133 return -EINVAL;
1134
1135 if (WARN_ON(drvdata->etr_buf))
1136 return -EBUSY;
1137
1138 rc = coresight_claim_device(drvdata->csdev);
1139 if (!rc) {
1140 drvdata->etr_buf = etr_buf;
1141 rc = __tmc_etr_enable_hw(drvdata);
1142 if (rc) {
1143 drvdata->etr_buf = NULL;
1144 coresight_disclaim_device(drvdata->csdev);
1145 }
1146 }
1147
1148 return rc;
1149 }
1150
1151 /*
1152 * Return the available trace data in the buffer (starts at etr_buf->offset,
1153 * limited by etr_buf->len) from @pos, with a maximum limit of @len,
1154 * also updating the @bufpp on where to find it. Since the trace data
1155 * starts at anywhere in the buffer, depending on the RRP, we adjust the
1156 * @len returned to handle buffer wrapping around.
1157 *
1158 * We are protected here by drvdata->reading != 0, which ensures the
1159 * sysfs_buf stays alive.
1160 */
tmc_etr_get_sysfs_trace(struct tmc_drvdata * drvdata,loff_t pos,size_t len,char ** bufpp)1161 ssize_t tmc_etr_get_sysfs_trace(struct tmc_drvdata *drvdata,
1162 loff_t pos, size_t len, char **bufpp)
1163 {
1164 s64 offset;
1165 ssize_t actual = len;
1166 struct etr_buf *etr_buf = drvdata->sysfs_buf;
1167
1168 if (pos + actual > etr_buf->len)
1169 actual = etr_buf->len - pos;
1170 if (actual <= 0)
1171 return actual;
1172
1173 /* Compute the offset from which we read the data */
1174 offset = etr_buf->offset + pos;
1175 if (offset >= etr_buf->size)
1176 offset -= etr_buf->size;
1177 return tmc_etr_buf_get_data(etr_buf, offset, actual, bufpp);
1178 }
1179
1180 static struct etr_buf *
tmc_etr_setup_sysfs_buf(struct tmc_drvdata * drvdata)1181 tmc_etr_setup_sysfs_buf(struct tmc_drvdata *drvdata)
1182 {
1183 return tmc_alloc_etr_buf(drvdata, drvdata->size,
1184 0, cpu_to_node(0), NULL);
1185 }
1186
1187 static void
tmc_etr_free_sysfs_buf(struct etr_buf * buf)1188 tmc_etr_free_sysfs_buf(struct etr_buf *buf)
1189 {
1190 if (buf)
1191 tmc_free_etr_buf(buf);
1192 }
1193
tmc_etr_sync_sysfs_buf(struct tmc_drvdata * drvdata)1194 static void tmc_etr_sync_sysfs_buf(struct tmc_drvdata *drvdata)
1195 {
1196 struct etr_buf *etr_buf = drvdata->etr_buf;
1197
1198 if (WARN_ON(drvdata->sysfs_buf != etr_buf)) {
1199 tmc_etr_free_sysfs_buf(drvdata->sysfs_buf);
1200 drvdata->sysfs_buf = NULL;
1201 } else {
1202 tmc_sync_etr_buf(drvdata);
1203 /*
1204 * Insert barrier packets at the beginning, if there was
1205 * an overflow.
1206 */
1207 if (etr_buf->full)
1208 tmc_etr_buf_insert_barrier_packet(etr_buf,
1209 etr_buf->offset);
1210 }
1211 }
1212
__tmc_etr_disable_hw(struct tmc_drvdata * drvdata)1213 static void __tmc_etr_disable_hw(struct tmc_drvdata *drvdata)
1214 {
1215 CS_UNLOCK(drvdata->base);
1216
1217 tmc_flush_and_stop(drvdata);
1218 /*
1219 * When operating in sysFS mode the content of the buffer needs to be
1220 * read before the TMC is disabled.
1221 */
1222 if (coresight_get_mode(drvdata->csdev) == CS_MODE_SYSFS)
1223 tmc_etr_sync_sysfs_buf(drvdata);
1224
1225 tmc_disable_hw(drvdata);
1226
1227 CS_LOCK(drvdata->base);
1228
1229 }
1230
tmc_etr_disable_hw(struct tmc_drvdata * drvdata)1231 void tmc_etr_disable_hw(struct tmc_drvdata *drvdata)
1232 {
1233 __tmc_etr_disable_hw(drvdata);
1234 coresight_disclaim_device(drvdata->csdev);
1235 /* Reset the ETR buf used by hardware */
1236 drvdata->etr_buf = NULL;
1237 }
1238
tmc_etr_get_sysfs_buffer(struct coresight_device * csdev)1239 static struct etr_buf *tmc_etr_get_sysfs_buffer(struct coresight_device *csdev)
1240 {
1241 int ret = 0;
1242 unsigned long flags;
1243 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1244 struct etr_buf *sysfs_buf = NULL, *new_buf = NULL, *free_buf = NULL;
1245
1246 /*
1247 * If we are enabling the ETR from disabled state, we need to make
1248 * sure we have a buffer with the right size. The etr_buf is not reset
1249 * immediately after we stop the tracing in SYSFS mode as we wait for
1250 * the user to collect the data. We may be able to reuse the existing
1251 * buffer, provided the size matches. Any allocation has to be done
1252 * with the lock released.
1253 */
1254 raw_spin_lock_irqsave(&drvdata->spinlock, flags);
1255 sysfs_buf = READ_ONCE(drvdata->sysfs_buf);
1256 if (!sysfs_buf || (sysfs_buf->size != drvdata->size)) {
1257 raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
1258
1259 /* Allocate memory with the locks released */
1260 free_buf = new_buf = tmc_etr_setup_sysfs_buf(drvdata);
1261 if (IS_ERR(new_buf))
1262 return new_buf;
1263
1264 /* Let's try again */
1265 raw_spin_lock_irqsave(&drvdata->spinlock, flags);
1266 }
1267
1268 if (drvdata->reading || coresight_get_mode(csdev) == CS_MODE_PERF) {
1269 ret = -EBUSY;
1270 goto out;
1271 }
1272
1273 /*
1274 * If we don't have a buffer or it doesn't match the requested size,
1275 * use the buffer allocated above. Otherwise reuse the existing buffer.
1276 */
1277 sysfs_buf = READ_ONCE(drvdata->sysfs_buf);
1278 if (!sysfs_buf || (new_buf && sysfs_buf->size != new_buf->size)) {
1279 free_buf = sysfs_buf;
1280 drvdata->sysfs_buf = new_buf;
1281 }
1282
1283 out:
1284 raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
1285
1286 /* Free memory outside the spinlock if need be */
1287 if (free_buf)
1288 tmc_etr_free_sysfs_buf(free_buf);
1289 return ret ? ERR_PTR(ret) : drvdata->sysfs_buf;
1290 }
1291
tmc_enable_etr_sink_sysfs(struct coresight_device * csdev)1292 static int tmc_enable_etr_sink_sysfs(struct coresight_device *csdev)
1293 {
1294 int ret = 0;
1295 unsigned long flags;
1296 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1297 struct etr_buf *sysfs_buf = tmc_etr_get_sysfs_buffer(csdev);
1298
1299 if (IS_ERR(sysfs_buf))
1300 return PTR_ERR(sysfs_buf);
1301
1302 raw_spin_lock_irqsave(&drvdata->spinlock, flags);
1303
1304 /*
1305 * In sysFS mode we can have multiple writers per sink. Since this
1306 * sink is already enabled no memory is needed and the HW need not be
1307 * touched, even if the buffer size has changed.
1308 */
1309 if (coresight_get_mode(csdev) == CS_MODE_SYSFS) {
1310 csdev->refcnt++;
1311 goto out;
1312 }
1313
1314 ret = tmc_etr_enable_hw(drvdata, sysfs_buf);
1315 if (!ret) {
1316 coresight_set_mode(csdev, CS_MODE_SYSFS);
1317 csdev->refcnt++;
1318 }
1319
1320 out:
1321 raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
1322
1323 if (!ret)
1324 dev_dbg(&csdev->dev, "TMC-ETR enabled\n");
1325
1326 return ret;
1327 }
1328
tmc_etr_get_buffer(struct coresight_device * csdev,enum cs_mode mode,void * data)1329 struct etr_buf *tmc_etr_get_buffer(struct coresight_device *csdev,
1330 enum cs_mode mode, void *data)
1331 {
1332 struct perf_output_handle *handle = data;
1333 struct etr_perf_buffer *etr_perf;
1334
1335 switch (mode) {
1336 case CS_MODE_SYSFS:
1337 return tmc_etr_get_sysfs_buffer(csdev);
1338 case CS_MODE_PERF:
1339 etr_perf = etm_perf_sink_config(handle);
1340 if (WARN_ON(!etr_perf || !etr_perf->etr_buf))
1341 return ERR_PTR(-EINVAL);
1342 return etr_perf->etr_buf;
1343 default:
1344 return ERR_PTR(-EINVAL);
1345 }
1346 }
1347 EXPORT_SYMBOL_GPL(tmc_etr_get_buffer);
1348
1349 /*
1350 * alloc_etr_buf: Allocate ETR buffer for use by perf.
1351 * The size of the hardware buffer is dependent on the size configured
1352 * via sysfs and the perf ring buffer size. We prefer to allocate the
1353 * largest possible size, scaling down the size by half until it
1354 * reaches a minimum limit (1M), beyond which we give up.
1355 */
1356 static struct etr_buf *
alloc_etr_buf(struct tmc_drvdata * drvdata,struct perf_event * event,int nr_pages,void ** pages,bool snapshot)1357 alloc_etr_buf(struct tmc_drvdata *drvdata, struct perf_event *event,
1358 int nr_pages, void **pages, bool snapshot)
1359 {
1360 int node;
1361 struct etr_buf *etr_buf;
1362 unsigned long size;
1363
1364 node = (event->cpu == -1) ? NUMA_NO_NODE : cpu_to_node(event->cpu);
1365 /*
1366 * Try to match the perf ring buffer size if it is larger
1367 * than the size requested via sysfs.
1368 */
1369 if ((nr_pages << PAGE_SHIFT) > drvdata->size) {
1370 etr_buf = tmc_alloc_etr_buf(drvdata, ((ssize_t)nr_pages << PAGE_SHIFT),
1371 0, node, NULL);
1372 if (!IS_ERR(etr_buf))
1373 goto done;
1374 }
1375
1376 /*
1377 * Else switch to configured size for this ETR
1378 * and scale down until we hit the minimum limit.
1379 */
1380 size = drvdata->size;
1381 do {
1382 etr_buf = tmc_alloc_etr_buf(drvdata, size, 0, node, NULL);
1383 if (!IS_ERR(etr_buf))
1384 goto done;
1385 size /= 2;
1386 } while (size >= TMC_ETR_PERF_MIN_BUF_SIZE);
1387
1388 return ERR_PTR(-ENOMEM);
1389
1390 done:
1391 return etr_buf;
1392 }
1393
1394 static struct etr_buf *
get_perf_etr_buf_cpu_wide(struct tmc_drvdata * drvdata,struct perf_event * event,int nr_pages,void ** pages,bool snapshot)1395 get_perf_etr_buf_cpu_wide(struct tmc_drvdata *drvdata,
1396 struct perf_event *event, int nr_pages,
1397 void **pages, bool snapshot)
1398 {
1399 int ret;
1400 pid_t pid = task_pid_nr(event->owner);
1401 struct etr_buf *etr_buf;
1402
1403 retry:
1404 /*
1405 * An etr_perf_buffer is associated with an event and holds a reference
1406 * to the AUX ring buffer that was created for that event. In CPU-wide
1407 * N:1 mode multiple events (one per CPU), each with its own AUX ring
1408 * buffer, share a sink. As such an etr_perf_buffer is created for each
1409 * event but a single etr_buf associated with the ETR is shared between
1410 * them. The last event in a trace session will copy the content of the
1411 * etr_buf to its AUX ring buffer. Ring buffer associated to other
1412 * events are simply not used an freed as events are destoyed. We still
1413 * need to allocate a ring buffer for each event since we don't know
1414 * which event will be last.
1415 */
1416
1417 /*
1418 * The first thing to do here is check if an etr_buf has already been
1419 * allocated for this session. If so it is shared with this event,
1420 * otherwise it is created.
1421 */
1422 mutex_lock(&drvdata->idr_mutex);
1423 etr_buf = idr_find(&drvdata->idr, pid);
1424 if (etr_buf) {
1425 refcount_inc(&etr_buf->refcount);
1426 mutex_unlock(&drvdata->idr_mutex);
1427 return etr_buf;
1428 }
1429
1430 /* If we made it here no buffer has been allocated, do so now. */
1431 mutex_unlock(&drvdata->idr_mutex);
1432
1433 etr_buf = alloc_etr_buf(drvdata, event, nr_pages, pages, snapshot);
1434 if (IS_ERR(etr_buf))
1435 return etr_buf;
1436
1437 /* Now that we have a buffer, add it to the IDR. */
1438 mutex_lock(&drvdata->idr_mutex);
1439 ret = idr_alloc(&drvdata->idr, etr_buf, pid, pid + 1, GFP_KERNEL);
1440 mutex_unlock(&drvdata->idr_mutex);
1441
1442 /* Another event with this session ID has allocated this buffer. */
1443 if (ret == -ENOSPC) {
1444 tmc_free_etr_buf(etr_buf);
1445 goto retry;
1446 }
1447
1448 /* The IDR can't allocate room for a new session, abandon ship. */
1449 if (ret == -ENOMEM) {
1450 tmc_free_etr_buf(etr_buf);
1451 return ERR_PTR(ret);
1452 }
1453
1454
1455 return etr_buf;
1456 }
1457
1458 static struct etr_buf *
get_perf_etr_buf_per_thread(struct tmc_drvdata * drvdata,struct perf_event * event,int nr_pages,void ** pages,bool snapshot)1459 get_perf_etr_buf_per_thread(struct tmc_drvdata *drvdata,
1460 struct perf_event *event, int nr_pages,
1461 void **pages, bool snapshot)
1462 {
1463 /*
1464 * In per-thread mode the etr_buf isn't shared, so just go ahead
1465 * with memory allocation.
1466 */
1467 return alloc_etr_buf(drvdata, event, nr_pages, pages, snapshot);
1468 }
1469
1470 static struct etr_buf *
get_perf_etr_buf(struct tmc_drvdata * drvdata,struct perf_event * event,int nr_pages,void ** pages,bool snapshot)1471 get_perf_etr_buf(struct tmc_drvdata *drvdata, struct perf_event *event,
1472 int nr_pages, void **pages, bool snapshot)
1473 {
1474 if (event->cpu == -1)
1475 return get_perf_etr_buf_per_thread(drvdata, event, nr_pages,
1476 pages, snapshot);
1477
1478 return get_perf_etr_buf_cpu_wide(drvdata, event, nr_pages,
1479 pages, snapshot);
1480 }
1481
1482 static struct etr_perf_buffer *
tmc_etr_setup_perf_buf(struct tmc_drvdata * drvdata,struct perf_event * event,int nr_pages,void ** pages,bool snapshot)1483 tmc_etr_setup_perf_buf(struct tmc_drvdata *drvdata, struct perf_event *event,
1484 int nr_pages, void **pages, bool snapshot)
1485 {
1486 int node;
1487 struct etr_buf *etr_buf;
1488 struct etr_perf_buffer *etr_perf;
1489
1490 node = (event->cpu == -1) ? NUMA_NO_NODE : cpu_to_node(event->cpu);
1491
1492 etr_perf = kzalloc_node(sizeof(*etr_perf), GFP_KERNEL, node);
1493 if (!etr_perf)
1494 return ERR_PTR(-ENOMEM);
1495
1496 etr_buf = get_perf_etr_buf(drvdata, event, nr_pages, pages, snapshot);
1497 if (!IS_ERR(etr_buf))
1498 goto done;
1499
1500 kfree(etr_perf);
1501 return ERR_PTR(-ENOMEM);
1502
1503 done:
1504 /*
1505 * Keep a reference to the ETR this buffer has been allocated for
1506 * in order to have access to the IDR in tmc_free_etr_buffer().
1507 */
1508 etr_perf->drvdata = drvdata;
1509 etr_perf->etr_buf = etr_buf;
1510
1511 return etr_perf;
1512 }
1513
1514
tmc_alloc_etr_buffer(struct coresight_device * csdev,struct perf_event * event,void ** pages,int nr_pages,bool snapshot)1515 static void *tmc_alloc_etr_buffer(struct coresight_device *csdev,
1516 struct perf_event *event, void **pages,
1517 int nr_pages, bool snapshot)
1518 {
1519 struct etr_perf_buffer *etr_perf;
1520 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1521
1522 etr_perf = tmc_etr_setup_perf_buf(drvdata, event,
1523 nr_pages, pages, snapshot);
1524 if (IS_ERR(etr_perf)) {
1525 dev_dbg(&csdev->dev, "Unable to allocate ETR buffer\n");
1526 return NULL;
1527 }
1528
1529 etr_perf->pid = task_pid_nr(event->owner);
1530 etr_perf->snapshot = snapshot;
1531 etr_perf->nr_pages = nr_pages;
1532 etr_perf->pages = pages;
1533
1534 return etr_perf;
1535 }
1536
tmc_free_etr_buffer(void * config)1537 static void tmc_free_etr_buffer(void *config)
1538 {
1539 struct etr_perf_buffer *etr_perf = config;
1540 struct tmc_drvdata *drvdata = etr_perf->drvdata;
1541 struct etr_buf *buf, *etr_buf = etr_perf->etr_buf;
1542
1543 if (!etr_buf)
1544 goto free_etr_perf_buffer;
1545
1546 mutex_lock(&drvdata->idr_mutex);
1547 /* If we are not the last one to use the buffer, don't touch it. */
1548 if (!refcount_dec_and_test(&etr_buf->refcount)) {
1549 mutex_unlock(&drvdata->idr_mutex);
1550 goto free_etr_perf_buffer;
1551 }
1552
1553 /* We are the last one, remove from the IDR and free the buffer. */
1554 buf = idr_remove(&drvdata->idr, etr_perf->pid);
1555 mutex_unlock(&drvdata->idr_mutex);
1556
1557 /*
1558 * Something went very wrong if the buffer associated with this ID
1559 * is not the same in the IDR. Leak to avoid use after free.
1560 */
1561 if (buf && WARN_ON(buf != etr_buf))
1562 goto free_etr_perf_buffer;
1563
1564 tmc_free_etr_buf(etr_perf->etr_buf);
1565
1566 free_etr_perf_buffer:
1567 kfree(etr_perf);
1568 }
1569
1570 /*
1571 * tmc_etr_sync_perf_buffer: Copy the actual trace data from the hardware
1572 * buffer to the perf ring buffer.
1573 */
tmc_etr_sync_perf_buffer(struct etr_perf_buffer * etr_perf,unsigned long head,unsigned long src_offset,unsigned long to_copy)1574 static void tmc_etr_sync_perf_buffer(struct etr_perf_buffer *etr_perf,
1575 unsigned long head,
1576 unsigned long src_offset,
1577 unsigned long to_copy)
1578 {
1579 long bytes;
1580 long pg_idx, pg_offset;
1581 char **dst_pages, *src_buf;
1582 struct etr_buf *etr_buf = etr_perf->etr_buf;
1583
1584 head = PERF_IDX2OFF(head, etr_perf);
1585 pg_idx = head >> PAGE_SHIFT;
1586 pg_offset = head & (PAGE_SIZE - 1);
1587 dst_pages = (char **)etr_perf->pages;
1588
1589 while (to_copy > 0) {
1590 /*
1591 * In one iteration, we can copy minimum of :
1592 * 1) what is available in the source buffer,
1593 * 2) what is available in the source buffer, before it
1594 * wraps around.
1595 * 3) what is available in the destination page.
1596 * in one iteration.
1597 */
1598 if (src_offset >= etr_buf->size)
1599 src_offset -= etr_buf->size;
1600 bytes = tmc_etr_buf_get_data(etr_buf, src_offset, to_copy,
1601 &src_buf);
1602 if (WARN_ON_ONCE(bytes <= 0))
1603 break;
1604 bytes = min(bytes, (long)(PAGE_SIZE - pg_offset));
1605
1606 memcpy(dst_pages[pg_idx] + pg_offset, src_buf, bytes);
1607
1608 to_copy -= bytes;
1609
1610 /* Move destination pointers */
1611 pg_offset += bytes;
1612 if (pg_offset == PAGE_SIZE) {
1613 pg_offset = 0;
1614 if (++pg_idx == etr_perf->nr_pages)
1615 pg_idx = 0;
1616 }
1617
1618 /* Move source pointers */
1619 src_offset += bytes;
1620 }
1621 }
1622
1623 /*
1624 * tmc_update_etr_buffer : Update the perf ring buffer with the
1625 * available trace data. We use software double buffering at the moment.
1626 *
1627 * TODO: Add support for reusing the perf ring buffer.
1628 */
1629 static unsigned long
tmc_update_etr_buffer(struct coresight_device * csdev,struct perf_output_handle * handle,void * config)1630 tmc_update_etr_buffer(struct coresight_device *csdev,
1631 struct perf_output_handle *handle,
1632 void *config)
1633 {
1634 bool lost = false;
1635 unsigned long flags, offset, size = 0;
1636 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1637 struct etr_perf_buffer *etr_perf = config;
1638 struct etr_buf *etr_buf = etr_perf->etr_buf;
1639
1640 raw_spin_lock_irqsave(&drvdata->spinlock, flags);
1641
1642 /* Don't do anything if another tracer is using this sink */
1643 if (csdev->refcnt != 1) {
1644 raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
1645 goto out;
1646 }
1647
1648 if (WARN_ON(drvdata->perf_buf != etr_buf)) {
1649 lost = true;
1650 raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
1651 goto out;
1652 }
1653
1654 CS_UNLOCK(drvdata->base);
1655
1656 tmc_flush_and_stop(drvdata);
1657 tmc_sync_etr_buf(drvdata);
1658
1659 CS_LOCK(drvdata->base);
1660 raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
1661
1662 lost = etr_buf->full;
1663 offset = etr_buf->offset;
1664 size = etr_buf->len;
1665
1666 /*
1667 * The ETR buffer may be bigger than the space available in the
1668 * perf ring buffer (handle->size). If so advance the offset so that we
1669 * get the latest trace data. In snapshot mode none of that matters
1670 * since we are expected to clobber stale data in favour of the latest
1671 * traces.
1672 */
1673 if (!etr_perf->snapshot && size > handle->size) {
1674 u32 mask = tmc_get_memwidth_mask(drvdata);
1675
1676 /*
1677 * Make sure the new size is aligned in accordance with the
1678 * requirement explained in function tmc_get_memwidth_mask().
1679 */
1680 size = handle->size & mask;
1681 offset = etr_buf->offset + etr_buf->len - size;
1682
1683 if (offset >= etr_buf->size)
1684 offset -= etr_buf->size;
1685 lost = true;
1686 }
1687
1688 /* Insert barrier packets at the beginning, if there was an overflow */
1689 if (lost)
1690 tmc_etr_buf_insert_barrier_packet(etr_buf, offset);
1691 tmc_etr_sync_perf_buffer(etr_perf, handle->head, offset, size);
1692
1693 /*
1694 * In snapshot mode we simply increment the head by the number of byte
1695 * that were written. User space will figure out how many bytes to get
1696 * from the AUX buffer based on the position of the head.
1697 */
1698 if (etr_perf->snapshot)
1699 handle->head += size;
1700
1701 /*
1702 * Ensure that the AUX trace data is visible before the aux_head
1703 * is updated via perf_aux_output_end(), as expected by the
1704 * perf ring buffer.
1705 */
1706 smp_wmb();
1707
1708 out:
1709 /*
1710 * Don't set the TRUNCATED flag in snapshot mode because 1) the
1711 * captured buffer is expected to be truncated and 2) a full buffer
1712 * prevents the event from being re-enabled by the perf core,
1713 * resulting in stale data being send to user space.
1714 */
1715 if (!etr_perf->snapshot && lost)
1716 perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED);
1717 return size;
1718 }
1719
tmc_enable_etr_sink_perf(struct coresight_device * csdev,void * data)1720 static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data)
1721 {
1722 int rc = 0;
1723 pid_t pid;
1724 unsigned long flags;
1725 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1726 struct perf_output_handle *handle = data;
1727 struct etr_perf_buffer *etr_perf = etm_perf_sink_config(handle);
1728
1729 raw_spin_lock_irqsave(&drvdata->spinlock, flags);
1730 /* Don't use this sink if it is already claimed by sysFS */
1731 if (coresight_get_mode(csdev) == CS_MODE_SYSFS) {
1732 rc = -EBUSY;
1733 goto unlock_out;
1734 }
1735
1736 if (WARN_ON(!etr_perf || !etr_perf->etr_buf)) {
1737 rc = -EINVAL;
1738 goto unlock_out;
1739 }
1740
1741 /* Get a handle on the pid of the session owner */
1742 pid = etr_perf->pid;
1743
1744 /* Do not proceed if this device is associated with another session */
1745 if (drvdata->pid != -1 && drvdata->pid != pid) {
1746 rc = -EBUSY;
1747 goto unlock_out;
1748 }
1749
1750 /*
1751 * No HW configuration is needed if the sink is already in
1752 * use for this session.
1753 */
1754 if (drvdata->pid == pid) {
1755 csdev->refcnt++;
1756 goto unlock_out;
1757 }
1758
1759 rc = tmc_etr_enable_hw(drvdata, etr_perf->etr_buf);
1760 if (!rc) {
1761 /* Associate with monitored process. */
1762 drvdata->pid = pid;
1763 coresight_set_mode(csdev, CS_MODE_PERF);
1764 drvdata->perf_buf = etr_perf->etr_buf;
1765 csdev->refcnt++;
1766 }
1767
1768 unlock_out:
1769 raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
1770 return rc;
1771 }
1772
tmc_enable_etr_sink(struct coresight_device * csdev,enum cs_mode mode,void * data)1773 static int tmc_enable_etr_sink(struct coresight_device *csdev,
1774 enum cs_mode mode, void *data)
1775 {
1776 switch (mode) {
1777 case CS_MODE_SYSFS:
1778 return tmc_enable_etr_sink_sysfs(csdev);
1779 case CS_MODE_PERF:
1780 return tmc_enable_etr_sink_perf(csdev, data);
1781 default:
1782 return -EINVAL;
1783 }
1784 }
1785
tmc_disable_etr_sink(struct coresight_device * csdev)1786 static int tmc_disable_etr_sink(struct coresight_device *csdev)
1787 {
1788 unsigned long flags;
1789 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1790
1791 raw_spin_lock_irqsave(&drvdata->spinlock, flags);
1792
1793 if (drvdata->reading) {
1794 raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
1795 return -EBUSY;
1796 }
1797
1798 csdev->refcnt--;
1799 if (csdev->refcnt) {
1800 raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
1801 return -EBUSY;
1802 }
1803
1804 /* Complain if we (somehow) got out of sync */
1805 WARN_ON_ONCE(coresight_get_mode(csdev) == CS_MODE_DISABLED);
1806 tmc_etr_disable_hw(drvdata);
1807 /* Dissociate from monitored process. */
1808 drvdata->pid = -1;
1809 coresight_set_mode(csdev, CS_MODE_DISABLED);
1810 /* Reset perf specific data */
1811 drvdata->perf_buf = NULL;
1812
1813 raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
1814
1815 dev_dbg(&csdev->dev, "TMC-ETR disabled\n");
1816 return 0;
1817 }
1818
tmc_panic_sync_etr(struct coresight_device * csdev)1819 static int tmc_panic_sync_etr(struct coresight_device *csdev)
1820 {
1821 u32 val;
1822 struct tmc_crash_metadata *mdata;
1823 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1824
1825 mdata = (struct tmc_crash_metadata *)drvdata->crash_mdata.vaddr;
1826
1827 if (!drvdata->etr_buf)
1828 return 0;
1829
1830 /* Being in RESRV mode implies valid reserved memory as well */
1831 if (drvdata->etr_buf->mode != ETR_MODE_RESRV)
1832 return 0;
1833
1834 if (!tmc_has_crash_mdata_buffer(drvdata))
1835 return 0;
1836
1837 CS_UNLOCK(drvdata->base);
1838
1839 /* Proceed only if ETR is enabled */
1840 val = readl(drvdata->base + TMC_CTL);
1841 if (!(val & TMC_CTL_CAPT_EN))
1842 goto out;
1843
1844 val = readl(drvdata->base + TMC_FFSR);
1845 /* Do manual flush and stop only if its not auto-stopped */
1846 if (!(val & TMC_FFSR_FT_STOPPED)) {
1847 dev_dbg(&csdev->dev,
1848 "%s: Triggering manual flush\n", __func__);
1849 tmc_flush_and_stop(drvdata);
1850 } else
1851 tmc_wait_for_tmcready(drvdata);
1852
1853 /* Sync registers from hardware to metadata region */
1854 mdata->tmc_ram_size = readl(drvdata->base + TMC_RSZ);
1855 mdata->tmc_sts = readl(drvdata->base + TMC_STS);
1856 mdata->tmc_mode = readl(drvdata->base + TMC_MODE);
1857 mdata->tmc_ffcr = readl(drvdata->base + TMC_FFCR);
1858 mdata->tmc_ffsr = readl(drvdata->base + TMC_FFSR);
1859 mdata->tmc_rrp = tmc_read_rrp(drvdata);
1860 mdata->tmc_rwp = tmc_read_rwp(drvdata);
1861 mdata->tmc_dba = tmc_read_dba(drvdata);
1862 mdata->trace_paddr = drvdata->resrv_buf.paddr;
1863 mdata->version = CS_CRASHDATA_VERSION;
1864
1865 /*
1866 * Make sure all previous writes are ordered,
1867 * before we mark valid
1868 */
1869 dmb(sy);
1870 mdata->valid = true;
1871 /*
1872 * Below order need to maintained, since crc of metadata
1873 * is dependent on first
1874 */
1875 mdata->crc32_tdata = find_crash_tracedata_crc(drvdata, mdata);
1876 mdata->crc32_mdata = find_crash_metadata_crc(mdata);
1877
1878 tmc_disable_hw(drvdata);
1879
1880 dev_dbg(&csdev->dev, "%s: success\n", __func__);
1881 out:
1882 CS_UNLOCK(drvdata->base);
1883
1884 return 0;
1885 }
1886
1887 static const struct coresight_ops_sink tmc_etr_sink_ops = {
1888 .enable = tmc_enable_etr_sink,
1889 .disable = tmc_disable_etr_sink,
1890 .alloc_buffer = tmc_alloc_etr_buffer,
1891 .update_buffer = tmc_update_etr_buffer,
1892 .free_buffer = tmc_free_etr_buffer,
1893 };
1894
1895 static const struct coresight_ops_panic tmc_etr_sync_ops = {
1896 .sync = tmc_panic_sync_etr,
1897 };
1898
1899 const struct coresight_ops tmc_etr_cs_ops = {
1900 .sink_ops = &tmc_etr_sink_ops,
1901 .panic_ops = &tmc_etr_sync_ops,
1902 };
1903
tmc_read_prepare_etr(struct tmc_drvdata * drvdata)1904 int tmc_read_prepare_etr(struct tmc_drvdata *drvdata)
1905 {
1906 int ret = 0;
1907 unsigned long flags;
1908
1909 /* config types are set a boot time and never change */
1910 if (WARN_ON_ONCE(drvdata->config_type != TMC_CONFIG_TYPE_ETR))
1911 return -EINVAL;
1912
1913 raw_spin_lock_irqsave(&drvdata->spinlock, flags);
1914 if (drvdata->reading) {
1915 ret = -EBUSY;
1916 goto out;
1917 }
1918
1919 /*
1920 * We can safely allow reads even if the ETR is operating in PERF mode,
1921 * since the sysfs session is captured in mode specific data.
1922 * If drvdata::sysfs_data is NULL the trace data has been read already.
1923 */
1924 if (!drvdata->sysfs_buf) {
1925 ret = -EINVAL;
1926 goto out;
1927 }
1928
1929 /* Disable the TMC if we are trying to read from a running session. */
1930 if (coresight_get_mode(drvdata->csdev) == CS_MODE_SYSFS)
1931 __tmc_etr_disable_hw(drvdata);
1932
1933 drvdata->reading = true;
1934 out:
1935 raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
1936
1937 return ret;
1938 }
1939
tmc_read_unprepare_etr(struct tmc_drvdata * drvdata)1940 int tmc_read_unprepare_etr(struct tmc_drvdata *drvdata)
1941 {
1942 unsigned long flags;
1943 struct etr_buf *sysfs_buf = NULL;
1944
1945 /* config types are set a boot time and never change */
1946 if (WARN_ON_ONCE(drvdata->config_type != TMC_CONFIG_TYPE_ETR))
1947 return -EINVAL;
1948
1949 raw_spin_lock_irqsave(&drvdata->spinlock, flags);
1950
1951 /* RE-enable the TMC if need be */
1952 if (coresight_get_mode(drvdata->csdev) == CS_MODE_SYSFS) {
1953 /*
1954 * The trace run will continue with the same allocated trace
1955 * buffer. Since the tracer is still enabled drvdata::buf can't
1956 * be NULL.
1957 */
1958 __tmc_etr_enable_hw(drvdata);
1959 } else {
1960 /*
1961 * The ETR is not tracing and the buffer was just read.
1962 * As such prepare to free the trace buffer.
1963 */
1964 sysfs_buf = drvdata->sysfs_buf;
1965 drvdata->sysfs_buf = NULL;
1966 }
1967
1968 drvdata->reading = false;
1969 raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
1970
1971 /* Free allocated memory out side of the spinlock */
1972 if (sysfs_buf)
1973 tmc_etr_free_sysfs_buf(sysfs_buf);
1974
1975 return 0;
1976 }
1977
1978 static const char *const buf_modes_str[] = {
1979 [ETR_MODE_FLAT] = "flat",
1980 [ETR_MODE_ETR_SG] = "tmc-sg",
1981 [ETR_MODE_CATU] = "catu",
1982 [ETR_MODE_RESRV] = "resrv",
1983 [ETR_MODE_AUTO] = "auto",
1984 };
1985
buf_modes_available_show(struct device * dev,struct device_attribute * attr,char * buf)1986 static ssize_t buf_modes_available_show(struct device *dev,
1987 struct device_attribute *attr, char *buf)
1988 {
1989 struct etr_buf_hw buf_hw;
1990 ssize_t size = 0;
1991
1992 get_etr_buf_hw(dev, &buf_hw);
1993 size += sysfs_emit(buf, "%s ", buf_modes_str[ETR_MODE_AUTO]);
1994 size += sysfs_emit_at(buf, size, "%s ", buf_modes_str[ETR_MODE_FLAT]);
1995 if (buf_hw.has_etr_sg)
1996 size += sysfs_emit_at(buf, size, "%s ", buf_modes_str[ETR_MODE_ETR_SG]);
1997
1998 if (buf_hw.has_catu)
1999 size += sysfs_emit_at(buf, size, "%s ", buf_modes_str[ETR_MODE_CATU]);
2000
2001 if (buf_hw.has_resrv)
2002 size += sysfs_emit_at(buf, size, "%s ", buf_modes_str[ETR_MODE_RESRV]);
2003
2004 size += sysfs_emit_at(buf, size, "\n");
2005 return size;
2006 }
2007 static DEVICE_ATTR_RO(buf_modes_available);
2008
buf_mode_preferred_show(struct device * dev,struct device_attribute * attr,char * buf)2009 static ssize_t buf_mode_preferred_show(struct device *dev,
2010 struct device_attribute *attr, char *buf)
2011 {
2012 struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
2013
2014 return sysfs_emit(buf, "%s\n", buf_modes_str[drvdata->etr_mode]);
2015 }
2016
buf_mode_set_resrv(struct tmc_drvdata * drvdata)2017 static int buf_mode_set_resrv(struct tmc_drvdata *drvdata)
2018 {
2019 int err = -EBUSY;
2020 unsigned long flags;
2021 struct tmc_resrv_buf *rbuf;
2022
2023 rbuf = &drvdata->resrv_buf;
2024
2025 /* Ensure there are no active crashdata read sessions */
2026 raw_spin_lock_irqsave(&drvdata->spinlock, flags);
2027 if (!rbuf->reading) {
2028 tmc_crashdata_set_invalid(drvdata);
2029 rbuf->len = 0;
2030 drvdata->etr_mode = ETR_MODE_RESRV;
2031 err = 0;
2032 }
2033 raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
2034 return err;
2035 }
2036
buf_mode_preferred_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)2037 static ssize_t buf_mode_preferred_store(struct device *dev,
2038 struct device_attribute *attr,
2039 const char *buf, size_t size)
2040 {
2041 struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
2042 struct etr_buf_hw buf_hw;
2043
2044 get_etr_buf_hw(dev, &buf_hw);
2045 if (sysfs_streq(buf, buf_modes_str[ETR_MODE_FLAT]))
2046 drvdata->etr_mode = ETR_MODE_FLAT;
2047 else if (sysfs_streq(buf, buf_modes_str[ETR_MODE_ETR_SG]) && buf_hw.has_etr_sg)
2048 drvdata->etr_mode = ETR_MODE_ETR_SG;
2049 else if (sysfs_streq(buf, buf_modes_str[ETR_MODE_CATU]) && buf_hw.has_catu)
2050 drvdata->etr_mode = ETR_MODE_CATU;
2051 else if (sysfs_streq(buf, buf_modes_str[ETR_MODE_RESRV]) && buf_hw.has_resrv)
2052 return buf_mode_set_resrv(drvdata) ? : size;
2053 else if (sysfs_streq(buf, buf_modes_str[ETR_MODE_AUTO]))
2054 drvdata->etr_mode = ETR_MODE_AUTO;
2055 else
2056 return -EINVAL;
2057 return size;
2058 }
2059 static DEVICE_ATTR_RW(buf_mode_preferred);
2060
2061 static struct attribute *coresight_etr_attrs[] = {
2062 &dev_attr_buf_modes_available.attr,
2063 &dev_attr_buf_mode_preferred.attr,
2064 NULL,
2065 };
2066
2067 const struct attribute_group coresight_etr_group = {
2068 .attrs = coresight_etr_attrs,
2069 };
2070