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