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