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