static void etb_update_buffer(struct coresight_device *csdev, struct perf_output_handle *handle, void *sink_config) { bool lost = false; int i, cur; u8 *buf_ptr; const u32 *barrier; u32 read_ptr, write_ptr, capacity; u32 status, read_data, to_read; unsigned long offset; struct cs_buffers *buf = sink_config; struct etb_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); if (!buf) return; capacity = drvdata->buffer_depth * ETB_FRAME_SIZE_WORDS; etb_disable_hw(drvdata); CS_UNLOCK(drvdata->base); /* unit is in words, not bytes */ read_ptr = readl_relaxed(drvdata->base + ETB_RAM_READ_POINTER); write_ptr = readl_relaxed(drvdata->base + ETB_RAM_WRITE_POINTER); /* * Entries should be aligned to the frame size. If they are not * go back to the last alignment point to give decoding tools a * chance to fix things. */ if (write_ptr % ETB_FRAME_SIZE_WORDS) { dev_err(drvdata->dev, "write_ptr: %lu not aligned to formatter frame size\n", (unsigned long)write_ptr); write_ptr &= ~(ETB_FRAME_SIZE_WORDS - 1); lost = true; } /* * Get a hold of the status register and see if a wrap around * has occurred. If so adjust things accordingly. Otherwise * start at the beginning and go until the write pointer has * been reached. */ status = readl_relaxed(drvdata->base + ETB_STATUS_REG); if (status & ETB_STATUS_RAM_FULL) { lost = true; to_read = capacity; read_ptr = write_ptr; } else { to_read = CIRC_CNT(write_ptr, read_ptr, drvdata->buffer_depth); to_read *= ETB_FRAME_SIZE_WORDS; } /* * Make sure we don't overwrite data that hasn't been consumed yet. * It is entirely possible that the HW buffer has more data than the * ring buffer can currently handle. If so adjust the start address * to take only the last traces. * * In snapshot mode we are looking to get the latest traces only and as * such, we don't care about not overwriting data that hasn't been * processed by user space. */ if (!buf->snapshot && to_read > handle->size) { u32 mask = ~(ETB_FRAME_SIZE_WORDS - 1); /* The new read pointer must be frame size aligned */ to_read = handle->size & mask; /* * Move the RAM read pointer up, keeping in mind that * everything is in frame size units. */ read_ptr = (write_ptr + drvdata->buffer_depth) - to_read / ETB_FRAME_SIZE_WORDS; /* Wrap around if need be*/ if (read_ptr > (drvdata->buffer_depth - 1)) read_ptr -= drvdata->buffer_depth; /* let the decoder know we've skipped ahead */ lost = true; } if (lost) perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED); /* finally tell HW where we want to start reading from */ writel_relaxed(read_ptr, drvdata->base + ETB_RAM_READ_POINTER); cur = buf->cur; offset = buf->offset; barrier = barrier_pkt; for (i = 0; i < to_read; i += 4) { buf_ptr = buf->data_pages[cur] + offset; read_data = readl_relaxed(drvdata->base + ETB_RAM_READ_DATA_REG); if (lost && *barrier) { read_data = *barrier; barrier++; } *(u32 *)buf_ptr = read_data; buf_ptr += 4; offset += 4; if (offset >= PAGE_SIZE) { offset = 0; cur++; /* wrap around at the end of the buffer */ cur &= buf->nr_pages - 1; } } /* reset ETB buffer for next run */ writel_relaxed(0x0, drvdata->base + ETB_RAM_READ_POINTER); writel_relaxed(0x0, drvdata->base + ETB_RAM_WRITE_POINTER); /* * In snapshot mode all we have to do is communicate to * perf_aux_output_end() the address of the current head. In full * trace mode the same function expects a size to move rb->aux_head * forward. */ if (buf->snapshot) local_set(&buf->data_size, (cur * PAGE_SIZE) + offset); else local_add(to_read, &buf->data_size); etb_enable_hw(drvdata); CS_LOCK(drvdata->base); }
static unsigned long tmc_update_etf_buffer(struct coresight_device *csdev, struct perf_output_handle *handle, void *sink_config) { bool lost = false; int i, cur; const u32 *barrier; u32 *buf_ptr; u64 read_ptr, write_ptr; u32 status; unsigned long offset, to_read = 0, flags; struct cs_buffers *buf = sink_config; struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); if (!buf) return 0; /* This shouldn't happen */ if (WARN_ON_ONCE(drvdata->mode != CS_MODE_PERF)) return 0; spin_lock_irqsave(&drvdata->spinlock, flags); /* Don't do anything if another tracer is using this sink */ if (atomic_read(csdev->refcnt) != 1) goto out; CS_UNLOCK(drvdata->base); tmc_flush_and_stop(drvdata); read_ptr = tmc_read_rrp(drvdata); write_ptr = tmc_read_rwp(drvdata); /* * Get a hold of the status register and see if a wrap around * has occurred. If so adjust things accordingly. */ status = readl_relaxed(drvdata->base + TMC_STS); if (status & TMC_STS_FULL) { lost = true; to_read = drvdata->size; } else { to_read = CIRC_CNT(write_ptr, read_ptr, drvdata->size); } /* * The TMC RAM buffer may be bigger than the space available in the * perf ring buffer (handle->size). If so advance the RRP so that we * get the latest trace data. */ if (to_read > handle->size) { u32 mask = 0; /* * The value written to RRP must be byte-address aligned to * the width of the trace memory databus _and_ to a frame * boundary (16 byte), whichever is the biggest. For example, * for 32-bit, 64-bit and 128-bit wide trace memory, the four * LSBs must be 0s. For 256-bit wide trace memory, the five * LSBs must be 0s. */ switch (drvdata->memwidth) { case TMC_MEM_INTF_WIDTH_32BITS: case TMC_MEM_INTF_WIDTH_64BITS: case TMC_MEM_INTF_WIDTH_128BITS: mask = GENMASK(31, 4); break; case TMC_MEM_INTF_WIDTH_256BITS: mask = GENMASK(31, 5); break; } /* * Make sure the new size is aligned in accordance with the * requirement explained above. */ to_read = handle->size & mask; /* Move the RAM read pointer up */ read_ptr = (write_ptr + drvdata->size) - to_read; /* Make sure we are still within our limits */ if (read_ptr > (drvdata->size - 1)) read_ptr -= drvdata->size; /* Tell the HW */ tmc_write_rrp(drvdata, read_ptr); lost = true; } if (lost) perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED); cur = buf->cur; offset = buf->offset; barrier = barrier_pkt; /* for every byte to read */ for (i = 0; i < to_read; i += 4) { buf_ptr = buf->data_pages[cur] + offset; *buf_ptr = readl_relaxed(drvdata->base + TMC_RRD); if (lost && *barrier) { *buf_ptr = *barrier; barrier++; } offset += 4; if (offset >= PAGE_SIZE) { offset = 0; cur++; /* wrap around at the end of the buffer */ cur &= buf->nr_pages - 1; } } /* In snapshot mode we have to update the head */ if (buf->snapshot) { handle->head = (cur * PAGE_SIZE) + offset; to_read = buf->nr_pages << PAGE_SHIFT; } CS_LOCK(drvdata->base); out: spin_unlock_irqrestore(&drvdata->spinlock, flags); return to_read; }