/* * etr_perf_buffer - Perf buffer used for ETR * @drvdata - The ETR drvdaga this buffer has been allocated for. * @etr_buf - Actual buffer used by the ETR * @pid - The PID of the session owner that etr_perf_buffer * belongs to. * @snaphost - Perf session mode * @nr_pages - Number of pages in the ring buffer. * @pages - Array of Pages in the ring buffer.
*/ struct etr_perf_buffer { struct tmc_drvdata *drvdata; struct etr_buf *etr_buf;
pid_t pid; bool snapshot; int nr_pages; void **pages;
};
/* Convert the perf index to an offset within the ETR buffer */ #define PERF_IDX2OFF(idx, buf) \
((idx) % ((unsignedlong)(buf)->nr_pages << PAGE_SHIFT))
/* * The TMC ETR SG has a page size of 4K. The SG table contains pointers * to 4KB buffers. However, the OS may use a PAGE_SIZE different from * 4K (i.e, 16KB or 64KB). This implies that a single OS page could * contain more than one SG buffer and tables. * * A table entry has the following format: * * ---Bit31------------Bit4-------Bit1-----Bit0-- * | Address[39:12] | SBZ | Entry Type | * ---------------------------------------------- * * Address: Bits [39:12] of a physical page address. Bits [11:0] are * always zero. * * Entry type: * b00 - Reserved. * b01 - Last entry in the tables, points to 4K page buffer. * b10 - Normal entry, points to 4K page buffer. * b11 - Link. The address points to the base of next table.
*/
/* * struct etr_sg_table : ETR SG Table * @sg_table: Generic SG Table holding the data/table pages. * @hwaddr: hwaddress used by the TMC, which is the base * address of the table.
*/ struct etr_sg_table { struct tmc_sg_table *sg_table;
dma_addr_t hwaddr;
};
/* * tmc_etr_sg_table_entries: Total number of table entries required to map * @nr_pages system pages. * * We need to map @nr_pages * ETR_SG_PAGES_PER_SYSPAGE data pages. * Each TMC page can map (ETR_SG_PTRS_PER_PAGE - 1) buffer pointers, * with the last entry pointing to another page of table entries. * If we spill over to a new page for mapping 1 entry, we could as * well replace the link entry of the previous page with the last entry.
*/ staticunsignedlong __attribute_const__
tmc_etr_sg_table_entries(int nr_pages)
{ unsignedlong nr_sgpages = nr_pages * ETR_SG_PAGES_PER_SYSPAGE; unsignedlong nr_sglinks = nr_sgpages / (ETR_SG_PTRS_PER_PAGE - 1); /* * If we spill over to a new page for 1 entry, we could as well * make it the LAST entry in the previous page, skipping the Link * address.
*/ if (nr_sglinks && (nr_sgpages % (ETR_SG_PTRS_PER_PAGE - 1) < 2))
nr_sglinks--; return nr_sgpages + nr_sglinks;
}
/* * tmc_pages_get_offset: Go through all the pages in the tmc_pages * and map the device address @addr to an offset within the virtual * contiguous buffer.
*/ staticlong
tmc_pages_get_offset(struct tmc_pages *tmc_pages, dma_addr_t addr)
{ int i;
dma_addr_t page_start;
for (i = 0; i < tmc_pages->nr_pages; i++) {
page_start = tmc_pages->daddrs[i]; if (addr >= page_start && addr < (page_start + PAGE_SIZE)) return i * PAGE_SIZE + (addr - page_start);
}
return -EINVAL;
}
/* * tmc_pages_free : Unmap and free the pages used by tmc_pages. * If the pages were not allocated in tmc_pages_alloc(), we would * simply drop the refcount.
*/ staticvoid tmc_pages_free(struct tmc_pages *tmc_pages, struct device *dev, enum dma_data_direction dir)
{ int i; struct device *real_dev = dev->parent;
for (i = 0; i < tmc_pages->nr_pages; i++) { if (tmc_pages->daddrs && tmc_pages->daddrs[i])
dma_unmap_page(real_dev, tmc_pages->daddrs[i],
PAGE_SIZE, dir); if (tmc_pages->pages && tmc_pages->pages[i])
__free_page(tmc_pages->pages[i]);
}
/* * tmc_pages_alloc : Allocate and map pages for a given @tmc_pages. * If @pages is not NULL, the list of page virtual addresses are * used as the data pages. The pages are then dma_map'ed for @dev * with dma_direction @dir. * * Returns 0 upon success, else the error number.
*/ staticint tmc_pages_alloc(struct tmc_pages *tmc_pages, struct device *dev, int node, enum dma_data_direction dir, void **pages)
{ int i, nr_pages;
dma_addr_t paddr; struct page *page; struct device *real_dev = dev->parent;
/* * Alloc pages for the table. Since this will be used by the device, * allocate the pages closer to the device (i.e, dev_to_node(dev) * rather than the CPU node).
*/ staticint tmc_alloc_table_pages(struct tmc_sg_table *sg_table)
{ int rc; struct tmc_pages *table_pages = &sg_table->table_pages;
staticint tmc_alloc_data_pages(struct tmc_sg_table *sg_table, void **pages)
{ int rc;
/* Allocate data pages on the node requested by the caller */
rc = tmc_pages_alloc(&sg_table->data_pages,
sg_table->dev, sg_table->node,
DMA_FROM_DEVICE, pages); if (!rc) {
sg_table->data_vaddr = vmap(sg_table->data_pages.pages,
sg_table->data_pages.nr_pages,
VM_MAP,
PAGE_KERNEL); if (!sg_table->data_vaddr)
rc = -ENOMEM;
} return rc;
}
/* * tmc_alloc_sg_table: Allocate and setup dma pages for the TMC SG table * and data buffers. TMC writes to the data buffers and reads from the SG * Table pages. * * @dev - Coresight device to which page should be DMA mapped. * @node - Numa node for mem allocations * @nr_tpages - Number of pages for the table entries. * @nr_dpages - Number of pages for Data buffer. * @pages - Optional list of virtual address of pages.
*/ struct tmc_sg_table *tmc_alloc_sg_table(struct device *dev, int node, int nr_tpages, int nr_dpages, void **pages)
{ long rc; struct tmc_sg_table *sg_table;
/* * tmc_sg_table_sync_data_range: Sync the data buffer written * by the device from @offset upto a @size bytes.
*/ void tmc_sg_table_sync_data_range(struct tmc_sg_table *table,
u64 offset, u64 size)
{ int i, index, start; int npages = DIV_ROUND_UP(size, PAGE_SIZE); struct device *real_dev = table->dev->parent; struct tmc_pages *data = &table->data_pages;
start = offset >> PAGE_SHIFT; for (i = start; i < (start + npages); i++) {
index = i % data->nr_pages;
dma_sync_single_for_cpu(real_dev, data->daddrs[index],
PAGE_SIZE, DMA_FROM_DEVICE);
}
}
EXPORT_SYMBOL_GPL(tmc_sg_table_sync_data_range);
for (i = 0; i < table_pages->nr_pages; i++)
dma_sync_single_for_device(real_dev, table_pages->daddrs[i],
PAGE_SIZE, DMA_TO_DEVICE);
}
EXPORT_SYMBOL_GPL(tmc_sg_table_sync_table);
/* * tmc_sg_table_get_data: Get the buffer pointer for data @offset * in the SG buffer. The @bufpp is updated to point to the buffer. * Returns : * the length of linear data available at @offset. * or * <= 0 if no data is available.
*/
ssize_t tmc_sg_table_get_data(struct tmc_sg_table *sg_table,
u64 offset, size_t len, char **bufpp)
{
size_t size; int pg_idx = offset >> PAGE_SHIFT; int pg_offset = offset & (PAGE_SIZE - 1); struct tmc_pages *data_pages = &sg_table->data_pages;
size = tmc_sg_table_buf_size(sg_table); if (offset >= size) return -EINVAL;
/* Make sure we don't go beyond the end */
len = (len < (size - offset)) ? len : size - offset; /* Respect the page boundaries */
len = (len < (PAGE_SIZE - pg_offset)) ? len : (PAGE_SIZE - pg_offset); if (len > 0)
*bufpp = page_address(data_pages->pages[pg_idx]) + pg_offset; return len;
}
EXPORT_SYMBOL_GPL(tmc_sg_table_get_data);
#ifdef ETR_SG_DEBUG /* Map a dma address to virtual address */ staticunsignedlong
tmc_sg_daddr_to_vaddr(struct tmc_sg_table *sg_table,
dma_addr_t addr, bool table)
{ long offset; unsignedlong base; struct tmc_pages *tmc_pages;
if (table) {
tmc_pages = &sg_table->table_pages;
base = (unsignedlong)sg_table->table_vaddr;
} else {
tmc_pages = &sg_table->data_pages;
base = (unsignedlong)sg_table->data_vaddr;
}
offset = tmc_pages_get_offset(tmc_pages, addr); if (offset < 0) return 0; return base + offset;
}
/* Dump the given sg_table */ staticvoid tmc_etr_sg_table_dump(struct etr_sg_table *etr_table)
{
sgte_t *ptr; int i = 0;
dma_addr_t addr; struct tmc_sg_table *sg_table = etr_table->sg_table;
ptr = (sgte_t *)tmc_sg_daddr_to_vaddr(sg_table,
etr_table->hwaddr, true); while (ptr) {
addr = ETR_SG_ADDR(*ptr); switch (ETR_SG_ET(*ptr)) { case ETR_SG_ET_NORMAL:
dev_dbg(sg_table->dev, "%05d: %p\t:[N] 0x%llx\n", i, ptr, addr);
ptr++; break; case ETR_SG_ET_LINK:
dev_dbg(sg_table->dev, "%05d: *** %p\t:{L} 0x%llx ***\n",
i, ptr, addr);
ptr = (sgte_t *)tmc_sg_daddr_to_vaddr(sg_table,
addr, true); break; case ETR_SG_ET_LAST:
dev_dbg(sg_table->dev, "%05d: ### %p\t:[L] 0x%llx ###\n",
i, ptr, addr); return; default:
dev_dbg(sg_table->dev, "%05d: xxx %p\t:[INVALID] 0x%llx xxx\n",
i, ptr, addr); return;
}
i++;
}
dev_dbg(sg_table->dev, "******* End of Table *****\n");
} #else staticvoid tmc_etr_sg_table_dump(struct etr_sg_table *etr_table) {} #endif
/* * Populate the SG Table page table entries from table/data * pages allocated. Each Data page has ETR_SG_PAGES_PER_SYSPAGE SG pages. * So does a Table page. So we keep track of indices of the tables * in each system page and move the pointers accordingly.
*/ #define INC_IDX_ROUND(idx, size) ((idx) = ((idx) + 1) % (size)) staticvoid tmc_etr_sg_table_populate(struct etr_sg_table *etr_table)
{
dma_addr_t paddr; int i, type, nr_entries; int tpidx = 0; /* index to the current system table_page */ int sgtidx = 0; /* index to the sg_table within the current syspage */ int sgtentry = 0; /* the entry within the sg_table */ int dpidx = 0; /* index to the current system data_page */ int spidx = 0; /* index to the SG page within the current data page */
sgte_t *ptr; /* pointer to the table entry to fill */ struct tmc_sg_table *sg_table = etr_table->sg_table;
dma_addr_t *table_daddrs = sg_table->table_pages.daddrs;
dma_addr_t *data_daddrs = sg_table->data_pages.daddrs;
nr_entries = tmc_etr_sg_table_entries(sg_table->data_pages.nr_pages); /* * Use the contiguous virtual address of the table to update entries.
*/
ptr = sg_table->table_vaddr; /* * Fill all the entries, except the last entry to avoid special * checks within the loop.
*/ for (i = 0; i < nr_entries - 1; i++) { if (sgtentry == ETR_SG_PTRS_PER_PAGE - 1) { /* * Last entry in a sg_table page is a link address to * the next table page. If this sg_table is the last * one in the system page, it links to the first * sg_table in the next system page. Otherwise, it * links to the next sg_table page within the system * page.
*/ if (sgtidx == ETR_SG_PAGES_PER_SYSPAGE - 1) {
paddr = table_daddrs[tpidx + 1];
} else {
paddr = table_daddrs[tpidx] +
(ETR_SG_PAGE_SIZE * (sgtidx + 1));
}
type = ETR_SG_ET_LINK;
} else { /* * Update the indices to the data_pages to point to the * next sg_page in the data buffer.
*/
type = ETR_SG_ET_NORMAL;
paddr = data_daddrs[dpidx] + spidx * ETR_SG_PAGE_SIZE; if (!INC_IDX_ROUND(spidx, ETR_SG_PAGES_PER_SYSPAGE))
dpidx++;
}
*ptr++ = ETR_SG_ENTRY(paddr, type); /* * Move to the next table pointer, moving the table page index * if necessary
*/ if (!INC_IDX_ROUND(sgtentry, ETR_SG_PTRS_PER_PAGE)) { if (!INC_IDX_ROUND(sgtidx, ETR_SG_PAGES_PER_SYSPAGE))
tpidx++;
}
}
/* Set up the last entry, which is always a data pointer */
paddr = data_daddrs[dpidx] + spidx * ETR_SG_PAGE_SIZE;
*ptr++ = ETR_SG_ENTRY(paddr, ETR_SG_ET_LAST);
}
/* * tmc_init_etr_sg_table: Allocate a TMC ETR SG table, data buffer of @size and * populate the table. * * @dev - Device pointer for the TMC * @node - NUMA node where the memory should be allocated * @size - Total size of the data buffer * @pages - Optional list of page virtual address
*/ staticstruct etr_sg_table *
tmc_init_etr_sg_table(struct device *dev, int node, unsignedlong size, void **pages)
{ int nr_entries, nr_tpages; int nr_dpages = size >> PAGE_SHIFT; struct tmc_sg_table *sg_table; struct etr_sg_table *etr_table;
etr_table->sg_table = sg_table; /* TMC should use table base address for DBA */
etr_table->hwaddr = sg_table->table_daddr;
tmc_etr_sg_table_populate(etr_table); /* Sync the table pages for the HW */
tmc_sg_table_sync_table(sg_table);
tmc_etr_sg_table_dump(etr_table);
/* * Adjust the buffer to point to the beginning of the trace data * and update the available trace data.
*/
etr_buf->offset = rrp - etr_buf->hwaddr; if (etr_buf->full)
etr_buf->len = etr_buf->size; else
etr_buf->len = rwp - rrp;
/* * The driver always starts tracing at the beginning of the buffer, * the only reason why we would get a wrap around is when the buffer * is full. Sync the entire buffer in one go for this case.
*/ if (etr_buf->offset + etr_buf->len > etr_buf->size)
dma_sync_single_for_cpu(real_dev, flat_buf->daddr,
etr_buf->size, DMA_FROM_DEVICE); else
dma_sync_single_for_cpu(real_dev,
flat_buf->daddr + etr_buf->offset,
etr_buf->len, DMA_FROM_DEVICE);
}
staticvoid tmc_etr_sync_resrv_buf(struct etr_buf *etr_buf, u64 rrp, u64 rwp)
{ /* * Adjust the buffer to point to the beginning of the trace data * and update the available trace data.
*/
etr_buf->offset = rrp - etr_buf->hwaddr; if (etr_buf->full)
etr_buf->len = etr_buf->size; else
etr_buf->len = rwp - rrp;
}
/* * TMC ETR could be connected to a CATU device, which can provide address * translation service. This is represented by the Output port of the TMC * (ETR) connected to the input port of the CATU. * * Returns : coresight_device ptr for the CATU device if a CATU is found. * : NULL otherwise.
*/ struct coresight_device *
tmc_etr_get_catu_device(struct tmc_drvdata *drvdata)
{ struct coresight_device *etr = drvdata->csdev; union coresight_dev_subtype catu_subtype = {
.helper_subtype = CORESIGHT_DEV_SUBTYPE_HELPER_CATU
};
if (!IS_ENABLED(CONFIG_CORESIGHT_CATU)) return NULL;
/* * tmc_alloc_etr_buf: Allocate a buffer use by ETR. * @drvdata : ETR device details. * @size : size of the requested buffer. * @flags : Required properties for the buffer. * @node : Node for memory allocations. * @pages : An optional list of pages.
*/ staticstruct etr_buf *tmc_alloc_etr_buf(struct tmc_drvdata *drvdata,
ssize_t size, int flags, int node, void **pages)
{ int rc = -ENOMEM; struct etr_buf *etr_buf; struct etr_buf_hw buf_hw; struct device *dev = &drvdata->csdev->dev;
get_etr_buf_hw(dev, &buf_hw);
etr_buf = kzalloc(sizeof(*etr_buf), GFP_KERNEL); if (!etr_buf) return ERR_PTR(-ENOMEM);
etr_buf->size = size;
/* If there is user directive for buffer mode, try that first */ if (drvdata->etr_mode != ETR_MODE_AUTO)
rc = tmc_etr_mode_alloc_buf(drvdata->etr_mode, drvdata,
etr_buf, node, pages);
/* * If we have to use an existing list of pages, we cannot reliably * use a contiguous DMA memory (even if we have an IOMMU). Otherwise, * we use the contiguous DMA memory if at least one of the following * conditions is true: * a) The ETR cannot use Scatter-Gather. * b) we have a backing IOMMU * c) The requested memory size is smaller (< 1M). * * Fallback to available mechanisms. *
*/ if (rc && !pages && etr_can_use_flat_mode(&buf_hw, size))
rc = tmc_etr_mode_alloc_buf(ETR_MODE_FLAT, drvdata,
etr_buf, node, pages); if (rc && buf_hw.has_etr_sg)
rc = tmc_etr_mode_alloc_buf(ETR_MODE_ETR_SG, drvdata,
etr_buf, node, pages); if (rc && buf_hw.has_catu)
rc = tmc_etr_mode_alloc_buf(ETR_MODE_CATU, drvdata,
etr_buf, node, pages); if (rc) {
kfree(etr_buf); return ERR_PTR(rc);
}
refcount_set(&etr_buf->refcount, 1);
dev_dbg(dev, "allocated buffer of size %ldKB in mode %d\n",
(unsignedlong)size >> 10, etr_buf->mode); return etr_buf;
}
/* * tmc_etr_buf_get_data: Get the pointer the trace data at @offset * with a maximum of @len bytes. * Returns: The size of the linear data available @pos, with *bufpp * updated to point to the buffer.
*/ static ssize_t tmc_etr_buf_get_data(struct etr_buf *etr_buf,
u64 offset, size_t len, char **bufpp)
{ /* Adjust the length to limit this transaction to end of buffer */
len = (len < (etr_buf->size - offset)) ? len : etr_buf->size - offset;
len = tmc_etr_buf_get_data(etr_buf, offset,
CORESIGHT_BARRIER_PKT_SIZE, &bufp); if (WARN_ON(len < 0 || len < CORESIGHT_BARRIER_PKT_SIZE)) return -EINVAL;
coresight_insert_barrier_packet(bufp); return offset + CORESIGHT_BARRIER_PKT_SIZE;
}
/* * tmc_sync_etr_buf: Sync the trace buffer availability with drvdata. * Makes sure the trace data is synced to the memory for consumption. * @etr_buf->offset will hold the offset to the beginning of the trace data * within the buffer, with @etr_buf->len bytes to consume.
*/ staticvoid tmc_sync_etr_buf(struct tmc_drvdata *drvdata)
{ struct etr_buf *etr_buf = drvdata->etr_buf;
u64 rrp, rwp;
u32 status;
/* * If there were memory errors in the session, truncate the * buffer.
*/ if (WARN_ON_ONCE(status & TMC_STS_MEMERR)) {
dev_dbg(&drvdata->csdev->dev, "tmc memory error detected, truncating buffer\n");
etr_buf->len = 0;
etr_buf->full = false; return;
}
/* Wait for TMCSReady bit to be set */
rc = tmc_wait_for_tmcready(drvdata); if (rc) {
dev_err(&drvdata->csdev->dev, "Failed to enable : TMC not ready\n");
CS_LOCK(drvdata->base); return rc;
}
if (etr_buf->mode == ETR_MODE_ETR_SG)
axictl |= TMC_AXICTL_SCT_GAT_MODE;
writel_relaxed(axictl, drvdata->base + TMC_AXICTL);
tmc_write_dba(drvdata, etr_buf->hwaddr); /* * If the TMC pointers must be programmed before the session, * we have to set it properly (i.e, RRP/RWP to base address and * STS to "not full").
*/ if (tmc_etr_has_cap(drvdata, TMC_ETR_SAVE_RESTORE)) {
tmc_write_rrp(drvdata, etr_buf->hwaddr);
tmc_write_rwp(drvdata, etr_buf->hwaddr);
sts = readl_relaxed(drvdata->base + TMC_STS) & ~TMC_STS_FULL;
writel_relaxed(sts, drvdata->base + TMC_STS);
}
/* * Return the available trace data in the buffer (starts at etr_buf->offset, * limited by etr_buf->len) from @pos, with a maximum limit of @len, * also updating the @bufpp on where to find it. Since the trace data * starts at anywhere in the buffer, depending on the RRP, we adjust the * @len returned to handle buffer wrapping around. * * We are protected here by drvdata->reading != 0, which ensures the * sysfs_buf stays alive.
*/
ssize_t tmc_etr_get_sysfs_trace(struct tmc_drvdata *drvdata,
loff_t pos, size_t len, char **bufpp)
{
s64 offset;
ssize_t actual = len; struct etr_buf *etr_buf = drvdata->sysfs_buf;
if (pos + actual > etr_buf->len)
actual = etr_buf->len - pos; if (actual <= 0) return actual;
/* Compute the offset from which we read the data */
offset = etr_buf->offset + pos; if (offset >= etr_buf->size)
offset -= etr_buf->size; return tmc_etr_buf_get_data(etr_buf, offset, actual, bufpp);
}
if (WARN_ON(drvdata->sysfs_buf != etr_buf)) {
tmc_etr_free_sysfs_buf(drvdata->sysfs_buf);
drvdata->sysfs_buf = NULL;
} else {
tmc_sync_etr_buf(drvdata); /* * Insert barrier packets at the beginning, if there was * an overflow.
*/ if (etr_buf->full)
tmc_etr_buf_insert_barrier_packet(etr_buf,
etr_buf->offset);
}
}
tmc_flush_and_stop(drvdata); /* * When operating in sysFS mode the content of the buffer needs to be * read before the TMC is disabled.
*/ if (coresight_get_mode(drvdata->csdev) == CS_MODE_SYSFS)
tmc_etr_sync_sysfs_buf(drvdata);
tmc_disable_hw(drvdata);
CS_LOCK(drvdata->base);
}
void tmc_etr_disable_hw(struct tmc_drvdata *drvdata)
{
__tmc_etr_disable_hw(drvdata);
coresight_disclaim_device(drvdata->csdev); /* Reset the ETR buf used by hardware */
drvdata->etr_buf = NULL;
}
/* * If we are enabling the ETR from disabled state, we need to make * sure we have a buffer with the right size. The etr_buf is not reset * immediately after we stop the tracing in SYSFS mode as we wait for * the user to collect the data. We may be able to reuse the existing * buffer, provided the size matches. Any allocation has to be done * with the lock released.
*/
raw_spin_lock_irqsave(&drvdata->spinlock, flags);
sysfs_buf = READ_ONCE(drvdata->sysfs_buf); if (!sysfs_buf || (sysfs_buf->size != drvdata->size)) {
raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
/* Allocate memory with the locks released */
free_buf = new_buf = tmc_etr_setup_sysfs_buf(drvdata); if (IS_ERR(new_buf)) return new_buf;
/* Let's try again */
raw_spin_lock_irqsave(&drvdata->spinlock, flags);
}
if (drvdata->reading || coresight_get_mode(csdev) == CS_MODE_PERF) {
ret = -EBUSY; goto out;
}
/* * If we don't have a buffer or it doesn't match the requested size, * use the buffer allocated above. Otherwise reuse the existing buffer.
*/
sysfs_buf = READ_ONCE(drvdata->sysfs_buf); if (!sysfs_buf || (new_buf && sysfs_buf->size != new_buf->size)) {
free_buf = sysfs_buf;
drvdata->sysfs_buf = new_buf;
}
/* * In sysFS mode we can have multiple writers per sink. Since this * sink is already enabled no memory is needed and the HW need not be * touched, even if the buffer size has changed.
*/ if (coresight_get_mode(csdev) == CS_MODE_SYSFS) {
csdev->refcnt++; goto out;
}
ret = tmc_etr_enable_hw(drvdata, sysfs_buf); if (!ret) {
coresight_set_mode(csdev, CS_MODE_SYSFS);
csdev->refcnt++;
}
switch (mode) { case CS_MODE_SYSFS: return tmc_etr_get_sysfs_buffer(csdev); case CS_MODE_PERF:
etr_perf = etm_perf_sink_config(handle); if (WARN_ON(!etr_perf || !etr_perf->etr_buf)) return ERR_PTR(-EINVAL); return etr_perf->etr_buf; default: return ERR_PTR(-EINVAL);
}
}
EXPORT_SYMBOL_GPL(tmc_etr_get_buffer);
/* * alloc_etr_buf: Allocate ETR buffer for use by perf. * The size of the hardware buffer is dependent on the size configured * via sysfs and the perf ring buffer size. We prefer to allocate the * largest possible size, scaling down the size by half until it * reaches a minimum limit (1M), beyond which we give up.
*/ staticstruct etr_buf *
alloc_etr_buf(struct tmc_drvdata *drvdata, struct perf_event *event, int nr_pages, void **pages, bool snapshot)
{ int node; struct etr_buf *etr_buf; unsignedlong size;
node = (event->cpu == -1) ? NUMA_NO_NODE : cpu_to_node(event->cpu); /* * Try to match the perf ring buffer size if it is larger * than the size requested via sysfs.
*/ if ((nr_pages << PAGE_SHIFT) > drvdata->size) {
etr_buf = tmc_alloc_etr_buf(drvdata, ((ssize_t)nr_pages << PAGE_SHIFT),
0, node, NULL); if (!IS_ERR(etr_buf)) goto done;
}
/* * Else switch to configured size for this ETR * and scale down until we hit the minimum limit.
*/
size = drvdata->size; do {
etr_buf = tmc_alloc_etr_buf(drvdata, size, 0, node, NULL); if (!IS_ERR(etr_buf)) goto done;
size /= 2;
} while (size >= TMC_ETR_PERF_MIN_BUF_SIZE);
retry: /* * An etr_perf_buffer is associated with an event and holds a reference * to the AUX ring buffer that was created for that event. In CPU-wide * N:1 mode multiple events (one per CPU), each with its own AUX ring * buffer, share a sink. As such an etr_perf_buffer is created for each * event but a single etr_buf associated with the ETR is shared between * them. The last event in a trace session will copy the content of the * etr_buf to its AUX ring buffer. Ring buffer associated to other * events are simply not used an freed as events are destoyed. We still * need to allocate a ring buffer for each event since we don't know * which event will be last.
*/
/* * The first thing to do here is check if an etr_buf has already been * allocated for this session. If so it is shared with this event, * otherwise it is created.
*/
mutex_lock(&drvdata->idr_mutex);
etr_buf = idr_find(&drvdata->idr, pid); if (etr_buf) {
refcount_inc(&etr_buf->refcount);
mutex_unlock(&drvdata->idr_mutex); return etr_buf;
}
/* If we made it here no buffer has been allocated, do so now. */
mutex_unlock(&drvdata->idr_mutex);
/* Now that we have a buffer, add it to the IDR. */
mutex_lock(&drvdata->idr_mutex);
ret = idr_alloc(&drvdata->idr, etr_buf, pid, pid + 1, GFP_KERNEL);
mutex_unlock(&drvdata->idr_mutex);
/* Another event with this session ID has allocated this buffer. */ if (ret == -ENOSPC) {
tmc_free_etr_buf(etr_buf); goto retry;
}
/* The IDR can't allocate room for a new session, abandon ship. */ if (ret == -ENOMEM) {
tmc_free_etr_buf(etr_buf); return ERR_PTR(ret);
}
return etr_buf;
}
staticstruct etr_buf *
get_perf_etr_buf_per_thread(struct tmc_drvdata *drvdata, struct perf_event *event, int nr_pages, void **pages, bool snapshot)
{ /* * In per-thread mode the etr_buf isn't shared, so just go ahead * with memory allocation.
*/ return alloc_etr_buf(drvdata, event, nr_pages, pages, snapshot);
}
done: /* * Keep a reference to the ETR this buffer has been allocated for * in order to have access to the IDR in tmc_free_etr_buffer().
*/
etr_perf->drvdata = drvdata;
etr_perf->etr_buf = etr_buf;
mutex_lock(&drvdata->idr_mutex); /* If we are not the last one to use the buffer, don't touch it. */ if (!refcount_dec_and_test(&etr_buf->refcount)) {
mutex_unlock(&drvdata->idr_mutex); goto free_etr_perf_buffer;
}
/* We are the last one, remove from the IDR and free the buffer. */
buf = idr_remove(&drvdata->idr, etr_perf->pid);
mutex_unlock(&drvdata->idr_mutex);
/* * Something went very wrong if the buffer associated with this ID * is not the same in the IDR. Leak to avoid use after free.
*/ if (buf && WARN_ON(buf != etr_buf)) goto free_etr_perf_buffer;
tmc_free_etr_buf(etr_perf->etr_buf);
free_etr_perf_buffer:
kfree(etr_perf);
}
/* * tmc_etr_sync_perf_buffer: Copy the actual trace data from the hardware * buffer to the perf ring buffer.
*/ staticvoid tmc_etr_sync_perf_buffer(struct etr_perf_buffer *etr_perf, unsignedlong head, unsignedlong src_offset, unsignedlong to_copy)
{ long bytes; long pg_idx, pg_offset; char **dst_pages, *src_buf; struct etr_buf *etr_buf = etr_perf->etr_buf;
head = PERF_IDX2OFF(head, etr_perf);
pg_idx = head >> PAGE_SHIFT;
pg_offset = head & (PAGE_SIZE - 1);
dst_pages = (char **)etr_perf->pages;
while (to_copy > 0) { /* * In one iteration, we can copy minimum of : * 1) what is available in the source buffer, * 2) what is available in the source buffer, before it * wraps around. * 3) what is available in the destination page. * in one iteration.
*/ if (src_offset >= etr_buf->size)
src_offset -= etr_buf->size;
bytes = tmc_etr_buf_get_data(etr_buf, src_offset, to_copy,
&src_buf); if (WARN_ON_ONCE(bytes <= 0)) break;
bytes = min(bytes, (long)(PAGE_SIZE - pg_offset));
/* * tmc_update_etr_buffer : Update the perf ring buffer with the * available trace data. We use software double buffering at the moment. * * TODO: Add support for reusing the perf ring buffer.
*/ staticunsignedlong
tmc_update_etr_buffer(struct coresight_device *csdev, struct perf_output_handle *handle, void *config)
{ bool lost = false; unsignedlong flags, offset, size = 0; struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); struct etr_perf_buffer *etr_perf = config; struct etr_buf *etr_buf = etr_perf->etr_buf; struct perf_event *event = handle->event;
raw_spin_lock_irqsave(&drvdata->spinlock, flags);
/* Don't do anything if another tracer is using this sink */ if (csdev->refcnt != 1) {
raw_spin_unlock_irqrestore(&drvdata->spinlock, flags); goto out;
}
if (WARN_ON(drvdata->perf_buf != etr_buf)) {
lost = true;
raw_spin_unlock_irqrestore(&drvdata->spinlock, flags); goto out;
}
lost = etr_buf->full;
offset = etr_buf->offset;
size = etr_buf->len;
/* * The ETR buffer may be bigger than the space available in the * perf ring buffer (handle->size). If so advance the offset so that we * get the latest trace data. In snapshot mode none of that matters * since we are expected to clobber stale data in favour of the latest * traces.
*/ if (!etr_perf->snapshot && size > handle->size) {
u32 mask = tmc_get_memwidth_mask(drvdata);
/* * Make sure the new size is aligned in accordance with the * requirement explained in function tmc_get_memwidth_mask().
*/
size = handle->size & mask;
offset = etr_buf->offset + etr_buf->len - size;
if (offset >= etr_buf->size)
offset -= etr_buf->size;
lost = true;
}
/* Insert barrier packets at the beginning, if there was an overflow */ if (lost)
tmc_etr_buf_insert_barrier_packet(etr_buf, offset);
tmc_etr_sync_perf_buffer(etr_perf, handle->head, offset, size);
/* * In snapshot mode we simply increment the head by the number of byte * that were written. User space will figure out how many bytes to get * from the AUX buffer based on the position of the head.
*/ if (etr_perf->snapshot)
handle->head += size;
/* * Ensure that the AUX trace data is visible before the aux_head * is updated via perf_aux_output_end(), as expected by the * perf ring buffer.
*/
smp_wmb();
/* * If the event is active, it is triggered during an AUX pause. * Re-enable the sink so that it is ready when AUX resume is invoked.
*/
raw_spin_lock_irqsave(&drvdata->spinlock, flags); if (csdev->refcnt && !event->hw.state)
__tmc_etr_enable_hw(drvdata);
raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
out: /* * Don't set the TRUNCATED flag in snapshot mode because 1) the * captured buffer is expected to be truncated and 2) a full buffer * prevents the event from being re-enabled by the perf core, * resulting in stale data being send to user space.
*/ if (!etr_perf->snapshot && lost)
perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED); return size;
}
raw_spin_lock_irqsave(&drvdata->spinlock, flags); /* Don't use this sink if it is already claimed by sysFS */ if (coresight_get_mode(csdev) == CS_MODE_SYSFS) {
rc = -EBUSY; goto unlock_out;
}
/* Get a handle on the pid of the session owner */
pid = etr_perf->pid;
/* Do not proceed if this device is associated with another session */ if (drvdata->pid != -1 && drvdata->pid != pid) {
rc = -EBUSY; goto unlock_out;
}
/* * No HW configuration is needed if the sink is already in * use for this session.
*/ if (drvdata->pid == pid) {
csdev->refcnt++; goto unlock_out;
}
if (drvdata->reading) {
raw_spin_unlock_irqrestore(&drvdata->spinlock, flags); return -EBUSY;
}
csdev->refcnt--; if (csdev->refcnt) {
raw_spin_unlock_irqrestore(&drvdata->spinlock, flags); return -EBUSY;
}
/* Complain if we (somehow) got out of sync */
WARN_ON_ONCE(coresight_get_mode(csdev) == CS_MODE_DISABLED);
tmc_etr_disable_hw(drvdata); /* Dissociate from monitored process. */
drvdata->pid = -1;
coresight_set_mode(csdev, CS_MODE_DISABLED); /* Reset perf specific data */
drvdata->perf_buf = NULL;
/* Being in RESRV mode implies valid reserved memory as well */ if (drvdata->etr_buf->mode != ETR_MODE_RESRV) return 0;
if (!tmc_has_crash_mdata_buffer(drvdata)) return 0;
CS_UNLOCK(drvdata->base);
/* Proceed only if ETR is enabled */
val = readl(drvdata->base + TMC_CTL); if (!(val & TMC_CTL_CAPT_EN)) goto out;
val = readl(drvdata->base + TMC_FFSR); /* Do manual flush and stop only if its not auto-stopped */ if (!(val & TMC_FFSR_FT_STOPPED)) {
dev_dbg(&csdev->dev, "%s: Triggering manual flush\n", __func__);
tmc_flush_and_stop(drvdata);
} else
tmc_wait_for_tmcready(drvdata);
/* * Make sure all previous writes are ordered, * before we mark valid
*/
dmb(sy);
mdata->valid = true; /* * Below order need to maintained, since crc of metadata * is dependent on first
*/
mdata->crc32_tdata = find_crash_tracedata_crc(drvdata, mdata);
mdata->crc32_mdata = find_crash_metadata_crc(mdata);
int tmc_read_prepare_etr(struct tmc_drvdata *drvdata)
{ int ret = 0; unsignedlong flags;
/* config types are set a boot time and never change */ if (WARN_ON_ONCE(drvdata->config_type != TMC_CONFIG_TYPE_ETR)) return -EINVAL;
raw_spin_lock_irqsave(&drvdata->spinlock, flags); if (drvdata->reading) {
ret = -EBUSY; goto out;
}
/* * We can safely allow reads even if the ETR is operating in PERF mode, * since the sysfs session is captured in mode specific data. * If drvdata::sysfs_data is NULL the trace data has been read already.
*/ if (!drvdata->sysfs_buf) {
ret = -EINVAL; goto out;
}
/* Disable the TMC if we are trying to read from a running session. */ if (coresight_get_mode(drvdata->csdev) == CS_MODE_SYSFS)
__tmc_etr_disable_hw(drvdata);
/* config types are set a boot time and never change */ if (WARN_ON_ONCE(drvdata->config_type != TMC_CONFIG_TYPE_ETR)) return -EINVAL;
raw_spin_lock_irqsave(&drvdata->spinlock, flags);
/* RE-enable the TMC if need be */ if (coresight_get_mode(drvdata->csdev) == CS_MODE_SYSFS) { /* * The trace run will continue with the same allocated trace * buffer. Since the tracer is still enabled drvdata::buf can't * be NULL.
*/
__tmc_etr_enable_hw(drvdata);
} else { /* * The ETR is not tracing and the buffer was just read. * As such prepare to free the trace buffer.
*/
sysfs_buf = drvdata->sysfs_buf;
drvdata->sysfs_buf = NULL;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.