• 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 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_noncoherent(real_dev, etr_buf->size,
614 						&flat_buf->daddr,
615 						DMA_FROM_DEVICE, GFP_KERNEL);
616 	if (!flat_buf->vaddr) {
617 		kfree(flat_buf);
618 		return -ENOMEM;
619 	}
620 
621 	flat_buf->size = etr_buf->size;
622 	flat_buf->dev = &drvdata->csdev->dev;
623 	etr_buf->hwaddr = flat_buf->daddr;
624 	etr_buf->mode = ETR_MODE_FLAT;
625 	etr_buf->private = flat_buf;
626 	return 0;
627 }
628 
tmc_etr_free_flat_buf(struct etr_buf * etr_buf)629 static void tmc_etr_free_flat_buf(struct etr_buf *etr_buf)
630 {
631 	struct etr_flat_buf *flat_buf = etr_buf->private;
632 
633 	if (flat_buf && flat_buf->daddr) {
634 		struct device *real_dev = flat_buf->dev->parent;
635 
636 		dma_free_noncoherent(real_dev, etr_buf->size,
637 				     flat_buf->vaddr, flat_buf->daddr,
638 				     DMA_FROM_DEVICE);
639 	}
640 	kfree(flat_buf);
641 }
642 
tmc_etr_sync_flat_buf(struct etr_buf * etr_buf,u64 rrp,u64 rwp)643 static void tmc_etr_sync_flat_buf(struct etr_buf *etr_buf, u64 rrp, u64 rwp)
644 {
645 	struct etr_flat_buf *flat_buf = etr_buf->private;
646 	struct device *real_dev = flat_buf->dev->parent;
647 
648 	/*
649 	 * Adjust the buffer to point to the beginning of the trace data
650 	 * and update the available trace data.
651 	 */
652 	etr_buf->offset = rrp - etr_buf->hwaddr;
653 	if (etr_buf->full)
654 		etr_buf->len = etr_buf->size;
655 	else
656 		etr_buf->len = rwp - rrp;
657 
658 	/*
659 	 * The driver always starts tracing at the beginning of the buffer,
660 	 * the only reason why we would get a wrap around is when the buffer
661 	 * is full.  Sync the entire buffer in one go for this case.
662 	 */
663 	if (etr_buf->offset + etr_buf->len > etr_buf->size)
664 		dma_sync_single_for_cpu(real_dev, flat_buf->daddr,
665 					etr_buf->size, DMA_FROM_DEVICE);
666 	else
667 		dma_sync_single_for_cpu(real_dev,
668 					flat_buf->daddr + etr_buf->offset,
669 					etr_buf->len, DMA_FROM_DEVICE);
670 }
671 
tmc_etr_get_data_flat_buf(struct etr_buf * etr_buf,u64 offset,size_t len,char ** bufpp)672 static ssize_t tmc_etr_get_data_flat_buf(struct etr_buf *etr_buf,
673 					 u64 offset, size_t len, char **bufpp)
674 {
675 	struct etr_flat_buf *flat_buf = etr_buf->private;
676 
677 	*bufpp = (char *)flat_buf->vaddr + offset;
678 	/*
679 	 * tmc_etr_buf_get_data already adjusts the length to handle
680 	 * buffer wrapping around.
681 	 */
682 	return len;
683 }
684 
685 static const struct etr_buf_operations etr_flat_buf_ops = {
686 	.alloc = tmc_etr_alloc_flat_buf,
687 	.free = tmc_etr_free_flat_buf,
688 	.sync = tmc_etr_sync_flat_buf,
689 	.get_data = tmc_etr_get_data_flat_buf,
690 };
691 
692 /*
693  * tmc_etr_alloc_sg_buf: Allocate an SG buf @etr_buf. Setup the parameters
694  * appropriately.
695  */
tmc_etr_alloc_sg_buf(struct tmc_drvdata * drvdata,struct etr_buf * etr_buf,int node,void ** pages)696 static int tmc_etr_alloc_sg_buf(struct tmc_drvdata *drvdata,
697 				struct etr_buf *etr_buf, int node,
698 				void **pages)
699 {
700 	struct etr_sg_table *etr_table;
701 	struct device *dev = &drvdata->csdev->dev;
702 
703 	etr_table = tmc_init_etr_sg_table(dev, node,
704 					  etr_buf->size, pages);
705 	if (IS_ERR(etr_table))
706 		return -ENOMEM;
707 	etr_buf->hwaddr = etr_table->hwaddr;
708 	etr_buf->mode = ETR_MODE_ETR_SG;
709 	etr_buf->private = etr_table;
710 	return 0;
711 }
712 
tmc_etr_free_sg_buf(struct etr_buf * etr_buf)713 static void tmc_etr_free_sg_buf(struct etr_buf *etr_buf)
714 {
715 	struct etr_sg_table *etr_table = etr_buf->private;
716 
717 	if (etr_table) {
718 		tmc_free_sg_table(etr_table->sg_table);
719 		kfree(etr_table);
720 	}
721 }
722 
tmc_etr_get_data_sg_buf(struct etr_buf * etr_buf,u64 offset,size_t len,char ** bufpp)723 static ssize_t tmc_etr_get_data_sg_buf(struct etr_buf *etr_buf, u64 offset,
724 				       size_t len, char **bufpp)
725 {
726 	struct etr_sg_table *etr_table = etr_buf->private;
727 
728 	return tmc_sg_table_get_data(etr_table->sg_table, offset, len, bufpp);
729 }
730 
tmc_etr_sync_sg_buf(struct etr_buf * etr_buf,u64 rrp,u64 rwp)731 static void tmc_etr_sync_sg_buf(struct etr_buf *etr_buf, u64 rrp, u64 rwp)
732 {
733 	long r_offset, w_offset;
734 	struct etr_sg_table *etr_table = etr_buf->private;
735 	struct tmc_sg_table *table = etr_table->sg_table;
736 
737 	/* Convert hw address to offset in the buffer */
738 	r_offset = tmc_sg_get_data_page_offset(table, rrp);
739 	if (r_offset < 0) {
740 		dev_warn(table->dev,
741 			 "Unable to map RRP %llx to offset\n", rrp);
742 		etr_buf->len = 0;
743 		return;
744 	}
745 
746 	w_offset = tmc_sg_get_data_page_offset(table, rwp);
747 	if (w_offset < 0) {
748 		dev_warn(table->dev,
749 			 "Unable to map RWP %llx to offset\n", rwp);
750 		etr_buf->len = 0;
751 		return;
752 	}
753 
754 	etr_buf->offset = r_offset;
755 	if (etr_buf->full)
756 		etr_buf->len = etr_buf->size;
757 	else
758 		etr_buf->len = ((w_offset < r_offset) ? etr_buf->size : 0) +
759 				w_offset - r_offset;
760 	tmc_sg_table_sync_data_range(table, r_offset, etr_buf->len);
761 }
762 
763 static const struct etr_buf_operations etr_sg_buf_ops = {
764 	.alloc = tmc_etr_alloc_sg_buf,
765 	.free = tmc_etr_free_sg_buf,
766 	.sync = tmc_etr_sync_sg_buf,
767 	.get_data = tmc_etr_get_data_sg_buf,
768 };
769 
770 /*
771  * TMC ETR could be connected to a CATU device, which can provide address
772  * translation service. This is represented by the Output port of the TMC
773  * (ETR) connected to the input port of the CATU.
774  *
775  * Returns	: coresight_device ptr for the CATU device if a CATU is found.
776  *		: NULL otherwise.
777  */
778 struct coresight_device *
tmc_etr_get_catu_device(struct tmc_drvdata * drvdata)779 tmc_etr_get_catu_device(struct tmc_drvdata *drvdata)
780 {
781 	int i;
782 	struct coresight_device *tmp, *etr = drvdata->csdev;
783 
784 	if (!IS_ENABLED(CONFIG_CORESIGHT_CATU))
785 		return NULL;
786 
787 	for (i = 0; i < etr->pdata->nr_outport; i++) {
788 		tmp = etr->pdata->conns[i].child_dev;
789 		if (tmp && coresight_is_catu_device(tmp))
790 			return tmp;
791 	}
792 
793 	return NULL;
794 }
795 EXPORT_SYMBOL_GPL(tmc_etr_get_catu_device);
796 
tmc_etr_enable_catu(struct tmc_drvdata * drvdata,struct etr_buf * etr_buf)797 static inline int tmc_etr_enable_catu(struct tmc_drvdata *drvdata,
798 				      struct etr_buf *etr_buf)
799 {
800 	struct coresight_device *catu = tmc_etr_get_catu_device(drvdata);
801 
802 	if (catu && helper_ops(catu)->enable)
803 		return helper_ops(catu)->enable(catu, etr_buf);
804 	return 0;
805 }
806 
tmc_etr_disable_catu(struct tmc_drvdata * drvdata)807 static inline void tmc_etr_disable_catu(struct tmc_drvdata *drvdata)
808 {
809 	struct coresight_device *catu = tmc_etr_get_catu_device(drvdata);
810 
811 	if (catu && helper_ops(catu)->disable)
812 		helper_ops(catu)->disable(catu, drvdata->etr_buf);
813 }
814 
815 static const struct etr_buf_operations *etr_buf_ops[] = {
816 	[ETR_MODE_FLAT] = &etr_flat_buf_ops,
817 	[ETR_MODE_ETR_SG] = &etr_sg_buf_ops,
818 	[ETR_MODE_CATU] = NULL,
819 };
820 
tmc_etr_set_catu_ops(const struct etr_buf_operations * catu)821 void tmc_etr_set_catu_ops(const struct etr_buf_operations *catu)
822 {
823 	etr_buf_ops[ETR_MODE_CATU] = catu;
824 }
825 EXPORT_SYMBOL_GPL(tmc_etr_set_catu_ops);
826 
tmc_etr_remove_catu_ops(void)827 void tmc_etr_remove_catu_ops(void)
828 {
829 	etr_buf_ops[ETR_MODE_CATU] = NULL;
830 }
831 EXPORT_SYMBOL_GPL(tmc_etr_remove_catu_ops);
832 
tmc_etr_mode_alloc_buf(int mode,struct tmc_drvdata * drvdata,struct etr_buf * etr_buf,int node,void ** pages)833 static inline int tmc_etr_mode_alloc_buf(int mode,
834 					 struct tmc_drvdata *drvdata,
835 					 struct etr_buf *etr_buf, int node,
836 					 void **pages)
837 {
838 	int rc = -EINVAL;
839 
840 	switch (mode) {
841 	case ETR_MODE_FLAT:
842 	case ETR_MODE_ETR_SG:
843 	case ETR_MODE_CATU:
844 		if (etr_buf_ops[mode] && etr_buf_ops[mode]->alloc)
845 			rc = etr_buf_ops[mode]->alloc(drvdata, etr_buf,
846 						      node, pages);
847 		if (!rc)
848 			etr_buf->ops = etr_buf_ops[mode];
849 		return rc;
850 	default:
851 		return -EINVAL;
852 	}
853 }
854 
855 /*
856  * tmc_alloc_etr_buf: Allocate a buffer use by ETR.
857  * @drvdata	: ETR device details.
858  * @size	: size of the requested buffer.
859  * @flags	: Required properties for the buffer.
860  * @node	: Node for memory allocations.
861  * @pages	: An optional list of pages.
862  */
tmc_alloc_etr_buf(struct tmc_drvdata * drvdata,ssize_t size,int flags,int node,void ** pages)863 static struct etr_buf *tmc_alloc_etr_buf(struct tmc_drvdata *drvdata,
864 					 ssize_t size, int flags,
865 					 int node, void **pages)
866 {
867 	int rc = -ENOMEM;
868 	bool has_etr_sg, has_iommu;
869 	bool has_sg, has_catu;
870 	struct etr_buf *etr_buf;
871 	struct device *dev = &drvdata->csdev->dev;
872 
873 	has_etr_sg = tmc_etr_has_cap(drvdata, TMC_ETR_SG);
874 	has_iommu = iommu_get_domain_for_dev(dev->parent);
875 	has_catu = !!tmc_etr_get_catu_device(drvdata);
876 
877 	has_sg = has_catu || has_etr_sg;
878 
879 	etr_buf = kzalloc(sizeof(*etr_buf), GFP_KERNEL);
880 	if (!etr_buf)
881 		return ERR_PTR(-ENOMEM);
882 
883 	etr_buf->size = size;
884 
885 	/*
886 	 * If we have to use an existing list of pages, we cannot reliably
887 	 * use a contiguous DMA memory (even if we have an IOMMU). Otherwise,
888 	 * we use the contiguous DMA memory if at least one of the following
889 	 * conditions is true:
890 	 *  a) The ETR cannot use Scatter-Gather.
891 	 *  b) we have a backing IOMMU
892 	 *  c) The requested memory size is smaller (< 1M).
893 	 *
894 	 * Fallback to available mechanisms.
895 	 *
896 	 */
897 	if (!pages &&
898 	    (!has_sg || has_iommu || size < SZ_1M))
899 		rc = tmc_etr_mode_alloc_buf(ETR_MODE_FLAT, drvdata,
900 					    etr_buf, node, pages);
901 	if (rc && has_etr_sg)
902 		rc = tmc_etr_mode_alloc_buf(ETR_MODE_ETR_SG, drvdata,
903 					    etr_buf, node, pages);
904 	if (rc && has_catu)
905 		rc = tmc_etr_mode_alloc_buf(ETR_MODE_CATU, drvdata,
906 					    etr_buf, node, pages);
907 	if (rc) {
908 		kfree(etr_buf);
909 		return ERR_PTR(rc);
910 	}
911 
912 	refcount_set(&etr_buf->refcount, 1);
913 	dev_dbg(dev, "allocated buffer of size %ldKB in mode %d\n",
914 		(unsigned long)size >> 10, etr_buf->mode);
915 	return etr_buf;
916 }
917 
tmc_free_etr_buf(struct etr_buf * etr_buf)918 static void tmc_free_etr_buf(struct etr_buf *etr_buf)
919 {
920 	WARN_ON(!etr_buf->ops || !etr_buf->ops->free);
921 	etr_buf->ops->free(etr_buf);
922 	kfree(etr_buf);
923 }
924 
925 /*
926  * tmc_etr_buf_get_data: Get the pointer the trace data at @offset
927  * with a maximum of @len bytes.
928  * Returns: The size of the linear data available @pos, with *bufpp
929  * updated to point to the buffer.
930  */
tmc_etr_buf_get_data(struct etr_buf * etr_buf,u64 offset,size_t len,char ** bufpp)931 static ssize_t tmc_etr_buf_get_data(struct etr_buf *etr_buf,
932 				    u64 offset, size_t len, char **bufpp)
933 {
934 	/* Adjust the length to limit this transaction to end of buffer */
935 	len = (len < (etr_buf->size - offset)) ? len : etr_buf->size - offset;
936 
937 	return etr_buf->ops->get_data(etr_buf, (u64)offset, len, bufpp);
938 }
939 
940 static inline s64
tmc_etr_buf_insert_barrier_packet(struct etr_buf * etr_buf,u64 offset)941 tmc_etr_buf_insert_barrier_packet(struct etr_buf *etr_buf, u64 offset)
942 {
943 	ssize_t len;
944 	char *bufp;
945 
946 	len = tmc_etr_buf_get_data(etr_buf, offset,
947 				   CORESIGHT_BARRIER_PKT_SIZE, &bufp);
948 	if (WARN_ON(len < 0 || len < CORESIGHT_BARRIER_PKT_SIZE))
949 		return -EINVAL;
950 	coresight_insert_barrier_packet(bufp);
951 	return offset + CORESIGHT_BARRIER_PKT_SIZE;
952 }
953 
954 /*
955  * tmc_sync_etr_buf: Sync the trace buffer availability with drvdata.
956  * Makes sure the trace data is synced to the memory for consumption.
957  * @etr_buf->offset will hold the offset to the beginning of the trace data
958  * within the buffer, with @etr_buf->len bytes to consume.
959  */
tmc_sync_etr_buf(struct tmc_drvdata * drvdata)960 static void tmc_sync_etr_buf(struct tmc_drvdata *drvdata)
961 {
962 	struct etr_buf *etr_buf = drvdata->etr_buf;
963 	u64 rrp, rwp;
964 	u32 status;
965 
966 	rrp = tmc_read_rrp(drvdata);
967 	rwp = tmc_read_rwp(drvdata);
968 	status = readl_relaxed(drvdata->base + TMC_STS);
969 
970 	/*
971 	 * If there were memory errors in the session, truncate the
972 	 * buffer.
973 	 */
974 	if (WARN_ON_ONCE(status & TMC_STS_MEMERR)) {
975 		dev_dbg(&drvdata->csdev->dev,
976 			"tmc memory error detected, truncating buffer\n");
977 		etr_buf->len = 0;
978 		etr_buf->full = 0;
979 		return;
980 	}
981 
982 	etr_buf->full = status & TMC_STS_FULL;
983 
984 	WARN_ON(!etr_buf->ops || !etr_buf->ops->sync);
985 
986 	etr_buf->ops->sync(etr_buf, rrp, rwp);
987 }
988 
__tmc_etr_enable_hw(struct tmc_drvdata * drvdata)989 static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
990 {
991 	u32 axictl, sts;
992 	struct etr_buf *etr_buf = drvdata->etr_buf;
993 
994 	CS_UNLOCK(drvdata->base);
995 
996 	/* Wait for TMCSReady bit to be set */
997 	tmc_wait_for_tmcready(drvdata);
998 
999 	writel_relaxed(etr_buf->size / 4, drvdata->base + TMC_RSZ);
1000 	writel_relaxed(TMC_MODE_CIRCULAR_BUFFER, drvdata->base + TMC_MODE);
1001 
1002 	axictl = readl_relaxed(drvdata->base + TMC_AXICTL);
1003 	axictl &= ~TMC_AXICTL_CLEAR_MASK;
1004 	axictl |= (TMC_AXICTL_PROT_CTL_B1 | TMC_AXICTL_WR_BURST_16);
1005 	axictl |= TMC_AXICTL_AXCACHE_OS;
1006 
1007 	if (tmc_etr_has_cap(drvdata, TMC_ETR_AXI_ARCACHE)) {
1008 		axictl &= ~TMC_AXICTL_ARCACHE_MASK;
1009 		axictl |= TMC_AXICTL_ARCACHE_OS;
1010 	}
1011 
1012 	if (etr_buf->mode == ETR_MODE_ETR_SG)
1013 		axictl |= TMC_AXICTL_SCT_GAT_MODE;
1014 
1015 	writel_relaxed(axictl, drvdata->base + TMC_AXICTL);
1016 	tmc_write_dba(drvdata, etr_buf->hwaddr);
1017 	/*
1018 	 * If the TMC pointers must be programmed before the session,
1019 	 * we have to set it properly (i.e, RRP/RWP to base address and
1020 	 * STS to "not full").
1021 	 */
1022 	if (tmc_etr_has_cap(drvdata, TMC_ETR_SAVE_RESTORE)) {
1023 		tmc_write_rrp(drvdata, etr_buf->hwaddr);
1024 		tmc_write_rwp(drvdata, etr_buf->hwaddr);
1025 		sts = readl_relaxed(drvdata->base + TMC_STS) & ~TMC_STS_FULL;
1026 		writel_relaxed(sts, drvdata->base + TMC_STS);
1027 	}
1028 
1029 	writel_relaxed(TMC_FFCR_EN_FMT | TMC_FFCR_EN_TI |
1030 		       TMC_FFCR_FON_FLIN | TMC_FFCR_FON_TRIG_EVT |
1031 		       TMC_FFCR_TRIGON_TRIGIN,
1032 		       drvdata->base + TMC_FFCR);
1033 	writel_relaxed(drvdata->trigger_cntr, drvdata->base + TMC_TRG);
1034 	tmc_enable_hw(drvdata);
1035 
1036 	CS_LOCK(drvdata->base);
1037 }
1038 
tmc_etr_enable_hw(struct tmc_drvdata * drvdata,struct etr_buf * etr_buf)1039 static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
1040 			     struct etr_buf *etr_buf)
1041 {
1042 	int rc;
1043 
1044 	/* Callers should provide an appropriate buffer for use */
1045 	if (WARN_ON(!etr_buf))
1046 		return -EINVAL;
1047 
1048 	if ((etr_buf->mode == ETR_MODE_ETR_SG) &&
1049 	    WARN_ON(!tmc_etr_has_cap(drvdata, TMC_ETR_SG)))
1050 		return -EINVAL;
1051 
1052 	if (WARN_ON(drvdata->etr_buf))
1053 		return -EBUSY;
1054 
1055 	/*
1056 	 * If this ETR is connected to a CATU, enable it before we turn
1057 	 * this on.
1058 	 */
1059 	rc = tmc_etr_enable_catu(drvdata, etr_buf);
1060 	if (rc)
1061 		return rc;
1062 	rc = coresight_claim_device(drvdata->csdev);
1063 	if (!rc) {
1064 		drvdata->etr_buf = etr_buf;
1065 		__tmc_etr_enable_hw(drvdata);
1066 	}
1067 
1068 	return rc;
1069 }
1070 
1071 /*
1072  * Return the available trace data in the buffer (starts at etr_buf->offset,
1073  * limited by etr_buf->len) from @pos, with a maximum limit of @len,
1074  * also updating the @bufpp on where to find it. Since the trace data
1075  * starts at anywhere in the buffer, depending on the RRP, we adjust the
1076  * @len returned to handle buffer wrapping around.
1077  *
1078  * We are protected here by drvdata->reading != 0, which ensures the
1079  * sysfs_buf stays alive.
1080  */
tmc_etr_get_sysfs_trace(struct tmc_drvdata * drvdata,loff_t pos,size_t len,char ** bufpp)1081 ssize_t tmc_etr_get_sysfs_trace(struct tmc_drvdata *drvdata,
1082 				loff_t pos, size_t len, char **bufpp)
1083 {
1084 	s64 offset;
1085 	ssize_t actual = len;
1086 	struct etr_buf *etr_buf = drvdata->sysfs_buf;
1087 
1088 	if (pos + actual > etr_buf->len)
1089 		actual = etr_buf->len - pos;
1090 	if (actual <= 0)
1091 		return actual;
1092 
1093 	/* Compute the offset from which we read the data */
1094 	offset = etr_buf->offset + pos;
1095 	if (offset >= etr_buf->size)
1096 		offset -= etr_buf->size;
1097 	return tmc_etr_buf_get_data(etr_buf, offset, actual, bufpp);
1098 }
1099 
1100 static struct etr_buf *
tmc_etr_setup_sysfs_buf(struct tmc_drvdata * drvdata)1101 tmc_etr_setup_sysfs_buf(struct tmc_drvdata *drvdata)
1102 {
1103 	return tmc_alloc_etr_buf(drvdata, drvdata->size,
1104 				 0, cpu_to_node(0), NULL);
1105 }
1106 
1107 static void
tmc_etr_free_sysfs_buf(struct etr_buf * buf)1108 tmc_etr_free_sysfs_buf(struct etr_buf *buf)
1109 {
1110 	if (buf)
1111 		tmc_free_etr_buf(buf);
1112 }
1113 
tmc_etr_sync_sysfs_buf(struct tmc_drvdata * drvdata)1114 static void tmc_etr_sync_sysfs_buf(struct tmc_drvdata *drvdata)
1115 {
1116 	struct etr_buf *etr_buf = drvdata->etr_buf;
1117 
1118 	if (WARN_ON(drvdata->sysfs_buf != etr_buf)) {
1119 		tmc_etr_free_sysfs_buf(drvdata->sysfs_buf);
1120 		drvdata->sysfs_buf = NULL;
1121 	} else {
1122 		tmc_sync_etr_buf(drvdata);
1123 		/*
1124 		 * Insert barrier packets at the beginning, if there was
1125 		 * an overflow.
1126 		 */
1127 		if (etr_buf->full)
1128 			tmc_etr_buf_insert_barrier_packet(etr_buf,
1129 							  etr_buf->offset);
1130 	}
1131 }
1132 
__tmc_etr_disable_hw(struct tmc_drvdata * drvdata)1133 static void __tmc_etr_disable_hw(struct tmc_drvdata *drvdata)
1134 {
1135 	CS_UNLOCK(drvdata->base);
1136 
1137 	tmc_flush_and_stop(drvdata);
1138 	/*
1139 	 * When operating in sysFS mode the content of the buffer needs to be
1140 	 * read before the TMC is disabled.
1141 	 */
1142 	if (drvdata->mode == CS_MODE_SYSFS)
1143 		tmc_etr_sync_sysfs_buf(drvdata);
1144 
1145 	tmc_disable_hw(drvdata);
1146 
1147 	CS_LOCK(drvdata->base);
1148 
1149 }
1150 
tmc_etr_disable_hw(struct tmc_drvdata * drvdata)1151 void tmc_etr_disable_hw(struct tmc_drvdata *drvdata)
1152 {
1153 	__tmc_etr_disable_hw(drvdata);
1154 	/* Disable CATU device if this ETR is connected to one */
1155 	tmc_etr_disable_catu(drvdata);
1156 	coresight_disclaim_device(drvdata->csdev);
1157 	/* Reset the ETR buf used by hardware */
1158 	drvdata->etr_buf = NULL;
1159 }
1160 
tmc_enable_etr_sink_sysfs(struct coresight_device * csdev)1161 static int tmc_enable_etr_sink_sysfs(struct coresight_device *csdev)
1162 {
1163 	int ret = 0;
1164 	unsigned long flags;
1165 	struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1166 	struct etr_buf *sysfs_buf = NULL, *new_buf = NULL, *free_buf = NULL;
1167 
1168 	/*
1169 	 * If we are enabling the ETR from disabled state, we need to make
1170 	 * sure we have a buffer with the right size. The etr_buf is not reset
1171 	 * immediately after we stop the tracing in SYSFS mode as we wait for
1172 	 * the user to collect the data. We may be able to reuse the existing
1173 	 * buffer, provided the size matches. Any allocation has to be done
1174 	 * with the lock released.
1175 	 */
1176 	spin_lock_irqsave(&drvdata->spinlock, flags);
1177 	sysfs_buf = READ_ONCE(drvdata->sysfs_buf);
1178 	if (!sysfs_buf || (sysfs_buf->size != drvdata->size)) {
1179 		spin_unlock_irqrestore(&drvdata->spinlock, flags);
1180 
1181 		/* Allocate memory with the locks released */
1182 		free_buf = new_buf = tmc_etr_setup_sysfs_buf(drvdata);
1183 		if (IS_ERR(new_buf))
1184 			return PTR_ERR(new_buf);
1185 
1186 		/* Let's try again */
1187 		spin_lock_irqsave(&drvdata->spinlock, flags);
1188 	}
1189 
1190 	if (drvdata->reading || drvdata->mode == CS_MODE_PERF) {
1191 		ret = -EBUSY;
1192 		goto out;
1193 	}
1194 
1195 	/*
1196 	 * In sysFS mode we can have multiple writers per sink.  Since this
1197 	 * sink is already enabled no memory is needed and the HW need not be
1198 	 * touched, even if the buffer size has changed.
1199 	 */
1200 	if (drvdata->mode == CS_MODE_SYSFS) {
1201 		atomic_inc(csdev->refcnt);
1202 		goto out;
1203 	}
1204 
1205 	/*
1206 	 * If we don't have a buffer or it doesn't match the requested size,
1207 	 * use the buffer allocated above. Otherwise reuse the existing buffer.
1208 	 */
1209 	sysfs_buf = READ_ONCE(drvdata->sysfs_buf);
1210 	if (!sysfs_buf || (new_buf && sysfs_buf->size != new_buf->size)) {
1211 		free_buf = sysfs_buf;
1212 		drvdata->sysfs_buf = new_buf;
1213 	}
1214 
1215 	ret = tmc_etr_enable_hw(drvdata, drvdata->sysfs_buf);
1216 	if (!ret) {
1217 		drvdata->mode = CS_MODE_SYSFS;
1218 		atomic_inc(csdev->refcnt);
1219 	}
1220 out:
1221 	spin_unlock_irqrestore(&drvdata->spinlock, flags);
1222 
1223 	/* Free memory outside the spinlock if need be */
1224 	if (free_buf)
1225 		tmc_etr_free_sysfs_buf(free_buf);
1226 
1227 	if (!ret)
1228 		dev_dbg(&csdev->dev, "TMC-ETR enabled\n");
1229 
1230 	return ret;
1231 }
1232 
1233 /*
1234  * alloc_etr_buf: Allocate ETR buffer for use by perf.
1235  * The size of the hardware buffer is dependent on the size configured
1236  * via sysfs and the perf ring buffer size. We prefer to allocate the
1237  * largest possible size, scaling down the size by half until it
1238  * reaches a minimum limit (1M), beyond which we give up.
1239  */
1240 static struct etr_buf *
alloc_etr_buf(struct tmc_drvdata * drvdata,struct perf_event * event,int nr_pages,void ** pages,bool snapshot)1241 alloc_etr_buf(struct tmc_drvdata *drvdata, struct perf_event *event,
1242 	      int nr_pages, void **pages, bool snapshot)
1243 {
1244 	int node;
1245 	struct etr_buf *etr_buf;
1246 	unsigned long size;
1247 
1248 	node = (event->cpu == -1) ? NUMA_NO_NODE : cpu_to_node(event->cpu);
1249 	/*
1250 	 * Try to match the perf ring buffer size if it is larger
1251 	 * than the size requested via sysfs.
1252 	 */
1253 	if ((nr_pages << PAGE_SHIFT) > drvdata->size) {
1254 		etr_buf = tmc_alloc_etr_buf(drvdata, ((ssize_t)nr_pages << PAGE_SHIFT),
1255 					    0, node, NULL);
1256 		if (!IS_ERR(etr_buf))
1257 			goto done;
1258 	}
1259 
1260 	/*
1261 	 * Else switch to configured size for this ETR
1262 	 * and scale down until we hit the minimum limit.
1263 	 */
1264 	size = drvdata->size;
1265 	do {
1266 		etr_buf = tmc_alloc_etr_buf(drvdata, size, 0, node, NULL);
1267 		if (!IS_ERR(etr_buf))
1268 			goto done;
1269 		size /= 2;
1270 	} while (size >= TMC_ETR_PERF_MIN_BUF_SIZE);
1271 
1272 	return ERR_PTR(-ENOMEM);
1273 
1274 done:
1275 	return etr_buf;
1276 }
1277 
1278 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)1279 get_perf_etr_buf_cpu_wide(struct tmc_drvdata *drvdata,
1280 			  struct perf_event *event, int nr_pages,
1281 			  void **pages, bool snapshot)
1282 {
1283 	int ret;
1284 	pid_t pid = task_pid_nr(event->owner);
1285 	struct etr_buf *etr_buf;
1286 
1287 retry:
1288 	/*
1289 	 * An etr_perf_buffer is associated with an event and holds a reference
1290 	 * to the AUX ring buffer that was created for that event.  In CPU-wide
1291 	 * N:1 mode multiple events (one per CPU), each with its own AUX ring
1292 	 * buffer, share a sink.  As such an etr_perf_buffer is created for each
1293 	 * event but a single etr_buf associated with the ETR is shared between
1294 	 * them.  The last event in a trace session will copy the content of the
1295 	 * etr_buf to its AUX ring buffer.  Ring buffer associated to other
1296 	 * events are simply not used an freed as events are destoyed.  We still
1297 	 * need to allocate a ring buffer for each event since we don't know
1298 	 * which event will be last.
1299 	 */
1300 
1301 	/*
1302 	 * The first thing to do here is check if an etr_buf has already been
1303 	 * allocated for this session.  If so it is shared with this event,
1304 	 * otherwise it is created.
1305 	 */
1306 	mutex_lock(&drvdata->idr_mutex);
1307 	etr_buf = idr_find(&drvdata->idr, pid);
1308 	if (etr_buf) {
1309 		refcount_inc(&etr_buf->refcount);
1310 		mutex_unlock(&drvdata->idr_mutex);
1311 		return etr_buf;
1312 	}
1313 
1314 	/* If we made it here no buffer has been allocated, do so now. */
1315 	mutex_unlock(&drvdata->idr_mutex);
1316 
1317 	etr_buf = alloc_etr_buf(drvdata, event, nr_pages, pages, snapshot);
1318 	if (IS_ERR(etr_buf))
1319 		return etr_buf;
1320 
1321 	/* Now that we have a buffer, add it to the IDR. */
1322 	mutex_lock(&drvdata->idr_mutex);
1323 	ret = idr_alloc(&drvdata->idr, etr_buf, pid, pid + 1, GFP_KERNEL);
1324 	mutex_unlock(&drvdata->idr_mutex);
1325 
1326 	/* Another event with this session ID has allocated this buffer. */
1327 	if (ret == -ENOSPC) {
1328 		tmc_free_etr_buf(etr_buf);
1329 		goto retry;
1330 	}
1331 
1332 	/* The IDR can't allocate room for a new session, abandon ship. */
1333 	if (ret == -ENOMEM) {
1334 		tmc_free_etr_buf(etr_buf);
1335 		return ERR_PTR(ret);
1336 	}
1337 
1338 
1339 	return etr_buf;
1340 }
1341 
1342 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)1343 get_perf_etr_buf_per_thread(struct tmc_drvdata *drvdata,
1344 			    struct perf_event *event, int nr_pages,
1345 			    void **pages, bool snapshot)
1346 {
1347 	/*
1348 	 * In per-thread mode the etr_buf isn't shared, so just go ahead
1349 	 * with memory allocation.
1350 	 */
1351 	return alloc_etr_buf(drvdata, event, nr_pages, pages, snapshot);
1352 }
1353 
1354 static struct etr_buf *
get_perf_etr_buf(struct tmc_drvdata * drvdata,struct perf_event * event,int nr_pages,void ** pages,bool snapshot)1355 get_perf_etr_buf(struct tmc_drvdata *drvdata, struct perf_event *event,
1356 		 int nr_pages, void **pages, bool snapshot)
1357 {
1358 	if (event->cpu == -1)
1359 		return get_perf_etr_buf_per_thread(drvdata, event, nr_pages,
1360 						   pages, snapshot);
1361 
1362 	return get_perf_etr_buf_cpu_wide(drvdata, event, nr_pages,
1363 					 pages, snapshot);
1364 }
1365 
1366 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)1367 tmc_etr_setup_perf_buf(struct tmc_drvdata *drvdata, struct perf_event *event,
1368 		       int nr_pages, void **pages, bool snapshot)
1369 {
1370 	int node;
1371 	struct etr_buf *etr_buf;
1372 	struct etr_perf_buffer *etr_perf;
1373 
1374 	node = (event->cpu == -1) ? NUMA_NO_NODE : cpu_to_node(event->cpu);
1375 
1376 	etr_perf = kzalloc_node(sizeof(*etr_perf), GFP_KERNEL, node);
1377 	if (!etr_perf)
1378 		return ERR_PTR(-ENOMEM);
1379 
1380 	etr_buf = get_perf_etr_buf(drvdata, event, nr_pages, pages, snapshot);
1381 	if (!IS_ERR(etr_buf))
1382 		goto done;
1383 
1384 	kfree(etr_perf);
1385 	return ERR_PTR(-ENOMEM);
1386 
1387 done:
1388 	/*
1389 	 * Keep a reference to the ETR this buffer has been allocated for
1390 	 * in order to have access to the IDR in tmc_free_etr_buffer().
1391 	 */
1392 	etr_perf->drvdata = drvdata;
1393 	etr_perf->etr_buf = etr_buf;
1394 
1395 	return etr_perf;
1396 }
1397 
1398 
tmc_alloc_etr_buffer(struct coresight_device * csdev,struct perf_event * event,void ** pages,int nr_pages,bool snapshot)1399 static void *tmc_alloc_etr_buffer(struct coresight_device *csdev,
1400 				  struct perf_event *event, void **pages,
1401 				  int nr_pages, bool snapshot)
1402 {
1403 	struct etr_perf_buffer *etr_perf;
1404 	struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1405 
1406 	etr_perf = tmc_etr_setup_perf_buf(drvdata, event,
1407 					  nr_pages, pages, snapshot);
1408 	if (IS_ERR(etr_perf)) {
1409 		dev_dbg(&csdev->dev, "Unable to allocate ETR buffer\n");
1410 		return NULL;
1411 	}
1412 
1413 	etr_perf->pid = task_pid_nr(event->owner);
1414 	etr_perf->snapshot = snapshot;
1415 	etr_perf->nr_pages = nr_pages;
1416 	etr_perf->pages = pages;
1417 
1418 	return etr_perf;
1419 }
1420 
tmc_free_etr_buffer(void * config)1421 static void tmc_free_etr_buffer(void *config)
1422 {
1423 	struct etr_perf_buffer *etr_perf = config;
1424 	struct tmc_drvdata *drvdata = etr_perf->drvdata;
1425 	struct etr_buf *buf, *etr_buf = etr_perf->etr_buf;
1426 
1427 	if (!etr_buf)
1428 		goto free_etr_perf_buffer;
1429 
1430 	mutex_lock(&drvdata->idr_mutex);
1431 	/* If we are not the last one to use the buffer, don't touch it. */
1432 	if (!refcount_dec_and_test(&etr_buf->refcount)) {
1433 		mutex_unlock(&drvdata->idr_mutex);
1434 		goto free_etr_perf_buffer;
1435 	}
1436 
1437 	/* We are the last one, remove from the IDR and free the buffer. */
1438 	buf = idr_remove(&drvdata->idr, etr_perf->pid);
1439 	mutex_unlock(&drvdata->idr_mutex);
1440 
1441 	/*
1442 	 * Something went very wrong if the buffer associated with this ID
1443 	 * is not the same in the IDR.  Leak to avoid use after free.
1444 	 */
1445 	if (buf && WARN_ON(buf != etr_buf))
1446 		goto free_etr_perf_buffer;
1447 
1448 	tmc_free_etr_buf(etr_perf->etr_buf);
1449 
1450 free_etr_perf_buffer:
1451 	kfree(etr_perf);
1452 }
1453 
1454 /*
1455  * tmc_etr_sync_perf_buffer: Copy the actual trace data from the hardware
1456  * buffer to the perf ring buffer.
1457  */
tmc_etr_sync_perf_buffer(struct etr_perf_buffer * etr_perf,unsigned long src_offset,unsigned long to_copy)1458 static void tmc_etr_sync_perf_buffer(struct etr_perf_buffer *etr_perf,
1459 				     unsigned long src_offset,
1460 				     unsigned long to_copy)
1461 {
1462 	long bytes;
1463 	long pg_idx, pg_offset;
1464 	unsigned long head = etr_perf->head;
1465 	char **dst_pages, *src_buf;
1466 	struct etr_buf *etr_buf = etr_perf->etr_buf;
1467 
1468 	head = etr_perf->head;
1469 	pg_idx = head >> PAGE_SHIFT;
1470 	pg_offset = head & (PAGE_SIZE - 1);
1471 	dst_pages = (char **)etr_perf->pages;
1472 
1473 	while (to_copy > 0) {
1474 		/*
1475 		 * In one iteration, we can copy minimum of :
1476 		 *  1) what is available in the source buffer,
1477 		 *  2) what is available in the source buffer, before it
1478 		 *     wraps around.
1479 		 *  3) what is available in the destination page.
1480 		 * in one iteration.
1481 		 */
1482 		if (src_offset >= etr_buf->size)
1483 			src_offset -= etr_buf->size;
1484 		bytes = tmc_etr_buf_get_data(etr_buf, src_offset, to_copy,
1485 					     &src_buf);
1486 		if (WARN_ON_ONCE(bytes <= 0))
1487 			break;
1488 		bytes = min(bytes, (long)(PAGE_SIZE - pg_offset));
1489 
1490 		memcpy(dst_pages[pg_idx] + pg_offset, src_buf, bytes);
1491 
1492 		to_copy -= bytes;
1493 
1494 		/* Move destination pointers */
1495 		pg_offset += bytes;
1496 		if (pg_offset == PAGE_SIZE) {
1497 			pg_offset = 0;
1498 			if (++pg_idx == etr_perf->nr_pages)
1499 				pg_idx = 0;
1500 		}
1501 
1502 		/* Move source pointers */
1503 		src_offset += bytes;
1504 	}
1505 }
1506 
1507 /*
1508  * tmc_update_etr_buffer : Update the perf ring buffer with the
1509  * available trace data. We use software double buffering at the moment.
1510  *
1511  * TODO: Add support for reusing the perf ring buffer.
1512  */
1513 static unsigned long
tmc_update_etr_buffer(struct coresight_device * csdev,struct perf_output_handle * handle,void * config)1514 tmc_update_etr_buffer(struct coresight_device *csdev,
1515 		      struct perf_output_handle *handle,
1516 		      void *config)
1517 {
1518 	bool lost = false;
1519 	unsigned long flags, offset, size = 0;
1520 	struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1521 	struct etr_perf_buffer *etr_perf = config;
1522 	struct etr_buf *etr_buf = etr_perf->etr_buf;
1523 
1524 	spin_lock_irqsave(&drvdata->spinlock, flags);
1525 
1526 	/* Don't do anything if another tracer is using this sink */
1527 	if (atomic_read(csdev->refcnt) != 1) {
1528 		spin_unlock_irqrestore(&drvdata->spinlock, flags);
1529 		goto out;
1530 	}
1531 
1532 	if (WARN_ON(drvdata->perf_buf != etr_buf)) {
1533 		lost = true;
1534 		spin_unlock_irqrestore(&drvdata->spinlock, flags);
1535 		goto out;
1536 	}
1537 
1538 	CS_UNLOCK(drvdata->base);
1539 
1540 	tmc_flush_and_stop(drvdata);
1541 	tmc_sync_etr_buf(drvdata);
1542 
1543 	CS_LOCK(drvdata->base);
1544 	spin_unlock_irqrestore(&drvdata->spinlock, flags);
1545 
1546 	lost = etr_buf->full;
1547 	offset = etr_buf->offset;
1548 	size = etr_buf->len;
1549 
1550 	/*
1551 	 * The ETR buffer may be bigger than the space available in the
1552 	 * perf ring buffer (handle->size).  If so advance the offset so that we
1553 	 * get the latest trace data.  In snapshot mode none of that matters
1554 	 * since we are expected to clobber stale data in favour of the latest
1555 	 * traces.
1556 	 */
1557 	if (!etr_perf->snapshot && size > handle->size) {
1558 		u32 mask = tmc_get_memwidth_mask(drvdata);
1559 
1560 		/*
1561 		 * Make sure the new size is aligned in accordance with the
1562 		 * requirement explained in function tmc_get_memwidth_mask().
1563 		 */
1564 		size = handle->size & mask;
1565 		offset = etr_buf->offset + etr_buf->len - size;
1566 
1567 		if (offset >= etr_buf->size)
1568 			offset -= etr_buf->size;
1569 		lost = true;
1570 	}
1571 
1572 	/* Insert barrier packets at the beginning, if there was an overflow */
1573 	if (lost)
1574 		tmc_etr_buf_insert_barrier_packet(etr_buf, offset);
1575 	tmc_etr_sync_perf_buffer(etr_perf, offset, size);
1576 
1577 	/*
1578 	 * In snapshot mode we simply increment the head by the number of byte
1579 	 * that were written.  User space function  cs_etm_find_snapshot() will
1580 	 * figure out how many bytes to get from the AUX buffer based on the
1581 	 * position of the head.
1582 	 */
1583 	if (etr_perf->snapshot)
1584 		handle->head += size;
1585 
1586 	/*
1587 	 * Ensure that the AUX trace data is visible before the aux_head
1588 	 * is updated via perf_aux_output_end(), as expected by the
1589 	 * perf ring buffer.
1590 	 */
1591 	smp_wmb();
1592 
1593 out:
1594 	/*
1595 	 * Don't set the TRUNCATED flag in snapshot mode because 1) the
1596 	 * captured buffer is expected to be truncated and 2) a full buffer
1597 	 * prevents the event from being re-enabled by the perf core,
1598 	 * resulting in stale data being send to user space.
1599 	 */
1600 	if (!etr_perf->snapshot && lost)
1601 		perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED);
1602 	return size;
1603 }
1604 
tmc_enable_etr_sink_perf(struct coresight_device * csdev,void * data)1605 static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data)
1606 {
1607 	int rc = 0;
1608 	pid_t pid;
1609 	unsigned long flags;
1610 	struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1611 	struct perf_output_handle *handle = data;
1612 	struct etr_perf_buffer *etr_perf = etm_perf_sink_config(handle);
1613 
1614 	spin_lock_irqsave(&drvdata->spinlock, flags);
1615 	 /* Don't use this sink if it is already claimed by sysFS */
1616 	if (drvdata->mode == CS_MODE_SYSFS) {
1617 		rc = -EBUSY;
1618 		goto unlock_out;
1619 	}
1620 
1621 	if (WARN_ON(!etr_perf || !etr_perf->etr_buf)) {
1622 		rc = -EINVAL;
1623 		goto unlock_out;
1624 	}
1625 
1626 	/* Get a handle on the pid of the process to monitor */
1627 	pid = etr_perf->pid;
1628 
1629 	/* Do not proceed if this device is associated with another session */
1630 	if (drvdata->pid != -1 && drvdata->pid != pid) {
1631 		rc = -EBUSY;
1632 		goto unlock_out;
1633 	}
1634 
1635 	etr_perf->head = PERF_IDX2OFF(handle->head, etr_perf);
1636 
1637 	/*
1638 	 * No HW configuration is needed if the sink is already in
1639 	 * use for this session.
1640 	 */
1641 	if (drvdata->pid == pid) {
1642 		atomic_inc(csdev->refcnt);
1643 		goto unlock_out;
1644 	}
1645 
1646 	rc = tmc_etr_enable_hw(drvdata, etr_perf->etr_buf);
1647 	if (!rc) {
1648 		/* Associate with monitored process. */
1649 		drvdata->pid = pid;
1650 		drvdata->mode = CS_MODE_PERF;
1651 		drvdata->perf_buf = etr_perf->etr_buf;
1652 		atomic_inc(csdev->refcnt);
1653 	}
1654 
1655 unlock_out:
1656 	spin_unlock_irqrestore(&drvdata->spinlock, flags);
1657 	return rc;
1658 }
1659 
tmc_enable_etr_sink(struct coresight_device * csdev,u32 mode,void * data)1660 static int tmc_enable_etr_sink(struct coresight_device *csdev,
1661 			       u32 mode, void *data)
1662 {
1663 	switch (mode) {
1664 	case CS_MODE_SYSFS:
1665 		return tmc_enable_etr_sink_sysfs(csdev);
1666 	case CS_MODE_PERF:
1667 		return tmc_enable_etr_sink_perf(csdev, data);
1668 	}
1669 
1670 	/* We shouldn't be here */
1671 	return -EINVAL;
1672 }
1673 
tmc_disable_etr_sink(struct coresight_device * csdev)1674 static int tmc_disable_etr_sink(struct coresight_device *csdev)
1675 {
1676 	unsigned long flags;
1677 	struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1678 
1679 	spin_lock_irqsave(&drvdata->spinlock, flags);
1680 
1681 	if (drvdata->reading) {
1682 		spin_unlock_irqrestore(&drvdata->spinlock, flags);
1683 		return -EBUSY;
1684 	}
1685 
1686 	if (atomic_dec_return(csdev->refcnt)) {
1687 		spin_unlock_irqrestore(&drvdata->spinlock, flags);
1688 		return -EBUSY;
1689 	}
1690 
1691 	/* Complain if we (somehow) got out of sync */
1692 	WARN_ON_ONCE(drvdata->mode == CS_MODE_DISABLED);
1693 	tmc_etr_disable_hw(drvdata);
1694 	/* Dissociate from monitored process. */
1695 	drvdata->pid = -1;
1696 	drvdata->mode = CS_MODE_DISABLED;
1697 	/* Reset perf specific data */
1698 	drvdata->perf_buf = NULL;
1699 
1700 	spin_unlock_irqrestore(&drvdata->spinlock, flags);
1701 
1702 	dev_dbg(&csdev->dev, "TMC-ETR disabled\n");
1703 	return 0;
1704 }
1705 
1706 static const struct coresight_ops_sink tmc_etr_sink_ops = {
1707 	.enable		= tmc_enable_etr_sink,
1708 	.disable	= tmc_disable_etr_sink,
1709 	.alloc_buffer	= tmc_alloc_etr_buffer,
1710 	.update_buffer	= tmc_update_etr_buffer,
1711 	.free_buffer	= tmc_free_etr_buffer,
1712 };
1713 
1714 const struct coresight_ops tmc_etr_cs_ops = {
1715 	.sink_ops	= &tmc_etr_sink_ops,
1716 };
1717 
tmc_read_prepare_etr(struct tmc_drvdata * drvdata)1718 int tmc_read_prepare_etr(struct tmc_drvdata *drvdata)
1719 {
1720 	int ret = 0;
1721 	unsigned long flags;
1722 
1723 	/* config types are set a boot time and never change */
1724 	if (WARN_ON_ONCE(drvdata->config_type != TMC_CONFIG_TYPE_ETR))
1725 		return -EINVAL;
1726 
1727 	spin_lock_irqsave(&drvdata->spinlock, flags);
1728 	if (drvdata->reading) {
1729 		ret = -EBUSY;
1730 		goto out;
1731 	}
1732 
1733 	/*
1734 	 * We can safely allow reads even if the ETR is operating in PERF mode,
1735 	 * since the sysfs session is captured in mode specific data.
1736 	 * If drvdata::sysfs_data is NULL the trace data has been read already.
1737 	 */
1738 	if (!drvdata->sysfs_buf) {
1739 		ret = -EINVAL;
1740 		goto out;
1741 	}
1742 
1743 	/* Disable the TMC if we are trying to read from a running session. */
1744 	if (drvdata->mode == CS_MODE_SYSFS)
1745 		__tmc_etr_disable_hw(drvdata);
1746 
1747 	drvdata->reading = true;
1748 out:
1749 	spin_unlock_irqrestore(&drvdata->spinlock, flags);
1750 
1751 	return ret;
1752 }
1753 
tmc_read_unprepare_etr(struct tmc_drvdata * drvdata)1754 int tmc_read_unprepare_etr(struct tmc_drvdata *drvdata)
1755 {
1756 	unsigned long flags;
1757 	struct etr_buf *sysfs_buf = NULL;
1758 
1759 	/* config types are set a boot time and never change */
1760 	if (WARN_ON_ONCE(drvdata->config_type != TMC_CONFIG_TYPE_ETR))
1761 		return -EINVAL;
1762 
1763 	spin_lock_irqsave(&drvdata->spinlock, flags);
1764 
1765 	/* RE-enable the TMC if need be */
1766 	if (drvdata->mode == CS_MODE_SYSFS) {
1767 		/*
1768 		 * The trace run will continue with the same allocated trace
1769 		 * buffer. Since the tracer is still enabled drvdata::buf can't
1770 		 * be NULL.
1771 		 */
1772 		__tmc_etr_enable_hw(drvdata);
1773 	} else {
1774 		/*
1775 		 * The ETR is not tracing and the buffer was just read.
1776 		 * As such prepare to free the trace buffer.
1777 		 */
1778 		sysfs_buf = drvdata->sysfs_buf;
1779 		drvdata->sysfs_buf = NULL;
1780 	}
1781 
1782 	drvdata->reading = false;
1783 	spin_unlock_irqrestore(&drvdata->spinlock, flags);
1784 
1785 	/* Free allocated memory out side of the spinlock */
1786 	if (sysfs_buf)
1787 		tmc_etr_free_sysfs_buf(sysfs_buf);
1788 
1789 	return 0;
1790 }
1791