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