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