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