This series enables future IP trace features Embedded Trace Extension (ETE)
and Trace Buffer Extension (TRBE). This series depends on the ETM system
register instruction support series [0] which is available here [1]. This
series which applies on [1] is avaialble here [2] for quick access.
ETE is the PE (CPU) trace unit for CPUs, implementing future architecture
extensions. ETE overlaps with the ETMv4 architecture, with additions to
support the newer architecture features and some restrictions on the
supported features w.r.t ETMv4. The ETE support is added by extending the
ETMv4 driver to recognise the ETE and handle the features as exposed by the
TRCIDRx registers. ETE only supports system instructions access from the
host CPU. The ETE could be integrated with a TRBE (see below), or with the
legacy CoreSight trace bus (e.g, ETRs). Thus the ETE follows same firmware
description as the ETMs and requires a node per instance.
Trace Buffer Extensions (TRBE) implements a per CPU trace buffer, which is
accessible via the system registers and can be combined with the ETE to
provide a 1x1 configuration of source & sink. TRBE is being represented
here as a CoreSight sink. Primary reason is that the ETE source could work
with other traditional CoreSight sink devices. As TRBE captures the trace
data which is produced by ETE, it cannot work alone.
TRBE representation here have some distinct deviations from a traditional
CoreSight sink device. Coresight path between ETE and TRBE are not built
during boot looking at respective DT or ACPI entries.
Unlike traditional sinks, TRBE can generate interrupts to signal including
many other things, buffer got filled. The interrupt is a PPI and should be
communicated from the platform. DT or ACPI entry representing TRBE should
have the PPI number for a given platform. During perf session, the TRBE IRQ
handler should capture trace for perf auxiliary buffer before restarting it
back. System registers being used here to configure ETE and TRBE could be
referred in the link below.
https://developer.arm.com/docs/ddi0601/g/aarch64-system-registers.
Question:
- Should we implement sysfs based trace sessions for TRBE ?
[0] https://lore.kernel.org/linux-arm-kernel/20210110224850.1880240-1-suzuki.poulose@arm.com/
[1] https://gitlab.arm.com/linux-arm/linux-skp/-/tree/coresight/etm/sysreg-v7
[2] https://gitlab.arm.com/linux-arm/linux-anshuman/-/tree/coresight/ete_trbe_v3
Changes in V3:
- Rebased on coresight/next
- Changed DT bindings for ETE
- Included additional patches for arm64 nvhe, perf aux buffer flags etc
- TRBE changes have been captured in the respective patches
Changes in V2:
https://lore.kernel.org/linux-arm-kernel/1610511498-4058-1-git-send-email-anshuman.khandual@arm.com/
- Converted both ETE and TRBE DT bindings into Yaml
- TRBE changes have been captured in the respective patches
Changes in V1:
https://lore.kernel.org/linux-arm-kernel/1608717823-18387-1-git-send-email-anshuman.khandual@arm.com/
- There are not much ETE changes from Suzuki apart from splitting of the ETE DTS patch
- TRBE changes have been captured in the respective patches
Changes in RFC:
https://lore.kernel.org/linux-arm-kernel/1605012309-24812-1-git-send-email-anshuman.khandual@arm.com/
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Cc: Mike Leach <redacted>
Cc: Linu Cherian <redacted>
Cc: coresight@lists.linaro.org
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-kernel@vger.kernel.org
Anshuman Khandual (3):
coresight: core: Add support for dedicated percpu sinks
arm64: Add TRBE definitions
coresight: sink: Add TRBE driver
Suzuki K Poulose (11):
coresight: etm-perf: Allow an event to use different sinks
coresight: Do not scan for graph if none is present
coresight: etm4x: Add support for PE OS lock
coresight: ete: Add support for ETE sysreg access
coresight: ete: Add support for ETE tracing
dts: bindings: Document device tree bindings for ETE
coresight: etm-perf: Handle stale output handles
arm64: nvhe: Allow TRBE access at EL1
dts: bindings: Document device tree bindings for Arm TRBE
perf: aux: Add flags for the buffer format
coresight: etm-perf: Add support for trace buffer format
Documentation/devicetree/bindings/arm/ete.yaml | 74 ++
Documentation/devicetree/bindings/arm/trbe.yaml | 49 +
Documentation/trace/coresight/coresight-trbe.rst | 39 +
arch/arm64/include/asm/el2_setup.h | 19 +
arch/arm64/include/asm/kvm_arm.h | 2 +
arch/arm64/include/asm/sysreg.h | 51 +
drivers/hwtracing/coresight/Kconfig | 21 +-
drivers/hwtracing/coresight/Makefile | 1 +
drivers/hwtracing/coresight/coresight-core.c | 16 +-
drivers/hwtracing/coresight/coresight-etm-perf.c | 93 +-
drivers/hwtracing/coresight/coresight-etm4x-core.c | 138 ++-
.../hwtracing/coresight/coresight-etm4x-sysfs.c | 19 +-
drivers/hwtracing/coresight/coresight-etm4x.h | 81 +-
drivers/hwtracing/coresight/coresight-platform.c | 6 +
drivers/hwtracing/coresight/coresight-trbe.c | 1025 ++++++++++++++++++++
drivers/hwtracing/coresight/coresight-trbe.h | 160 +++
include/linux/coresight.h | 12 +
include/uapi/linux/perf_event.h | 13 +-
18 files changed, 1759 insertions(+), 60 deletions(-)
create mode 100644 Documentation/devicetree/bindings/arm/ete.yaml
create mode 100644 Documentation/devicetree/bindings/arm/trbe.yaml
create mode 100644 Documentation/trace/coresight/coresight-trbe.rst
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.c
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.h
--
2.7.4
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: Suzuki K Poulose <suzuki.poulose@arm.com>
Add support for handling the system registers for Embedded Trace
Extensions (ETE). ETE shares most of the registers with ETMv4 except
for some and also adds some new registers. Re-arrange the ETMv4x list
to share the common definitions and add the ETE sysreg support.
Cc: Mike Leach <redacted>
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
drivers/hwtracing/coresight/coresight-etm4x-core.c | 32 +++++++++++++
drivers/hwtracing/coresight/coresight-etm4x.h | 52 ++++++++++++++++++----
2 files changed, 75 insertions(+), 9 deletions(-)
@@ -160,10 +162,22 @@#define CASE_NOP(__unused, x) \case(x):/* fall through */+#define ETE_ONLY_SYSREG_LIST(op, val) \+CASE_##op((val),TRCRSR)\+CASE_##op((val),TRCEXTINSELRn(1))\+CASE_##op((val),TRCEXTINSELRn(2))\+CASE_##op((val),TRCEXTINSELRn(3))+/* List of registers accessible via System instructions */-#define ETM_SYSREG_LIST(op, val) \-CASE_##op((val),TRCPRGCTLR)\+#define ETM4x_ONLY_SYSREG_LIST(op, val) \CASE_##op((val),TRCPROCSELR)\+CASE_##op((val),TRCVDCTLR)\+CASE_##op((val),TRCVDSACCTLR)\+CASE_##op((val),TRCVDARCCTLR)\+CASE_##op((val),TRCOSLAR)++#define ETM_COMMON_SYSREG_LIST(op, val) \+CASE_##op((val),TRCPRGCTLR)\CASE_##op((val),TRCSTATR)\CASE_##op((val),TRCCONFIGR)\CASE_##op((val),TRCAUXCTLR)\
From: Suzuki K Poulose <suzuki.poulose@arm.com>
ETE may not implement the OS lock and instead could rely on
the PE OS Lock for the trace unit access. This is indicated
by the TRCOLSR.OSM == 0b100. Add support for handling the
PE OS lock
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
drivers/hwtracing/coresight/coresight-etm4x-core.c | 50 ++++++++++++++++++----
drivers/hwtracing/coresight/coresight-etm4x.h | 15 +++++++
2 files changed, 56 insertions(+), 9 deletions(-)
@@ -114,30 +114,59 @@ void etm4x_sysreg_write(u64 val, u32 offset, bool _relaxed, bool _64bit)}}-staticvoidetm4_os_unlock_csa(structetmv4_drvdata*drvdata,structcsdev_access*csa)+staticvoidetm_detect_os_lock(structetmv4_drvdata*drvdata,+structcsdev_access*csa){-/* Writing 0 to TRCOSLAR unlocks the trace registers */-etm4x_relaxed_write32(csa,0x0,TRCOSLAR);-drvdata->os_unlock=true;+u32oslsr=etm4x_relaxed_read32(csa,TRCOSLSR);++drvdata->os_lock_model=ETM_OSLSR_OSLM(oslsr);+}++staticvoidetm_write_os_lock(structetmv4_drvdata*drvdata,+structcsdev_access*csa,u32val)+{+val=!!val;++switch(drvdata->os_lock_model){+caseETM_OSLOCK_PRESENT:+etm4x_relaxed_write32(csa,val,TRCOSLAR);+break;+caseETM_OSLOCK_PE:+write_sysreg_s(val,SYS_OSLAR_EL1);+break;+default:+pr_warn_once("CPU%d: Unsupported Trace OSLock model: %x\n",+smp_processor_id(),drvdata->os_lock_model);+fallthrough;+caseETM_OSLOCK_NI:+return;+}isb();}+staticinlinevoidetm4_os_unlock_csa(structetmv4_drvdata*drvdata,+structcsdev_access*csa)+{+WARN_ON(drvdata->cpu!=smp_processor_id());++/* Writing 0 to OS Lock unlocks the trace unit registers */+etm_write_os_lock(drvdata,csa,0x0);+drvdata->os_unlock=true;+}+staticvoidetm4_os_unlock(structetmv4_drvdata*drvdata){if(!WARN_ON(!drvdata->csdev))etm4_os_unlock_csa(drvdata,&drvdata->csdev->access);-}staticvoidetm4_os_lock(structetmv4_drvdata*drvdata){if(WARN_ON(!drvdata->csdev))return;--/* Writing 0x1 to TRCOSLAR locks the trace registers */-etm4x_relaxed_write32(&drvdata->csdev->access,0x1,TRCOSLAR);+/* Writing 0x1 to OS Lock locks the trace registers */+etm_write_os_lock(drvdata,&drvdata->csdev->access,0x1);drvdata->os_unlock=false;-isb();}staticvoidetm4_cs_lock(structetmv4_drvdata*drvdata,
@@ -906,6 +935,9 @@ static void etm4_init_arch_data(void *info)if(!etm4_init_csdev_access(drvdata,csa))return;+/* Detect the support for OS Lock before we actuall use it */+etm_detect_os_lock(drvdata,csa);+/* Make sure all registers are accessible */etm4_os_unlock_csa(drvdata,csa);etm4_cs_unlock(drvdata,csa);
From: Suzuki K Poulose <suzuki.poulose@arm.com>
When there are multiple sinks on the system, in the absence
of a specified sink, it is quite possible that a default sink
for an ETM could be different from that of another ETM. However
we do not support having multiple sinks for an event yet. This
patch allows the event to use the default sinks on the ETMs
where they are scheduled as long as the sinks are of the same
type.
e.g, if we have 1x1 topology with per-CPU ETRs, the event can
use the per-CPU ETR for the session. However, if the sinks
are of different type, e.g TMC-ETR on one and a custom sink
on another, the event will only trace on the first detected
sink.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Tested-by: Linu Cherian <redacted>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
drivers/hwtracing/coresight/coresight-etm-perf.c | 48 +++++++++++++++++++-----
1 file changed, 38 insertions(+), 10 deletions(-)
From: Suzuki K Poulose <suzuki.poulose@arm.com>
If a graph node is not found for a given node, of_get_next_endpoint()
will emit the following error message :
OF: graph: no port node found in /<node_name>
If the given component doesn't have any explicit connections (e.g,
ETE) we could simply ignore the graph parsing.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
drivers/hwtracing/coresight/coresight-platform.c | 6 ++++++
1 file changed, 6 insertions(+)
Trace Buffer Extension (TRBE) implements a trace buffer per CPU which is
accessible via the system registers. The TRBE supports different addressing
modes including CPU virtual address and buffer modes including the circular
buffer mode. The TRBE buffer is addressed by a base pointer (TRBBASER_EL1),
an write pointer (TRBPTR_EL1) and a limit pointer (TRBLIMITR_EL1). But the
access to the trace buffer could be prohibited by a higher exception level
(EL3 or EL2), indicated by TRBIDR_EL1.P. The TRBE can also generate a CPU
private interrupt (PPI) on address translation errors and when the buffer
is full. Overall implementation here is inspired from the Arm SPE driver.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
Changes in V3:
- Added new DT bindings document TRBE.yaml
- Changed TRBLIMITR_TRIG_MODE_SHIFT from 2 to 3
- Dropped isb() from trbe_reset_local()
- Dropped gap between (void *) and buf->trbe_base
- Changed 'int' to 'unsigned int' in is_trbe_available()
- Dropped unused function set_trbe_running(), set_trbe_virtual_mode(),
set_trbe_enabled() and set_trbe_limit_pointer()
- Changed get_trbe_flag_update(), is_trbe_programmable() and
get_trbe_address_align() to accept TRBIDR value
- Changed is_trbe_running(), is_trbe_abort(), is_trbe_wrap(), is_trbe_trg(),
is_trbe_irq(), get_trbe_bsc() and get_trbe_ec() to accept TRBSR value
- Dropped snapshot mode condition in arm_trbe_alloc_buffer()
- Exit arm_trbe_init() when arm64_kernel_unmapped_at_el0() is enabled
- Compute trbe_limit before trbe_write to get the updated handle
- Added trbe_stop_and_truncate_event()
- Dropped trbe_handle_fatal()
Documentation/trace/coresight/coresight-trbe.rst | 39 +
arch/arm64/include/asm/sysreg.h | 1 +
drivers/hwtracing/coresight/Kconfig | 11 +
drivers/hwtracing/coresight/Makefile | 1 +
drivers/hwtracing/coresight/coresight-trbe.c | 1023 ++++++++++++++++++++++
drivers/hwtracing/coresight/coresight-trbe.h | 160 ++++
6 files changed, 1235 insertions(+)
create mode 100644 Documentation/trace/coresight/coresight-trbe.rst
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.c
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.h
@@ -0,0 +1,39 @@+.. SPDX-License-Identifier: GPL-2.0++==============================+Trace Buffer Extension (TRBE).+==============================++:Author: Anshuman Khandual <anshuman.khandual@arm.com>+:Date: November 2020++Hardware Description+--------------------++Trace Buffer Extension (TRBE) is a percpu hardware which captures in system+memory, CPU traces generated from a corresponding percpu tracing unit. This+gets plugged in as a coresight sink device because the corresponding trace+genarators (ETE), are plugged in as source device.++The TRBE is not compliant to CoreSight architecture specifications, but is+driven via the CoreSight driver framework to support the ETE (which is+CoreSight compliant) integration.++Sysfs files and directories+---------------------------++The TRBE devices appear on the existing coresight bus alongside the other+coresight devices::++ >$ ls /sys/bus/coresight/devices+ trbe0 trbe1 trbe2 trbe3++The ``trbe<N>`` named TRBEs are associated with a CPU.::++ >$ ls /sys/bus/coresight/devices/trbe0/+ align dbm++*Key file items are:-*+*``align``: TRBE write pointer alignment+*``dbm``: TRBE updates memory with access and dirty flags+
@@ -0,0 +1,1023 @@+// SPDX-License-Identifier: GPL-2.0+/*+*ThisdriverenablesTraceBufferExtension(TRBE)asaper-cpucoresight+*sinkdevicecouldthenpairwithanappropriateper-cpucoresightsource+*device(ETE)thusgeneratingrequiredtracedata.Tracecanbeenabled+*viatheperfframework.+*+*Copyright(C)2020ARMLtd.+*+*Author:AnshumanKhandual<anshuman.khandual@arm.com>+*/+#define DRVNAME "arm_trbe"++#define pr_fmt(fmt) DRVNAME ": " fmt++#include"coresight-trbe.h"++#define PERF_IDX2OFF(idx, buf) ((idx) % ((buf)->nr_pages << PAGE_SHIFT))++/*+*Apaddingpacketthatwillhelptheuserspacetools+*inskippingrelevantsectionsinthecapturedtrace+*datawhichcouldnotbedecoded.TRBEdoesn'tsupport+*formattingthetracedata,unlikethelegacyCoreSight+*sinksandthusweuseETEtracepacketstopadthe+*sectionsofthebuffer.+*/+#define ETE_IGNORE_PACKET 0x70++/*+*Minimumamountofmeaningfultracewillcontain:+*A-Sync,TraceInfo,TraceOn,Address,Atom.+*Thisisabout44bytesofETEtrace.Tobeon+*thesaferside,weassume64bytesistheminimum+*spacerequiredforameaningfulsession,before+*wehita"WRAP"event.+*/+#define TRBE_TRACE_MIN_BUF_SIZE 64++enumtrbe_fault_action{+TRBE_FAULT_ACT_WRAP,+TRBE_FAULT_ACT_SPURIOUS,+TRBE_FAULT_ACT_FATAL,+};++structtrbe_buf{+unsignedlongtrbe_base;+unsignedlongtrbe_limit;+unsignedlongtrbe_write;+intnr_pages;+void**pages;+boolsnapshot;+structtrbe_cpudata*cpudata;+};++structtrbe_cpudata{+booltrbe_dbm;+u64trbe_align;+intcpu;+enumcs_modemode;+structtrbe_buf*buf;+structtrbe_drvdata*drvdata;+};++structtrbe_drvdata{+structtrbe_cpudata__percpu*cpudata;+structperf_output_handle__percpu**handle;+structhlist_nodehotplug_node;+intirq;+cpumask_tsupported_cpus;+enumcpuhp_statetrbe_online;+structplatform_device*pdev;+};++staticinttrbe_alloc_node(structperf_event*event)+{+if(event->cpu==-1)+returnNUMA_NO_NODE;+returncpu_to_node(event->cpu);+}++staticvoidtrbe_drain_buffer(void)+{+asm(TSB_CSYNC);+dsb(nsh);+}++staticvoidtrbe_drain_and_disable_local(void)+{+trbe_drain_buffer();+write_sysreg_s(0,SYS_TRBLIMITR_EL1);+isb();+}++staticvoidtrbe_reset_local(void)+{+trbe_drain_and_disable_local();+write_sysreg_s(0,SYS_TRBPTR_EL1);+write_sysreg_s(0,SYS_TRBBASER_EL1);+write_sysreg_s(0,SYS_TRBSR_EL1);+}++staticvoidtrbe_stop_and_truncate_event(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);++/*+*Wecannotproceedwiththebuffercollectionandwe+*donothaveanydataforthecurrentsession.The+*etm_perfdriverexpectstocloseouttheaux_buffer+*atevent_stop().SodisabletheTRBEhereandleave+*theupdate_buffer()toreturna0size.+*/+trbe_drain_and_disable_local();+perf_aux_output_flag(handle,PERF_AUX_FLAG_TRUNCATED);+*this_cpu_ptr(buf->cpudata->drvdata->handle)=NULL;+}++/*+*TRBEBufferManagement+*+*TheTRBEbufferspansfromthebasepointertillthelimitpointer.Whenenabled,+*itstartswritingtracedatafromthewritepointeronwardtillthelimitpointer.+*Whenthewritepointerreachestheaddressjustbeforethelimitpointer,itgets+*wrappedaroundagaintothebasepointer.ThisiscalledaTRBEwrapevent,which+*generatesamaintenanceinterruptwhenoperatedinWRAPorFILLmode.Thisdriver+*usesFILLmode,wheretheTRBEstopsthetracecollectionatwrapevent.TheIRQ+*handlerupdatestheAUXbufferandre-enablestheTRBEwithupdatedWRITEand+*LIMITpointers.+*+*WraparoundwithanIRQ+*------<------<-------<-----<-----+*||+*------>------>------->----->-----+*+*+---------------+-----------------------++*|||+*+---------------+-----------------------++*BasePointerWritePointerLimitPointer+*+*ThebaseandlimitpointersalwaysneedstobePAGE_SIZEaligned.Butthewrite+*pointercanbealignedtotheimplementationdefinedTRBEtracebufferalignment+*ascapturedintrbe_cpudata->trbe_align.+*+*+*headtailwakeup+*+---------------------------------------+-----~~------+*|$$$$$$$|################|$$$$$$$$$$$$$$||+*+---------------------------------------+-----~~------+*BasePointerWritePointerLimitPointer+*+*Theperf_output_handleindices(head,tail,wakeup)aremonotonicallyincreasing+*valueswhichtracksallthedriverwritesanduserreadsfromtheperfauxiliary+*buffer.Generally[head..tail]istheareawherethedrivercanwriteintounless+*thewakeupisbehindthetail.EnabledTRBEbufferspanneedstobeadjustedand+*configureddependingontheperf_output_handleindices,sothatthedriverdoes+*notoverrideintoareasintheperfauxiliarybufferwhichisbeingoryettobe+*consumedfromtheuserspace.TheenabledTRBEbufferareaisamovingsubsetof+*theallocatedperfauxiliarybuffer.+*/+staticvoidtrbe_pad_buf(structperf_output_handle*handle,intlen)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);+u64head=PERF_IDX2OFF(handle->head,buf);++memset((void*)buf->trbe_base+head,ETE_IGNORE_PACKET,len);+if(!buf->snapshot)+perf_aux_output_skip(handle,len);+}++staticunsignedlongtrbe_snapshot_offset(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);++/*+*TheETEtracehasalignmentsynchronizationpacketsallowing+*thedecodertoresetincaseofanoverfloworcorruption.+*Sowecanusetheentirebufferforthesnapshotmode.+*/+returnbuf->nr_pages*PAGE_SIZE;+}++/*+*TRBELimitCalculation+*+*ThefollowingmarkersareusedtoillustratevariousTRBEbuffersituations.+*+*$$$$-Dataarea,unconsumedcapturedtracedata,nottobeoverridden+*####-Freearea,enabled,tracewillbewritten+*%%%%-Freearea,disabled,tracewillnotbewritten+*====-Freearea,paddedwithETE_IGNORE_PACKET,tracewillbeskipped+*/+staticunsignedlong__trbe_normal_offset(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);+structtrbe_cpudata*cpudata=buf->cpudata;+constu64bufsize=buf->nr_pages*PAGE_SIZE;+u64limit=bufsize;+u64head,tail,wakeup;++head=PERF_IDX2OFF(handle->head,buf);++/*+*head+*------->|+*|+*headTRBEaligntail+*+----|-------|---------------|-------++*|$$$$|=======|###############|$$$$$$$|+*+----|-------|---------------|-------++*trbe_basetrbe_base+nr_pages+*+*Perfauxbufferoutputheadpositioncanbemisaligneddependingon+*variousfactorsincludinguserspacereads.Incasemisaligned,head+*needstobealignedbeforeTRBEcanbeconfigured.Padthealignment+*gapwithETE_IGNORE_PACKETbytesthatwillbeignoredbyusertools+*andskipthissectionthusadvancingthehead.+*/+if(!IS_ALIGNED(head,cpudata->trbe_align)){+unsignedlongdelta=roundup(head,cpudata->trbe_align)-head;++delta=min(delta,handle->size);+trbe_pad_buf(handle,delta);+head=PERF_IDX2OFF(handle->head,buf);+}++/*+*head=tail(size=0)+*+----|-------------------------------++*|$$$$|$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$|+*+----|-------------------------------++*trbe_basetrbe_base+nr_pages+*+*Perfauxbufferdoesnothaveanyspaceforthedrivertowriteinto.+*Justcommunicatetracetruncationeventtotheuserspacebymarking+*itwithPERF_AUX_FLAG_TRUNCATED.+*/+if(!handle->size){+perf_aux_output_flag(handle,PERF_AUX_FLAG_TRUNCATED);+return0;+}++/* Compute the tail and wakeup indices now that we've aligned head */+tail=PERF_IDX2OFF(handle->head+handle->size,buf);+wakeup=PERF_IDX2OFF(handle->wakeup,buf);++/*+*LetscalculatethebufferareawhichTRBEcouldwriteinto.There+*arethreepossiblescenarioshere.Limitneedstobealignedwith+*PAGE_SIZEpertheTRBErequirement.Alwaysavoidclobberingthe+*unconsumeddata.+*+*1)head<tail+*+*headtail+*+----|-----------------------|-------++*|$$$$|#######################|$$$$$$$|+*+----|-----------------------|-------++*trbe_baselimittrbe_base+nr_pages+*+*TRBEcouldwriteinto[head..tail]area.Unlessthetailisrightat+*theendofthebuffer,neitheranwraparoundnoranIRQisexpected+*whilebeingenabled.+*+*2)head==tail+*+*head=tail(size>0)+*+----|-------------------------------++*|%%%%|###############################|+*+----|-------------------------------++*trbe_baselimit=trbe_base+nr_pages+*+*TRBEshouldjustwriteinto[head..base+nr_pages]areaeventhough+*theentirebufferisempty.Reasonbeing,whenthetracereachesthe+*endofthebuffer,itwilljustwraparoundwithanIRQgivingan+*opportunitytoreconfigurethebuffer.+*+*3)tail<head+*+*tailhead+*+----|-----------------------|-------++*|%%%%|$$$$$$$$$$$$$$$$$$$$$$$|#######|+*+----|-----------------------|-------++*trbe_baselimit=trbe_base+nr_pages+*+*TRBEshouldjustwriteinto[head..base+nr_pages]areaeventhough+*the[trbe_base..tail]isalsoempty.Reasonbeing,whenthetrace+*reachestheendofthebuffer,itwilljustwraparoundwithanIRQ+*givinganopportunitytoreconfigurethebuffer.+*/+if(head<tail)+limit=round_down(tail,PAGE_SIZE);++/*+*Wakeupmaybearbitrarilyfarintothefuture.Ifit'snotinthe+*currentgeneration,eitherwe'llwrapbeforehittingit,orit's+*inthepastandhasbeenhandledalready.+*+*Ifthere'sawakeupbeforewewrap,arrangetobewokenupbythe+*pageboundaryfollowingit.Keepthetailboundaryifthat'slower.+*+*headwakeuptail+*+----|---------------|-------|-------++*|$$$$|###############|%%%%%%%|$$$$$$$|+*+----|---------------|-------|-------++*trbe_baselimittrbe_base+nr_pages+*/+if(handle->wakeup<(handle->head+handle->size)&&head<=wakeup)+limit=min(limit,round_up(wakeup,PAGE_SIZE));++/*+*Therearetwosituationwhenthiscanhappeni.elimitisbefore+*theheadandhenceTRBEcannotbeconfigured.+*+*1)head<tail(aligneddownwithPAGE_SIZE)andalsotheyareboth+*withinthesamePAGEsizerange.+*+*PAGE_SIZE+*|----------------------|+*+*limitheadtail+*+------------|------|--------|-------++*|$$$$$$$$$$$$$$$$$$$|========|$$$$$$$|+*+------------|------|--------|-------++*trbe_basetrbe_base+nr_pages+*+*2)head<wakeup(alignedupwithPAGE_SIZE)<tailandalsoboth+*headandwakeuparewithinsamePAGEsizerange.+*+*PAGE_SIZE+*|----------------------|+*+*limitheadwakeuptail+*+----|------|-------|--------|-------++*|$$$$$$$$$$$|=======|========|$$$$$$$|+*+----|------|-------|--------|-------++*trbe_basetrbe_base+nr_pages+*/+if(limit>head)+returnlimit;++trbe_pad_buf(handle,handle->size);+perf_aux_output_flag(handle,PERF_AUX_FLAG_TRUNCATED);+return0;+}++staticunsignedlongtrbe_normal_offset(structperf_output_handle*handle)+{+structtrbe_buf*buf=perf_get_aux(handle);+u64limit=__trbe_normal_offset(handle);+u64head=PERF_IDX2OFF(handle->head,buf);++/*+*Iftheheadistooclosetothelimitandwedon't+*havespaceforameaningfulrun,weratherpadit+*andstartfresh.+*/+if(limit&&(limit-head<TRBE_TRACE_MIN_BUF_SIZE)){+trbe_pad_buf(handle,limit-head);+limit=__trbe_normal_offset(handle);+}+returnlimit;+}++staticunsignedlongcompute_trbe_buffer_limit(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);+unsignedlongoffset;++if(buf->snapshot)+offset=trbe_snapshot_offset(handle);+else+offset=trbe_normal_offset(handle);+returnbuf->trbe_base+offset;+}++staticvoidclr_trbe_status(void)+{+u64trbsr=read_sysreg_s(SYS_TRBSR_EL1);++WARN_ON(is_trbe_enabled());+trbsr&=~TRBSR_IRQ;+trbsr&=~TRBSR_TRG;+trbsr&=~TRBSR_WRAP;+trbsr&=~(TRBSR_EC_MASK<<TRBSR_EC_SHIFT);+trbsr&=~(TRBSR_BSC_MASK<<TRBSR_BSC_SHIFT);+trbsr&=~TRBSR_STOP;+write_sysreg_s(trbsr,SYS_TRBSR_EL1);+}++staticvoidset_trbe_limit_pointer_enabled(unsignedlongaddr)+{+u64trblimitr=read_sysreg_s(SYS_TRBLIMITR_EL1);++WARN_ON(!IS_ALIGNED(addr,(1UL<<TRBLIMITR_LIMIT_SHIFT)));+WARN_ON(!IS_ALIGNED(addr,PAGE_SIZE));++trblimitr&=~TRBLIMITR_NVM;+trblimitr&=~(TRBLIMITR_FILL_MODE_MASK<<TRBLIMITR_FILL_MODE_SHIFT);+trblimitr&=~(TRBLIMITR_TRIG_MODE_MASK<<TRBLIMITR_TRIG_MODE_SHIFT);+trblimitr&=~(TRBLIMITR_LIMIT_MASK<<TRBLIMITR_LIMIT_SHIFT);++/*+*Filltracebuffermodeisusedherewhileconfiguringthe+*TRBEfortracecapture.Inthisparticularmode,thetrace+*collectionisstoppedandamaintenanceinterruptisraised+*whenthecurrentwritepointerwraps.Thispauseintrace+*collectiongivesthesoftwareanopportunitytocapturethe+*tracedataintheinterrupthandler,beforereconfiguring+*theTRBE.+*/+trblimitr|=(TRBE_FILL_MODE_FILL&TRBLIMITR_FILL_MODE_MASK)<<TRBLIMITR_FILL_MODE_SHIFT;++/*+*TriggermodeisnotusedherewhileconfiguringtheTRBEfor+*thetracecapture.Hencejustkeepthisintheignoremode.+*/+trblimitr|=(TRBE_TRIG_MODE_IGNORE&TRBLIMITR_TRIG_MODE_MASK)<<TRBLIMITR_TRIG_MODE_SHIFT;+trblimitr|=(addr&PAGE_MASK);++trblimitr|=TRBLIMITR_ENABLE;+write_sysreg_s(trblimitr,SYS_TRBLIMITR_EL1);+}++staticvoidtrbe_enable_hw(structtrbe_buf*buf)+{+WARN_ON(buf->trbe_write<buf->trbe_base);+WARN_ON(buf->trbe_write>=buf->trbe_limit);+set_trbe_disabled();+isb();+clr_trbe_status();+set_trbe_base_pointer(buf->trbe_base);+set_trbe_write_pointer(buf->trbe_write);++/*+*Synchronizealltheregisterupdates+*tillnowbeforeenablingtheTRBE.+*/+isb();+set_trbe_limit_pointer_enabled(buf->trbe_limit);++/* Synchronize the TRBE enable event */+isb();+}++staticvoid*arm_trbe_alloc_buffer(structcoresight_device*csdev,+structperf_event*event,void**pages,+intnr_pages,boolsnapshot)+{+structtrbe_buf*buf;+structpage**pglist;+inti;++/*+*TRBELIMITandTRBEWRITEpointersmustbepagealigned.Butwith+*justasinglepage,thereisnotmuchroomleftwhilewritinginto+*apartiallyfilledTRBEbuffer.Hencerestricttheminimumbuffer+*sizeastwopages.+*/+if(nr_pages<2)+returnNULL;++buf=kzalloc_node(sizeof(*buf),GFP_KERNEL,trbe_alloc_node(event));+if(IS_ERR(buf))+returnERR_PTR(-ENOMEM);++pglist=kcalloc(nr_pages,sizeof(*pglist),GFP_KERNEL);+if(IS_ERR(pglist)){+kfree(buf);+returnERR_PTR(-ENOMEM);+}++for(i=0;i<nr_pages;i++)+pglist[i]=virt_to_page(pages[i]);++buf->trbe_base=(unsignedlong)vmap(pglist,nr_pages,VM_MAP,PAGE_KERNEL);+if(IS_ERR((void*)buf->trbe_base)){+kfree(pglist);+kfree(buf);+returnERR_PTR(buf->trbe_base);+}+buf->trbe_limit=buf->trbe_base+nr_pages*PAGE_SIZE;+buf->trbe_write=buf->trbe_base;+buf->snapshot=snapshot;+buf->nr_pages=nr_pages;+buf->pages=pages;+kfree(pglist);+returnbuf;+}++voidarm_trbe_free_buffer(void*config)+{+structtrbe_buf*buf=config;++vunmap((void*)buf->trbe_base);+kfree(buf);+}++staticunsignedlongarm_trbe_update_buffer(structcoresight_device*csdev,+structperf_output_handle*handle,+void*config)+{+structtrbe_drvdata*drvdata=dev_get_drvdata(csdev->dev.parent);+structtrbe_cpudata*cpudata=dev_get_drvdata(&csdev->dev);+structtrbe_buf*buf=config;+unsignedlongsize,offset;++WARN_ON(buf->cpudata!=cpudata);+WARN_ON(cpudata->cpu!=smp_processor_id());+WARN_ON(cpudata->drvdata!=drvdata);+if(cpudata->mode!=CS_MODE_PERF)+return-EINVAL;++/*+*IftheTRBEwasdisabledduetolackofspaceintheAUXbufferora+*spuriousfault,thedriverleavesitdisabled,truncatingthebuffer.+*Sincetheetm_perfdriverexpectstocloseouttheAUXbuffer,the+*driverskipsit.Thus,justpassin0sizeheretoindicatethatthe+*bufferwastruncated.+*/+if(!is_trbe_enabled())+return0;+/*+*perfhandlestructureneedstobesharedwiththeTRBEIRQhandlerfor+*capturingtracedataandrestartingthehandle.Thereisaprobability+*ofanundefinedreferencebasedcrashwhenetmeventisbeingstopped+*whileaTRBEIRQalsogettingprocessed.Thishappensduetherelease+*ofperfhandleviaperf_aux_output_end()inetm_event_stop().Stopping+*theTRBEherewillensurethatnoIRQcouldbegeneratedwhentheperf+*handlegetsfreedinetm_event_stop().+*/+trbe_drain_and_disable_local();+offset=get_trbe_write_pointer()-get_trbe_base_pointer();+size=offset-PERF_IDX2OFF(handle->head,buf);+if(buf->snapshot)+handle->head+=size;+returnsize;+}++staticintarm_trbe_enable(structcoresight_device*csdev,u32mode,void*data)+{+structtrbe_drvdata*drvdata=dev_get_drvdata(csdev->dev.parent);+structtrbe_cpudata*cpudata=dev_get_drvdata(&csdev->dev);+structperf_output_handle*handle=data;+structtrbe_buf*buf=etm_perf_sink_config(handle);++WARN_ON(cpudata->cpu!=smp_processor_id());+WARN_ON(cpudata->drvdata!=drvdata);+if(mode!=CS_MODE_PERF)+return-EINVAL;++*this_cpu_ptr(drvdata->handle)=handle;+cpudata->buf=buf;+cpudata->mode=mode;+buf->cpudata=cpudata;+buf->trbe_limit=compute_trbe_buffer_limit(handle);+buf->trbe_write=buf->trbe_base+PERF_IDX2OFF(handle->head,buf);+if(buf->trbe_limit==buf->trbe_base){+trbe_stop_and_truncate_event(handle);+return0;+}+trbe_enable_hw(buf);+return0;+}++staticintarm_trbe_disable(structcoresight_device*csdev)+{+structtrbe_drvdata*drvdata=dev_get_drvdata(csdev->dev.parent);+structtrbe_cpudata*cpudata=dev_get_drvdata(&csdev->dev);+structtrbe_buf*buf=cpudata->buf;++WARN_ON(buf->cpudata!=cpudata);+WARN_ON(cpudata->cpu!=smp_processor_id());+WARN_ON(cpudata->drvdata!=drvdata);+if(cpudata->mode!=CS_MODE_PERF)+return-EINVAL;++trbe_drain_and_disable_local();+buf->cpudata=NULL;+cpudata->buf=NULL;+cpudata->mode=CS_MODE_DISABLED;+return0;+}++staticvoidtrbe_handle_spurious(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);++buf->trbe_limit=compute_trbe_buffer_limit(handle);+buf->trbe_write=buf->trbe_base+PERF_IDX2OFF(handle->head,buf);+if(buf->trbe_limit==buf->trbe_base){+trbe_drain_and_disable_local();+return;+}+trbe_enable_hw(buf);+}++staticvoidtrbe_handle_overflow(structperf_output_handle*handle)+{+structperf_event*event=handle->event;+structtrbe_buf*buf=etm_perf_sink_config(handle);+unsignedlongoffset,size;+structetm_event_data*event_data;++offset=get_trbe_limit_pointer()-get_trbe_base_pointer();+size=offset-PERF_IDX2OFF(handle->head,buf);+if(buf->snapshot)+handle->head=offset;+perf_aux_output_end(handle,size);++event_data=perf_aux_output_begin(handle,event);+if(!event_data){+trbe_drain_and_disable_local();+*this_cpu_ptr(buf->cpudata->drvdata->handle)=NULL;+return;+}+buf->trbe_limit=compute_trbe_buffer_limit(handle);+buf->trbe_write=buf->trbe_base+PERF_IDX2OFF(handle->head,buf);+if(buf->trbe_limit==buf->trbe_base){+trbe_stop_and_truncate_event(handle);+return;+}+*this_cpu_ptr(buf->cpudata->drvdata->handle)=handle;+trbe_enable_hw(buf);+}++staticboolis_perf_trbe(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);+structtrbe_cpudata*cpudata=buf->cpudata;+structtrbe_drvdata*drvdata=cpudata->drvdata;+intcpu=smp_processor_id();++WARN_ON(buf->trbe_base!=get_trbe_base_pointer());+WARN_ON(buf->trbe_limit!=get_trbe_limit_pointer());++if(cpudata->mode!=CS_MODE_PERF)+returnfalse;++if(cpudata->cpu!=cpu)+returnfalse;++if(!cpumask_test_cpu(cpu,&drvdata->supported_cpus))+returnfalse;++returntrue;+}++staticenumtrbe_fault_actiontrbe_get_fault_act(structperf_output_handle*handle)+{+u64trbsr=read_sysreg_s(SYS_TRBSR_EL1);+intec=get_trbe_ec(trbsr);+intbsc=get_trbe_bsc(trbsr);++WARN_ON(is_trbe_running(trbsr));+if(is_trbe_trg(trbsr)||is_trbe_abort(trbsr))+returnTRBE_FAULT_ACT_FATAL;++if((ec==TRBE_EC_STAGE1_ABORT)||(ec==TRBE_EC_STAGE2_ABORT))+returnTRBE_FAULT_ACT_FATAL;++if(is_trbe_wrap(trbsr)&&(ec==TRBE_EC_OTHERS)&&(bsc==TRBE_BSC_FILLED)){+if(get_trbe_write_pointer()==get_trbe_base_pointer())+returnTRBE_FAULT_ACT_WRAP;+}+returnTRBE_FAULT_ACT_SPURIOUS;+}++staticirqreturn_tarm_trbe_irq_handler(intirq,void*dev)+{+structperf_output_handle**handle_ptr=dev;+structperf_output_handle*handle=*handle_ptr;+enumtrbe_fault_actionact;++WARN_ON(!is_trbe_irq(read_sysreg_s(SYS_TRBSR_EL1)));+clr_trbe_irq();++/*+*EnsurethetraceisvisibletotheCPUsand+*anyexternalabortshavebeenresolved.+*/+trbe_drain_buffer();+isb();++if(!perf_get_aux(handle))+returnIRQ_NONE;++if(!is_perf_trbe(handle))+returnIRQ_NONE;++irq_work_run();++act=trbe_get_fault_act(handle);+switch(act){+caseTRBE_FAULT_ACT_WRAP:+trbe_handle_overflow(handle);+break;+caseTRBE_FAULT_ACT_SPURIOUS:+trbe_handle_spurious(handle);+break;+caseTRBE_FAULT_ACT_FATAL:+trbe_stop_and_truncate_event(handle);+break;+}+returnIRQ_HANDLED;+}++staticconststructcoresight_ops_sinkarm_trbe_sink_ops={+.enable=arm_trbe_enable,+.disable=arm_trbe_disable,+.alloc_buffer=arm_trbe_alloc_buffer,+.free_buffer=arm_trbe_free_buffer,+.update_buffer=arm_trbe_update_buffer,+};++staticconststructcoresight_opsarm_trbe_cs_ops={+.sink_ops=&arm_trbe_sink_ops,+};++staticssize_talign_show(structdevice*dev,structdevice_attribute*attr,char*buf)+{+structtrbe_cpudata*cpudata=dev_get_drvdata(dev);++returnsprintf(buf,"%llx\n",cpudata->trbe_align);+}+staticDEVICE_ATTR_RO(align);++staticssize_tdbm_show(structdevice*dev,structdevice_attribute*attr,char*buf)+{+structtrbe_cpudata*cpudata=dev_get_drvdata(dev);++returnsprintf(buf,"%d\n",cpudata->trbe_dbm);+}+staticDEVICE_ATTR_RO(dbm);++staticstructattribute*arm_trbe_attrs[]={+&dev_attr_align.attr,+&dev_attr_dbm.attr,+NULL,+};++staticconststructattribute_grouparm_trbe_group={+.attrs=arm_trbe_attrs,+};++staticconststructattribute_group*arm_trbe_groups[]={+&arm_trbe_group,+NULL,+};++staticvoidarm_trbe_probe_coresight_cpu(void*info)+{+structtrbe_drvdata*drvdata=info;+structcoresight_descdesc={0};+intcpu=smp_processor_id();+structtrbe_cpudata*cpudata=per_cpu_ptr(drvdata->cpudata,cpu);+structcoresight_device*trbe_csdev=per_cpu(csdev_sink,cpu);+u64trbidr=read_sysreg_s(SYS_TRBIDR_EL1);+structdevice*dev;++if(WARN_ON(!cpudata))+gotocpu_clear;++if(trbe_csdev)+return;++cpudata->cpu=smp_processor_id();+cpudata->drvdata=drvdata;+dev=&cpudata->drvdata->pdev->dev;++if(!is_trbe_available()){+pr_err("TRBE is not implemented on cpu %d\n",cpudata->cpu);+gotocpu_clear;+}++if(!is_trbe_programmable(trbidr)){+pr_err("TRBE is owned in higher exception level on cpu %d\n",cpudata->cpu);+gotocpu_clear;+}+desc.name=devm_kasprintf(dev,GFP_KERNEL,"%s%d",DRVNAME,smp_processor_id());+if(IS_ERR(desc.name))+gotocpu_clear;++desc.type=CORESIGHT_DEV_TYPE_SINK;+desc.subtype.sink_subtype=CORESIGHT_DEV_SUBTYPE_SINK_PERCPU_SYSMEM;+desc.ops=&arm_trbe_cs_ops;+desc.pdata=dev_get_platdata(dev);+desc.groups=arm_trbe_groups;+desc.dev=dev;+trbe_csdev=coresight_register(&desc);+if(IS_ERR(trbe_csdev))+gotocpu_clear;++dev_set_drvdata(&trbe_csdev->dev,cpudata);+cpudata->trbe_dbm=get_trbe_flag_update(trbidr);+cpudata->trbe_align=1ULL<<get_trbe_address_align(trbidr);+if(cpudata->trbe_align>SZ_2K){+pr_err("Unsupported alignment on cpu %d\n",cpudata->cpu);+gotocpu_clear;+}+per_cpu(csdev_sink,cpu)=trbe_csdev;+trbe_reset_local();+enable_percpu_irq(drvdata->irq,IRQ_TYPE_NONE);+return;+cpu_clear:+cpumask_clear_cpu(cpudata->cpu,&cpudata->drvdata->supported_cpus);+}++staticvoidarm_trbe_remove_coresight_cpu(void*info)+{+intcpu=smp_processor_id();+structtrbe_drvdata*drvdata=info;+structtrbe_cpudata*cpudata=per_cpu_ptr(drvdata->cpudata,cpu);+structcoresight_device*trbe_csdev=per_cpu(csdev_sink,cpu);++if(trbe_csdev){+coresight_unregister(trbe_csdev);+cpudata->drvdata=NULL;+per_cpu(csdev_sink,cpu)=NULL;+}+disable_percpu_irq(drvdata->irq);+trbe_reset_local();+}++staticintarm_trbe_probe_coresight(structtrbe_drvdata*drvdata)+{+drvdata->cpudata=alloc_percpu(typeof(*drvdata->cpudata));+if(IS_ERR(drvdata->cpudata))+returnPTR_ERR(drvdata->cpudata);++arm_trbe_probe_coresight_cpu(drvdata);+smp_call_function_many(&drvdata->supported_cpus,arm_trbe_probe_coresight_cpu,drvdata,1);+return0;+}++staticintarm_trbe_remove_coresight(structtrbe_drvdata*drvdata)+{+arm_trbe_remove_coresight_cpu(drvdata);+smp_call_function_many(&drvdata->supported_cpus,arm_trbe_remove_coresight_cpu,drvdata,1);+free_percpu(drvdata->cpudata);+return0;+}++staticintarm_trbe_cpu_startup(unsignedintcpu,structhlist_node*node)+{+structtrbe_drvdata*drvdata=hlist_entry_safe(node,structtrbe_drvdata,hotplug_node);++if(cpumask_test_cpu(cpu,&drvdata->supported_cpus)){+if(!per_cpu(csdev_sink,cpu)){+arm_trbe_probe_coresight_cpu(drvdata);+}else{+trbe_reset_local();+enable_percpu_irq(drvdata->irq,IRQ_TYPE_NONE);+}+}+return0;+}++staticintarm_trbe_cpu_teardown(unsignedintcpu,structhlist_node*node)+{+structtrbe_drvdata*drvdata=hlist_entry_safe(node,structtrbe_drvdata,hotplug_node);++if(cpumask_test_cpu(cpu,&drvdata->supported_cpus)){+disable_percpu_irq(drvdata->irq);+trbe_reset_local();+}+return0;+}++staticintarm_trbe_probe_cpuhp(structtrbe_drvdata*drvdata)+{+enumcpuhp_statetrbe_online;++trbe_online=cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,DRVNAME,+arm_trbe_cpu_startup,arm_trbe_cpu_teardown);+if(trbe_online<0)+return-EINVAL;++if(cpuhp_state_add_instance(trbe_online,&drvdata->hotplug_node))+return-EINVAL;++drvdata->trbe_online=trbe_online;+return0;+}++staticvoidarm_trbe_remove_cpuhp(structtrbe_drvdata*drvdata)+{+cpuhp_remove_multi_state(drvdata->trbe_online);+}++staticintarm_trbe_probe_irq(structplatform_device*pdev,+structtrbe_drvdata*drvdata)+{+drvdata->irq=platform_get_irq(pdev,0);+if(!drvdata->irq){+pr_err("IRQ not found for the platform device\n");+return-ENXIO;+}++if(!irq_is_percpu(drvdata->irq)){+pr_err("IRQ is not a PPI\n");+return-EINVAL;+}++if(irq_get_percpu_devid_partition(drvdata->irq,&drvdata->supported_cpus))+return-EINVAL;++drvdata->handle=alloc_percpu(typeof(*drvdata->handle));+if(!drvdata->handle)+return-ENOMEM;++if(request_percpu_irq(drvdata->irq,arm_trbe_irq_handler,DRVNAME,drvdata->handle)){+free_percpu(drvdata->handle);+return-EINVAL;+}+return0;+}++staticvoidarm_trbe_remove_irq(structtrbe_drvdata*drvdata)+{+free_percpu_irq(drvdata->irq,drvdata->handle);+free_percpu(drvdata->handle);+}++staticintarm_trbe_device_probe(structplatform_device*pdev)+{+structcoresight_platform_data*pdata;+structtrbe_drvdata*drvdata;+structdevice*dev=&pdev->dev;+intret;++drvdata=devm_kzalloc(dev,sizeof(*drvdata),GFP_KERNEL);+if(IS_ERR(drvdata))+return-ENOMEM;++pdata=coresight_get_platform_data(dev);+if(IS_ERR(pdata)){+kfree(drvdata);+return-ENOMEM;+}++dev_set_drvdata(dev,drvdata);+dev->platform_data=pdata;+drvdata->pdev=pdev;+ret=arm_trbe_probe_irq(pdev,drvdata);+if(ret)+gotoirq_failed;++ret=arm_trbe_probe_coresight(drvdata);+if(ret)+gotoprobe_failed;++ret=arm_trbe_probe_cpuhp(drvdata);+if(ret)+gotocpuhp_failed;++return0;+cpuhp_failed:+arm_trbe_remove_coresight(drvdata);+probe_failed:+arm_trbe_remove_irq(drvdata);+irq_failed:+kfree(pdata);+kfree(drvdata);+returnret;+}++staticintarm_trbe_device_remove(structplatform_device*pdev)+{+structcoresight_platform_data*pdata=dev_get_platdata(&pdev->dev);+structtrbe_drvdata*drvdata=platform_get_drvdata(pdev);++arm_trbe_remove_coresight(drvdata);+arm_trbe_remove_cpuhp(drvdata);+arm_trbe_remove_irq(drvdata);+kfree(pdata);+kfree(drvdata);+return0;+}++staticconststructof_device_idarm_trbe_of_match[]={+{.compatible="arm,trace-buffer-extension"},+{},+};+MODULE_DEVICE_TABLE(of,arm_trbe_of_match);++staticstructplatform_driverarm_trbe_driver={+.driver={+.name=DRVNAME,+.of_match_table=of_match_ptr(arm_trbe_of_match),+.suppress_bind_attrs=true,+},+.probe=arm_trbe_device_probe,+.remove=arm_trbe_device_remove,+};++staticint__initarm_trbe_init(void)+{+intret;++if(arm64_kernel_unmapped_at_el0()){+pr_err("TRBE wouldn't work if kernel gets unmapped at EL0\n");+return-EOPNOTSUPP;+}++ret=platform_driver_register(&arm_trbe_driver);+if(!ret)+return0;++pr_err("Error registering %s platform driver\n",DRVNAME);+returnret;+}++staticvoid__exitarm_trbe_exit(void)+{+platform_driver_unregister(&arm_trbe_driver);+}+module_init(arm_trbe_init);+module_exit(arm_trbe_exit);++MODULE_AUTHOR("Anshuman Khandual <anshuman.khandual@arm.com>");+MODULE_DESCRIPTION("Arm Trace Buffer Extension (TRBE) driver");+MODULE_LICENSE("GPL v2");
From: Suzuki K Poulose <suzuki.poulose@arm.com>
Add ETE as one of the supported device types we support
with ETM4x driver. The devices are named following the
existing convention as ete<N>.
ETE mandates that the trace resource status register is programmed
before the tracing is turned on. For the moment simply write to
it indicating TraceActive.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
drivers/hwtracing/coresight/Kconfig | 10 ++--
drivers/hwtracing/coresight/coresight-etm4x-core.c | 56 +++++++++++++++++-----
.../hwtracing/coresight/coresight-etm4x-sysfs.c | 19 ++++++--
drivers/hwtracing/coresight/coresight-etm4x.h | 16 ++++++-
4 files changed, 79 insertions(+), 22 deletions(-)
@@ -431,6 +431,13 @@ static int etm4_enable_hw(struct etmv4_drvdata *drvdata)etm4x_relaxed_write32(csa,trcpdcr|TRCPDCR_PU,TRCPDCR);}+/*+*ETEmandatesthattheTRCRSRiswrittentobefore+*enablingit.+*/+if(etm4x_is_ete(drvdata))+etm4x_relaxed_write32(csa,TRCRSR_TA,TRCRSR);+/* Enable the trace unit */etm4x_relaxed_write32(csa,1,TRCPRGCTLR);
@@ -1853,6 +1869,20 @@ static int etm4_probe(struct device *dev, void __iomem *base, u32 etm_pid)if(!desc.access.io_mem||fwnode_property_present(dev_fwnode(dev),"qcom,skip-power-up"))drvdata->skip_power_up=true;+major=ETM_ARCH_MAJOR_VERSION(drvdata->arch);+minor=ETM_ARCH_MINOR_VERSION(drvdata->arch);+if(etm4x_is_ete(drvdata)){+type_name="ete";+/* ETE v1 has major version == 5. Adjust this for logging.*/+major-=4;+}else{+type_name="etm";+}++desc.name=devm_kasprintf(dev,GFP_KERNEL,+"%s%d",type_name,drvdata->cpu);+if(!desc.name)+return-ENOMEM;etm4_init_trace_id(drvdata);etm4_set_default(&drvdata->config);
@@ -0,0 +1,49 @@+# SPDX-License-Identifier: GPL-2.0-only or BSD-2-Clause+# Copyright 2021, Arm Ltd+%YAML1.2+---+$id:"http://devicetree.org/schemas/arm/trbe.yaml#"+$schema:"http://devicetree.org/meta-schemas/core.yaml#"++title:ARM Trace Buffer Extensions++maintainers:+-Anshuman Khandual <anshuman.khandual@arm.com>++description:|+Arm Trace Buffer Extension (TRBE) is a per CPU component+for storing trace generated on the CPU to memory. It is+accessed via CPU system registers. The software can verify+if it is permitted to use the component by checking the+TRBIDR register.++properties:+$nodename:+const:"trbe"+compatible:+items:+-const:arm,trace-buffer-extension++interrupts:+description:|+Exactly 1 PPI must be listed. For heterogeneous systems where+TRBE is only supported on a subset of the CPUs, please consult+the arm,gic-v3 binding for details on describing a PPI partition.+maxItems:1++required:+-compatible+-interrupts++additionalProperties:false++examples:++-|+#include <dt-bindings/interrupt-controller/arm-gic.h>++trbe {+compatible = "arm,trace-buffer-extension";+interrupts = <GIC_PPI 15 IRQ_TYPE_LEVEL_HIGH>;+};+...
--
2.7.4
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: Suzuki K Poulose <suzuki.poulose@arm.com>
The context associated with an ETM for a given perf event
includes :
- handle -> the perf output handle for the AUX buffer.
- the path for the trace components
- the buffer config for the sink.
The path and the buffer config are part of the "aux_priv" data
(etm_event_data) setup by the setup_aux() callback, and made available
via perf_get_aux(handle).
Now with a sink supporting IRQ, the sink could "end" an output
handle when the buffer reaches the programmed limit and would try
to restart a handle. This could fail if there is not enough
space left the AUX buffer (e.g, the userspace has not consumed
the data). This leaves the "handle" disconnected from the "event"
and also the "perf_get_aux()" cleared. This all happens within
the sink driver, without the etm_perf driver being aware.
Now when the event is actually stopped, etm_event_stop()
will need to access the "event_data". But since the handle
is not valid anymore, we loose the information to stop the
"trace" path. So, we need a reliable way to access the etm_event_data
even when the handle may not be active.
This patch replaces the per_cpu handle array with a per_cpu context
for the ETM, which tracks the "handle" as well as the "etm_event_data".
The context notes the etm_event_data at etm_event_start() and clears
it at etm_event_stop(). This makes sure that we don't access a
stale "etm_event_data" as we are guaranteed that it is not
freed by free_aux() as long as the event is active and tracing,
also provides us with access to the critical information
needed to wind up a session even in the absence of an active
output_handle.
This is not an issue for the legacy sinks as none of them supports
an IRQ and is centrally handled by the etm-perf.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Anshuman Khandual <redacted>
Cc: Leo Yan <redacted>
Cc: Mike Leach <redacted>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
drivers/hwtracing/coresight/coresight-etm-perf.c | 45 +++++++++++++++++++++---
1 file changed, 40 insertions(+), 5 deletions(-)
@@ -374,6 +394,8 @@ static void etm_event_start(struct perf_event *event, int flags)if(source_ops(csdev)->enable(csdev,event,CS_MODE_PERF))gotofail_disable_path;+/* Save the event_data for this ETM */+ctxt->event_data=event_data;out:return;
@@ -392,13 +414,20 @@ static void etm_event_stop(struct perf_event *event, int mode)intcpu=smp_processor_id();unsignedlongsize;structcoresight_device*sink,*csdev=per_cpu(csdev_src,cpu);-structperf_output_handle*handle=this_cpu_ptr(&ctx_handle);-structetm_event_data*event_data=perf_get_aux(handle);+structetm_ctxt*ctxt=this_cpu_ptr(&etm_ctxt);+structperf_output_handle*handle=&ctxt->handle;+structetm_event_data*event_data=ctxt->event_data;structlist_head*path;+/* Clear the event_data as this ETM is stopping the trace. */+ctxt->event_data=NULL;if(event->hw.state==PERF_HES_STOPPED)return;+/* We must have a valid event_data for a running event */+if(WARN_ON(!event_data))+return;+if(!csdev)return;
@@ -416,7 +445,13 @@ static void etm_event_stop(struct perf_event *event, int mode)/* tell the core */event->hw.state=PERF_HES_STOPPED;-if(mode&PERF_EF_UPDATE){+/*+*Ifthehandleisnotboundtoaneventanymore+*(e.g,thesinkdriverwasunabletorestartthe+*handleduetolackofbufferspace),wedon't+*havetodoanythinghere.+*/+if(handle->event&&(mode&PERF_EF_UPDATE)){if(WARN_ON_ONCE(handle->event!=event))return;
--
2.7.4
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: Suzuki K Poulose <suzuki.poulose@arm.com>
CoreSight PMU supports aux-buffer for the ETM tracing. The trace
generated by the ETM (associated with individual CPUs, like Intel PT)
is captured by a separate IP (CoreSight TMC-ETR/ETF until now).
The TMC-ETR applies formatting of the raw ETM trace data, as it
can collect traces from multiple ETMs, with the TraceID to indicate
the source of a given trace packet.
Arm Trace Buffer Extension is new "sink" IP, attached to individual
CPUs and thus do not provide additional formatting, like TMC-ETR.
Additionally, a system could have both TRBE *and* TMC-ETR for
the trace collection. e.g, TMC-ETR could be used as a single
trace buffer to collect data from multiple ETMs to correlate
the traces from different CPUs. It is possible to have a
perf session where some events end up collecting the trace
in TMC-ETR while the others in TRBE. Thus we need a way
to identify the type of the trace for each AUX record.
Define the trace formats exported by the CoreSight PMU.
We don't define the flags following the "ETM" as this
information is available to the user when issuing
the session. What is missing is the additional
formatting applied by the "sink" which is decided
at the runtime and the user may not have a control on.
So we define :
- CORESIGHT format (indicates the Frame format)
- RAW format (indicates the format of the source)
The default value is CORESIGHT format for all the records
(i,e == 0). Add the RAW format for the TRBE sink driver.
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Mike Leach <redacted>
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Leo Yan <redacted>
Cc: Anshuman Khandual <redacted>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
drivers/hwtracing/coresight/coresight-trbe.c | 2 ++
include/uapi/linux/perf_event.h | 4 ++++
2 files changed, 6 insertions(+)
From: Suzuki K Poulose <suzuki.poulose@arm.com>
Allocate a byte for advertising the PMU specific format type
of the given AUX record. A PMU could end up providing hardware
trace data in multiple format in a single session.
e.g, The format of hardware buffer produced by CoreSight ETM
PMU depends on the type of the "sink" device used for collection
for an event (Traditional TMC-ETR/Bs with formatting or
TRBEs without any formatting).
# Boring story of why this is needed. Goto The_End_of_Story for skipping.
CoreSight ETM trace allows instruction level tracing of Arm CPUs.
The ETM generates the CPU excecution trace and pumps it into CoreSight
AMBA Trace Bus and is collected by a different CoreSight component
(traditionally CoreSight TMC-ETR /ETB/ETF), called "sink".
Important to note that there is no guarantee that every CPU has
a dedicated sink. Thus multiple ETMs could pump the trace data
into the same "sink" and thus they apply additional formatting
of the trace data for the user to decode it properly and attribute
the trace data to the corresponding ETM.
However, with the introduction of Arm Trace buffer Extensions (TRBE),
we now have a dedicated per-CPU architected sink for collecting the
trace. Since the TRBE is always per-CPU, it doesn't apply any formatting
of the trace. The support for this driver is under review [1].
Now a system could have a per-cpu TRBE and one or more shared
TMC-ETRs on the system. A user could choose a "specific" sink
for a perf session (e.g, a TMC-ETR) or the driver could automatically
select the nearest sink for a given ETM. It is possible that
some ETMs could end up using TMC-ETR (e.g, if the TRBE is not
usable on the CPU) while the others using TRBE in a single
perf session. Thus we now have "formatted" trace collected
from TMC-ETR and "unformatted" trace collected from TRBE.
However, we don't get into a situation where a single event
could end up using TMC-ETR & TRBE. i.e, any AUX buffer is
guaranteed to be either RAW or FORMATTED, but not a mix
of both.
As for perf decoding, we need to know the type of the data
in the individual AUX buffers, so that it can set up the
"OpenCSD" (library for decoding CoreSight trace) decoder
instance appropriately. Thus the perf.data file must conatin
the hints for the tool to decode the data correctly.
Since this is a runtime variable, and perf tool doesn't have
a control on what sink gets used (in case of automatic sink
selection), we need this information made available from
the PMU driver for each AUX record.
# The_End_of_Story
Cc: Peter Ziljstra <peterz@infradead.org>
Cc: alexander.shishkin@linux.intel.com
Cc: mingo@redhat.com
Cc: will@kernel.org
Cc: mark.rutland@arm.com
Cc: mike.leach@linaro.org
Cc: acme@kernel.org
Cc: jolsa@redhat.com
Cc: Mathieu Poirier <redacted>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
include/uapi/linux/perf_event.h | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
@@ -1105,10 +1105,11 @@ enum perf_callchain_context {/***PERF_RECORD_AUX::flagsbits*/-#define PERF_AUX_FLAG_TRUNCATED 0x01 /* record was truncated to fit */-#define PERF_AUX_FLAG_OVERWRITE 0x02 /* snapshot from overwrite mode */-#define PERF_AUX_FLAG_PARTIAL 0x04 /* record contains gaps */-#define PERF_AUX_FLAG_COLLISION 0x08 /* sample collided with another */+#define PERF_AUX_FLAG_TRUNCATED 0x01 /* record was truncated to fit */+#define PERF_AUX_FLAG_OVERWRITE 0x02 /* snapshot from overwrite mode */+#define PERF_AUX_FLAG_PARTIAL 0x04 /* record contains gaps */+#define PERF_AUX_FLAG_COLLISION 0x08 /* sample collided with another */+#define PERF_AUX_FLAG_PMU_FORMAT_TYPE_MASK 0xff00 /* PMU specific trace format type */#define PERF_FLAG_FD_NO_GROUP (1UL << 0)#define PERF_FLAG_FD_OUTPUT (1UL << 1)
--
2.7.4
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Add support for dedicated sinks that are bound to individual CPUs. (e.g,
TRBE). To allow quicker access to the sink for a given CPU bound source,
keep a percpu array of the sink devices. Also, add support for building
a path to the CPU local sink from the ETM.
This adds a new percpu sink type CORESIGHT_DEV_SUBTYPE_SINK_PERCPU_SYSMEM.
This new sink type is exclusively available and can only work with percpu
source type device CORESIGHT_DEV_SUBTYPE_SOURCE_PERCPU_PROC.
This defines a percpu structure that accommodates a single coresight_device
which can be used to store an initialized instance from a sink driver. As
these sinks are exclusively linked and dependent on corresponding percpu
sources devices, they should also be the default sink device during a perf
session.
Outwards device connections are scanned while establishing paths between a
source and a sink device. But such connections are not present for certain
percpu source and sink devices which are exclusively linked and dependent.
Build the path directly and skip connection scanning for such devices.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
Changes in V3:
- Updated coresight_find_default_sink()
drivers/hwtracing/coresight/coresight-core.c | 16 ++++++++++++++--
include/linux/coresight.h | 12 ++++++++++++
2 files changed, 26 insertions(+), 2 deletions(-)
@@ -784,6 +785,13 @@ static int _coresight_build_path(struct coresight_device *csdev,if(csdev==sink)gotoout;+if(coresight_is_percpu_source(csdev)&&coresight_is_percpu_sink(sink)&&+sink==per_cpu(csdev_sink,source_ops(csdev)->cpu_id(csdev))){+_coresight_build_path(sink,sink,path);+found=true;+gotoout;+}+/* Not a sink - recursively explore each port found on this element */for(i=0;i<csdev->pdata->nr_outport;i++){structcoresight_device*child_dev;
@@ -999,8 +1007,12 @@ coresight_find_default_sink(struct coresight_device *csdev)intdepth=0;/* look for a default sink if we have not found for this device */-if(!csdev->def_sink)-csdev->def_sink=coresight_find_sink(csdev,&depth);+if(!csdev->def_sink){+if(coresight_is_percpu_source(csdev))+csdev->def_sink=per_cpu(csdev_sink,source_ops(csdev)->cpu_id(csdev));+if(!csdev->def_sink)+csdev->def_sink=coresight_find_sink(csdev,&depth);+}returncsdev->def_sink;}
From: Suzuki K Poulose <suzuki.poulose@arm.com>
When the kernel is booted at EL2 in a nvhe configuration,
enable the TRBE access to the EL1. The EL1 still can't trace
EL2, unless EL2 permits explicitly via TRFCR_EL2.E2TRE.
Cc: Will Deacon <will@kernel.org>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Marc Zyngier <maz@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
cc: Anshuman Khandual <redacted>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
arch/arm64/include/asm/el2_setup.h | 19 +++++++++++++++++++
arch/arm64/include/asm/kvm_arm.h | 2 ++
2 files changed, 21 insertions(+)
From: Suzuki K Poulose <suzuki.poulose@arm.com>
Document the device tree bindings for Embedded Trace Extensions.
ETE can be connected to legacy coresight components and thus
could optionally contain a connection graph as described by
the CoreSight bindings.
Cc: devicetree@vger.kernel.org
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Rob Herring <robh@kernel.org>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
Changes in V3:
- Fixed all DT yaml semantics problems
Documentation/devicetree/bindings/arm/ete.yaml | 74 ++++++++++++++++++++++++++
1 file changed, 74 insertions(+)
create mode 100644 Documentation/devicetree/bindings/arm/ete.yaml
@@ -0,0 +1,74 @@+# SPDX-License-Identifier: GPL-2.0-only or BSD-2-Clause+# Copyright 2021, Arm Ltd+%YAML1.2+---+$id:"http://devicetree.org/schemas/arm/ete.yaml#"+$schema:"http://devicetree.org/meta-schemas/core.yaml#"++title:ARM Embedded Trace Extensions++maintainers:+-Suzuki K Poulose <suzuki.poulose@arm.com>+-Mathieu Poirier <mathieu.poirier@linaro.org>++description:|+Arm Embedded Trace Extension(ETE) is a per CPU trace component that+allows tracing the CPU execution. It overlaps with the CoreSight ETMv4+architecture and has extended support for future architecture changes.+The trace generated by the ETE could be stored via legacy CoreSight+components (e.g, TMC-ETR) or other means (e.g, using a per CPU buffer+Arm Trace Buffer Extension (TRBE)). Since the ETE can be connected to+legacy CoreSight components, a node must be listed per instance, along+with any optional connection graph as per the coresight bindings.+See bindings/arm/coresight.txt.++properties:+$nodename:+pattern:"^ete([0-9a-f]+)$"+compatible:+items:+-const:arm,embedded-trace-extension++cpu:+description:|+Handle to the cpu this ETE is bound to.+$ref:/schemas/types.yaml#/definitions/phandle++out-ports:+type:object+description:|+Output connections from the ETE to legacy CoreSight trace bus.+properties:+port:+$ref:/schemas/graph.yaml#/properties/port++required:+-compatible+-cpu++additionalProperties:false++examples:++# An ETE node without legacy CoreSight connections+-|+ete0 {+compatible = "arm,embedded-trace-extension";+cpu = <&cpu_0>;+};+# An ETE node with legacy CoreSight connections+-|+ete1 {+compatible = "arm,embedded-trace-extension";+cpu = <&cpu_1>;++out-ports { /* legacy coresight connection */+port {+ete1_out_port:endpoint {+remote-endpoint = <&funnel_in_port0>;+};+};+};+};++...
--
2.7.4
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
This adds TRBE related registers and corresponding feature macros.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
Changes in V3:
- ID_AA64DFR0_TRBE_SHIFT has been moved here from the TRBE driver
- Changed TRBLIMITR_TRIG_MODE_SHIFT as 3
arch/arm64/include/asm/sysreg.h | 50 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 50 insertions(+)
From: Marc Zyngier <maz@kernel.org> Date: 2021-01-27 10:01:49
On 2021-01-27 08:55, Anshuman Khandual wrote:
From: Suzuki K Poulose <suzuki.poulose@arm.com>
When the kernel is booted at EL2 in a nvhe configuration,
enable the TRBE access to the EL1. The EL1 still can't trace
EL2, unless EL2 permits explicitly via TRFCR_EL2.E2TRE.
Cc: Will Deacon <will@kernel.org>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Marc Zyngier <maz@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
cc: Anshuman Khandual <redacted>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
Acked-by: Marc Zyngier <maz@kernel.org>
One comment below, though:
From: Peter Zijlstra <peterz@infradead.org> Date: 2021-01-27 12:55:13
On Wed, Jan 27, 2021 at 02:25:37PM +0530, Anshuman Khandual wrote:
From: Suzuki K Poulose <suzuki.poulose@arm.com>
Allocate a byte for advertising the PMU specific format type
of the given AUX record. A PMU could end up providing hardware
trace data in multiple format in a single session.
e.g, The format of hardware buffer produced by CoreSight ETM
PMU depends on the type of the "sink" device used for collection
for an event (Traditional TMC-ETR/Bs with formatting or
TRBEs without any formatting).
@@ -1105,10 +1105,11 @@ enum perf_callchain_context {/***PERF_RECORD_AUX::flagsbits*/-#define PERF_AUX_FLAG_TRUNCATED 0x01 /* record was truncated to fit */-#define PERF_AUX_FLAG_OVERWRITE 0x02 /* snapshot from overwrite mode */-#define PERF_AUX_FLAG_PARTIAL 0x04 /* record contains gaps */-#define PERF_AUX_FLAG_COLLISION 0x08 /* sample collided with another */+#define PERF_AUX_FLAG_TRUNCATED 0x01 /* record was truncated to fit */+#define PERF_AUX_FLAG_OVERWRITE 0x02 /* snapshot from overwrite mode */+#define PERF_AUX_FLAG_PARTIAL 0x04 /* record contains gaps */+#define PERF_AUX_FLAG_COLLISION 0x08 /* sample collided with another */+#define PERF_AUX_FLAG_PMU_FORMAT_TYPE_MASK 0xff00 /* PMU specific trace format type */#define PERF_FLAG_FD_NO_GROUP (1UL << 0)#define PERF_FLAG_FD_OUTPUT (1UL << 1)
From: Peter Zijlstra <peterz@infradead.org> Date: 2021-01-27 12:58:42
On Wed, Jan 27, 2021 at 02:25:38PM +0530, Anshuman Khandual wrote:
It is possible to have a
perf session where some events end up collecting the trace
in TMC-ETR while the others in TRBE. Thus we need a way
to identify the type of the trace for each AUX record.
@@ -1111,6 +1111,10 @@ enum perf_callchain_context {#define PERF_AUX_FLAG_COLLISION 0x08 /* sample collided with another */#define PERF_AUX_FLAG_PMU_FORMAT_TYPE_MASK 0xff00 /* PMU specific trace format type */+/* CoreSight PMU AUX buffer formats */+#define PERF_AUX_FLAG_CORESIGHT_FORMAT_CORESIGHT 0x0000 /* Default for backward compatibility */+#define PERF_AUX_FLAG_CORESIGHT_FORMAT_RAW 0x0100 /* Raw format of the source */
Would CORESIGHT_FORMAT_ETR / CORESIGHT_FORMAT_TRBE be better names?
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Raw format of the source */
Would CORESIGHT_FORMAT_ETR / CORESIGHT_FORMAT_TRBE be better
names?
Unformatted (raw) streams could be used any time you had a writer dedicated
to a single trace source. So in a situation where you had one ETR per CPU,
it would be appropriate to use an unformatted stream. A TRBE is always
dedicated to a single CPU, but potentially you (i.e. when designing the system)
can do this with any type of trace sink. So the raw/formatted distinction is
really about whether you are combining multiple streams in one buffer or not,
rather than the type of block that is writing into the buffer.
Al
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: Suzuki K Poulose <suzuki.poulose@arm.com> Date: 2021-01-27 14:16:11
Hi Peter
On 1/27/21 12:54 PM, Peter Zijlstra wrote:
On Wed, Jan 27, 2021 at 02:25:38PM +0530, Anshuman Khandual wrote:
quoted
It is possible to have a
perf session where some events end up collecting the trace
in TMC-ETR while the others in TRBE. Thus we need a way
to identify the type of the trace for each AUX record.
@@ -1111,6 +1111,10 @@ enum perf_callchain_context {#define PERF_AUX_FLAG_COLLISION 0x08 /* sample collided with another */#define PERF_AUX_FLAG_PMU_FORMAT_TYPE_MASK 0xff00 /* PMU specific trace format type */+/* CoreSight PMU AUX buffer formats */+#define PERF_AUX_FLAG_CORESIGHT_FORMAT_CORESIGHT 0x0000 /* Default for backward compatibility */+#define PERF_AUX_FLAG_CORESIGHT_FORMAT_RAW 0x0100 /* Raw format of the source */
Would CORESIGHT_FORMAT_ETR / CORESIGHT_FORMAT_TRBE be better names?
Not really. The format used by the ETR is CORESIGHT frame formatting. And
there are other sinks that uses the formatting. e.g, TMC-ETB (a sink with
internal memory), TMC-ETF (trace fifo with internal memory). So it is really
not tied to ETR.
As for TRBE, it simply pumps the data thrown at it to the memory. As such
calling it TRBE format would be confusing as the format of the buffer is
really Raw trace thrown at it. This can be inferred from the ETM/ETE
looking at the ID registers, which the userspace perf already captures
in the perf.data. So the decoder perf can look at the perf.data and the
AUX records to interpret the buffer correctly.
Suzuki
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Hi Anshuman,
I will start looking at your work later this week or early next week.
Mathieu
On Wed, 27 Jan 2021 at 01:55, Anshuman Khandual
[off-list ref] wrote:
This series enables future IP trace features Embedded Trace Extension (ETE)
and Trace Buffer Extension (TRBE). This series depends on the ETM system
register instruction support series [0] which is available here [1]. This
series which applies on [1] is avaialble here [2] for quick access.
ETE is the PE (CPU) trace unit for CPUs, implementing future architecture
extensions. ETE overlaps with the ETMv4 architecture, with additions to
support the newer architecture features and some restrictions on the
supported features w.r.t ETMv4. The ETE support is added by extending the
ETMv4 driver to recognise the ETE and handle the features as exposed by the
TRCIDRx registers. ETE only supports system instructions access from the
host CPU. The ETE could be integrated with a TRBE (see below), or with the
legacy CoreSight trace bus (e.g, ETRs). Thus the ETE follows same firmware
description as the ETMs and requires a node per instance.
Trace Buffer Extensions (TRBE) implements a per CPU trace buffer, which is
accessible via the system registers and can be combined with the ETE to
provide a 1x1 configuration of source & sink. TRBE is being represented
here as a CoreSight sink. Primary reason is that the ETE source could work
with other traditional CoreSight sink devices. As TRBE captures the trace
data which is produced by ETE, it cannot work alone.
TRBE representation here have some distinct deviations from a traditional
CoreSight sink device. Coresight path between ETE and TRBE are not built
during boot looking at respective DT or ACPI entries.
Unlike traditional sinks, TRBE can generate interrupts to signal including
many other things, buffer got filled. The interrupt is a PPI and should be
communicated from the platform. DT or ACPI entry representing TRBE should
have the PPI number for a given platform. During perf session, the TRBE IRQ
handler should capture trace for perf auxiliary buffer before restarting it
back. System registers being used here to configure ETE and TRBE could be
referred in the link below.
https://developer.arm.com/docs/ddi0601/g/aarch64-system-registers.
Question:
- Should we implement sysfs based trace sessions for TRBE ?
[0] https://lore.kernel.org/linux-arm-kernel/20210110224850.1880240-1-suzuki.poulose@arm.com/
[1] https://gitlab.arm.com/linux-arm/linux-skp/-/tree/coresight/etm/sysreg-v7
[2] https://gitlab.arm.com/linux-arm/linux-anshuman/-/tree/coresight/ete_trbe_v3
Changes in V3:
- Rebased on coresight/next
- Changed DT bindings for ETE
- Included additional patches for arm64 nvhe, perf aux buffer flags etc
- TRBE changes have been captured in the respective patches
Changes in V2:
https://lore.kernel.org/linux-arm-kernel/1610511498-4058-1-git-send-email-anshuman.khandual@arm.com/
- Converted both ETE and TRBE DT bindings into Yaml
- TRBE changes have been captured in the respective patches
Changes in V1:
https://lore.kernel.org/linux-arm-kernel/1608717823-18387-1-git-send-email-anshuman.khandual@arm.com/
- There are not much ETE changes from Suzuki apart from splitting of the ETE DTS patch
- TRBE changes have been captured in the respective patches
Changes in RFC:
https://lore.kernel.org/linux-arm-kernel/1605012309-24812-1-git-send-email-anshuman.khandual@arm.com/
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Cc: Mike Leach <redacted>
Cc: Linu Cherian <redacted>
Cc: coresight@lists.linaro.org
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-kernel@vger.kernel.org
Anshuman Khandual (3):
coresight: core: Add support for dedicated percpu sinks
arm64: Add TRBE definitions
coresight: sink: Add TRBE driver
Suzuki K Poulose (11):
coresight: etm-perf: Allow an event to use different sinks
coresight: Do not scan for graph if none is present
coresight: etm4x: Add support for PE OS lock
coresight: ete: Add support for ETE sysreg access
coresight: ete: Add support for ETE tracing
dts: bindings: Document device tree bindings for ETE
coresight: etm-perf: Handle stale output handles
arm64: nvhe: Allow TRBE access at EL1
dts: bindings: Document device tree bindings for Arm TRBE
perf: aux: Add flags for the buffer format
coresight: etm-perf: Add support for trace buffer format
Documentation/devicetree/bindings/arm/ete.yaml | 74 ++
Documentation/devicetree/bindings/arm/trbe.yaml | 49 +
Documentation/trace/coresight/coresight-trbe.rst | 39 +
arch/arm64/include/asm/el2_setup.h | 19 +
arch/arm64/include/asm/kvm_arm.h | 2 +
arch/arm64/include/asm/sysreg.h | 51 +
drivers/hwtracing/coresight/Kconfig | 21 +-
drivers/hwtracing/coresight/Makefile | 1 +
drivers/hwtracing/coresight/coresight-core.c | 16 +-
drivers/hwtracing/coresight/coresight-etm-perf.c | 93 +-
drivers/hwtracing/coresight/coresight-etm4x-core.c | 138 ++-
.../hwtracing/coresight/coresight-etm4x-sysfs.c | 19 +-
drivers/hwtracing/coresight/coresight-etm4x.h | 81 +-
drivers/hwtracing/coresight/coresight-platform.c | 6 +
drivers/hwtracing/coresight/coresight-trbe.c | 1025 ++++++++++++++++++++
drivers/hwtracing/coresight/coresight-trbe.h | 160 +++
include/linux/coresight.h | 12 +
include/uapi/linux/perf_event.h | 13 +-
18 files changed, 1759 insertions(+), 60 deletions(-)
create mode 100644 Documentation/devicetree/bindings/arm/ete.yaml
create mode 100644 Documentation/devicetree/bindings/arm/trbe.yaml
create mode 100644 Documentation/trace/coresight/coresight-trbe.rst
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.c
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.h
--
2.7.4
From: Suzuki K Poulose <suzuki.poulose@arm.com> Date: 2021-01-28 09:23:05
On 1/27/21 8:55 AM, Anshuman Khandual wrote:
quoted hunk
Add support for dedicated sinks that are bound to individual CPUs. (e.g,
TRBE). To allow quicker access to the sink for a given CPU bound source,
keep a percpu array of the sink devices. Also, add support for building
a path to the CPU local sink from the ETM.
This adds a new percpu sink type CORESIGHT_DEV_SUBTYPE_SINK_PERCPU_SYSMEM.
This new sink type is exclusively available and can only work with percpu
source type device CORESIGHT_DEV_SUBTYPE_SOURCE_PERCPU_PROC.
This defines a percpu structure that accommodates a single coresight_device
which can be used to store an initialized instance from a sink driver. As
these sinks are exclusively linked and dependent on corresponding percpu
sources devices, they should also be the default sink device during a perf
session.
Outwards device connections are scanned while establishing paths between a
source and a sink device. But such connections are not present for certain
percpu source and sink devices which are exclusively linked and dependent.
Build the path directly and skip connection scanning for such devices.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
Changes in V3:
- Updated coresight_find_default_sink()
drivers/hwtracing/coresight/coresight-core.c | 16 ++++++++++++++--
include/linux/coresight.h | 12 ++++++++++++
2 files changed, 26 insertions(+), 2 deletions(-)
@@ -784,6 +785,13 @@ static int _coresight_build_path(struct coresight_device *csdev,if(csdev==sink)gotoout;+if(coresight_is_percpu_source(csdev)&&coresight_is_percpu_sink(sink)&&+sink==per_cpu(csdev_sink,source_ops(csdev)->cpu_id(csdev))){+_coresight_build_path(sink,sink,path);+found=true;+gotoout;+}+/* Not a sink - recursively explore each port found on this element */for(i=0;i<csdev->pdata->nr_outport;i++){structcoresight_device*child_dev;
@@ -999,8 +1007,12 @@ coresight_find_default_sink(struct coresight_device *csdev)intdepth=0;/* look for a default sink if we have not found for this device */-if(!csdev->def_sink)-csdev->def_sink=coresight_find_sink(csdev,&depth);+if(!csdev->def_sink){+if(coresight_is_percpu_source(csdev))+csdev->def_sink=per_cpu(csdev_sink,source_ops(csdev)->cpu_id(csdev));+if(!csdev->def_sink)+csdev->def_sink=coresight_find_sink(csdev,&depth);+}returncsdev->def_sink;}
With the above :
Tested-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Reviewed-by: Suzuki K Poulose <suzuki.poulose@arm.com>
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: Suzuki K Poulose <suzuki.poulose@arm.com> Date: 2021-01-28 09:35:49
On 1/27/21 8:55 AM, Anshuman Khandual wrote:
This adds TRBE related registers and corresponding feature macros.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Will Deacon <will@kernel.org>
Signed-off-by: Anshuman Khandual <redacted>
Reviewed-by: Suzuki K Poulose <suzuki.poulose@arm.com>
quoted hunk
---
Changes in V3:
- ID_AA64DFR0_TRBE_SHIFT has been moved here from the TRBE driver
- Changed TRBLIMITR_TRIG_MODE_SHIFT as 3
arch/arm64/include/asm/sysreg.h | 50 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 50 insertions(+)
From: Suzuki K Poulose <suzuki.poulose@arm.com> Date: 2021-01-28 09:40:47
On 1/27/21 9:58 AM, Marc Zyngier wrote:
On 2021-01-27 08:55, Anshuman Khandual wrote:
quoted
From: Suzuki K Poulose <suzuki.poulose@arm.com>
When the kernel is booted at EL2 in a nvhe configuration,
enable the TRBE access to the EL1. The EL1 still can't trace
EL2, unless EL2 permits explicitly via TRFCR_EL2.E2TRE.
Cc: Will Deacon <will@kernel.org>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Marc Zyngier <maz@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
cc: Anshuman Khandual <redacted>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
Acked-by: Marc Zyngier <maz@kernel.org>
One comment below, though:
.endif
3:
+
+.ifeqs "\mode", "nvhe"
+ /*
+ * If the Trace Buffer is available, allow
+ * the EL1 to own it. Note that EL1 cannot
+ * trace the EL2, as it is prevented by
+ * TRFCR_EL2.E2TRE == 0.
+ */
+ ubfx x0, x1, #ID_AA64DFR0_TRBE_SHIFT, #4
+ cbz x0, 1f
+
+ mrs_s x0, SYS_TRBIDR_EL1
+ and x0, x0, TRBIDR_PROG
+ cbnz x0, 1f
+ mov x0, #(MDCR_EL2_E2TB_EL1_OWN << MDCR_EL2_E2TB_SHIFT)
+ orr x2, x2, x0
+.endif
+
+1:
Note that this will (badly) conflict with the late-VHE patches[1],
where this code path has been reworked.
Thanks for the heads up. We will need to see how things get merged.
Ideally this patch and the previous one (TRBE definitions could go
via the arm64 tree / kvm tree), in which case we could rebase these
two patches on the respective tree.
Cheers
Suzuki
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: Marc Zyngier <maz@kernel.org> Date: 2021-01-28 09:49:07
On 2021-01-28 09:34, Suzuki K Poulose wrote:
On 1/27/21 9:58 AM, Marc Zyngier wrote:
quoted
On 2021-01-27 08:55, Anshuman Khandual wrote:
quoted
From: Suzuki K Poulose <suzuki.poulose@arm.com>
When the kernel is booted at EL2 in a nvhe configuration,
enable the TRBE access to the EL1. The EL1 still can't trace
EL2, unless EL2 permits explicitly via TRFCR_EL2.E2TRE.
Cc: Will Deacon <will@kernel.org>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Marc Zyngier <maz@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
cc: Anshuman Khandual <redacted>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
Acked-by: Marc Zyngier <maz@kernel.org>
One comment below, though:
.endif
3:
+
+.ifeqs "\mode", "nvhe"
+ /*
+ * If the Trace Buffer is available, allow
+ * the EL1 to own it. Note that EL1 cannot
+ * trace the EL2, as it is prevented by
+ * TRFCR_EL2.E2TRE == 0.
+ */
+ ubfx x0, x1, #ID_AA64DFR0_TRBE_SHIFT, #4
+ cbz x0, 1f
+
+ mrs_s x0, SYS_TRBIDR_EL1
+ and x0, x0, TRBIDR_PROG
+ cbnz x0, 1f
+ mov x0, #(MDCR_EL2_E2TB_EL1_OWN << MDCR_EL2_E2TB_SHIFT)
+ orr x2, x2, x0
+.endif
+
+1:
Note that this will (badly) conflict with the late-VHE patches[1],
where this code path has been reworked.
Thanks for the heads up. We will need to see how things get merged.
Ideally this patch and the previous one (TRBE definitions could go
via the arm64 tree / kvm tree), in which case we could rebase these
two patches on the respective tree.
I think the current plan of action is to go via the arm64 tree,
given that there is nothing really KVM specific there. I'll respin
the series one last (hopefully!) time on Monday. Let me know if
you need a hand with the rebasing.
Thanks,
M.
--
Jazz is not dead. It just smells funny...
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: Suzuki K Poulose <suzuki.poulose@arm.com> Date: 2021-01-28 09:51:05
On 1/28/21 9:46 AM, Marc Zyngier wrote:
On 2021-01-28 09:34, Suzuki K Poulose wrote:
quoted
On 1/27/21 9:58 AM, Marc Zyngier wrote:
quoted
On 2021-01-27 08:55, Anshuman Khandual wrote:
quoted
From: Suzuki K Poulose <suzuki.poulose@arm.com>
When the kernel is booted at EL2 in a nvhe configuration,
enable the TRBE access to the EL1. The EL1 still can't trace
EL2, unless EL2 permits explicitly via TRFCR_EL2.E2TRE.
Cc: Will Deacon <will@kernel.org>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Marc Zyngier <maz@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
cc: Anshuman Khandual <redacted>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
Acked-by: Marc Zyngier <maz@kernel.org>
One comment below, though:
.endif
3:
+
+.ifeqs "\mode", "nvhe"
+ /*
+ * If the Trace Buffer is available, allow
+ * the EL1 to own it. Note that EL1 cannot
+ * trace the EL2, as it is prevented by
+ * TRFCR_EL2.E2TRE == 0.
+ */
+ ubfx x0, x1, #ID_AA64DFR0_TRBE_SHIFT, #4
+ cbz x0, 1f
+
+ mrs_s x0, SYS_TRBIDR_EL1
+ and x0, x0, TRBIDR_PROG
+ cbnz x0, 1f
+ mov x0, #(MDCR_EL2_E2TB_EL1_OWN << MDCR_EL2_E2TB_SHIFT)
+ orr x2, x2, x0
+.endif
+
+1:
Note that this will (badly) conflict with the late-VHE patches[1],
where this code path has been reworked.
Thanks for the heads up. We will need to see how things get merged.
Ideally this patch and the previous one (TRBE definitions could go
via the arm64 tree / kvm tree), in which case we could rebase these
two patches on the respective tree.
I think the current plan of action is to go via the arm64 tree,
given that there is nothing really KVM specific there. I'll respin
the series one last (hopefully!) time on Monday. Let me know if
you need a hand with the rebasing.
From: Suzuki K Poulose <suzuki.poulose@arm.com> Date: 2021-01-29 10:25:06
Hi Anshuman
On 1/27/21 8:55 AM, Anshuman Khandual wrote:
Trace Buffer Extension (TRBE) implements a trace buffer per CPU which is
accessible via the system registers. The TRBE supports different addressing
modes including CPU virtual address and buffer modes including the circular
buffer mode. The TRBE buffer is addressed by a base pointer (TRBBASER_EL1),
an write pointer (TRBPTR_EL1) and a limit pointer (TRBLIMITR_EL1). But the
access to the trace buffer could be prohibited by a higher exception level
(EL3 or EL2), indicated by TRBIDR_EL1.P. The TRBE can also generate a CPU
private interrupt (PPI) on address translation errors and when the buffer
is full. Overall implementation here is inspired from the Arm SPE driver.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
This version looks functionally correct to me. There are some minor
issues with the devm_ allocated memory and some driver hardening comments.
I ran this on a model and have tested this with various scenarios.
quoted hunk
---
Changes in V3:
- Added new DT bindings document TRBE.yaml
- Changed TRBLIMITR_TRIG_MODE_SHIFT from 2 to 3
- Dropped isb() from trbe_reset_local()
- Dropped gap between (void *) and buf->trbe_base
- Changed 'int' to 'unsigned int' in is_trbe_available()
- Dropped unused function set_trbe_running(), set_trbe_virtual_mode(),
set_trbe_enabled() and set_trbe_limit_pointer()
- Changed get_trbe_flag_update(), is_trbe_programmable() and
get_trbe_address_align() to accept TRBIDR value
- Changed is_trbe_running(), is_trbe_abort(), is_trbe_wrap(), is_trbe_trg(),
is_trbe_irq(), get_trbe_bsc() and get_trbe_ec() to accept TRBSR value
- Dropped snapshot mode condition in arm_trbe_alloc_buffer()
- Exit arm_trbe_init() when arm64_kernel_unmapped_at_el0() is enabled
- Compute trbe_limit before trbe_write to get the updated handle
- Added trbe_stop_and_truncate_event()
- Dropped trbe_handle_fatal()
Documentation/trace/coresight/coresight-trbe.rst | 39 +
arch/arm64/include/asm/sysreg.h | 1 +
drivers/hwtracing/coresight/Kconfig | 11 +
drivers/hwtracing/coresight/Makefile | 1 +
drivers/hwtracing/coresight/coresight-trbe.c | 1023 ++++++++++++++++++++++
drivers/hwtracing/coresight/coresight-trbe.h | 160 ++++
6 files changed, 1235 insertions(+)
create mode 100644 Documentation/trace/coresight/coresight-trbe.rst
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.c
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.h
@@ -0,0 +1,39 @@+.. SPDX-License-Identifier: GPL-2.0++==============================+Trace Buffer Extension (TRBE).+==============================++:Author: Anshuman Khandual <anshuman.khandual@arm.com>+:Date: November 2020++Hardware Description+--------------------++Trace Buffer Extension (TRBE) is a percpu hardware which captures in system+memory, CPU traces generated from a corresponding percpu tracing unit. This+gets plugged in as a coresight sink device because the corresponding trace+genarators (ETE), are plugged in as source device.++The TRBE is not compliant to CoreSight architecture specifications, but is+driven via the CoreSight driver framework to support the ETE (which is+CoreSight compliant) integration.++Sysfs files and directories+---------------------------++The TRBE devices appear on the existing coresight bus alongside the other+coresight devices::++ >$ ls /sys/bus/coresight/devices+ trbe0 trbe1 trbe2 trbe3++The ``trbe<N>`` named TRBEs are associated with a CPU.::++ >$ ls /sys/bus/coresight/devices/trbe0/+ align dbm++*Key file items are:-*+*``align``: TRBE write pointer alignment+*``dbm``: TRBE updates memory with access and dirty flags+
@@ -0,0 +1,1023 @@+// SPDX-License-Identifier: GPL-2.0+/*+*ThisdriverenablesTraceBufferExtension(TRBE)asaper-cpucoresight+*sinkdevicecouldthenpairwithanappropriateper-cpucoresightsource+*device(ETE)thusgeneratingrequiredtracedata.Tracecanbeenabled+*viatheperfframework.+*+*Copyright(C)2020ARMLtd.+*+*Author:AnshumanKhandual<anshuman.khandual@arm.com>+*/+#define DRVNAME "arm_trbe"++#define pr_fmt(fmt) DRVNAME ": " fmt++#include"coresight-trbe.h"++#define PERF_IDX2OFF(idx, buf) ((idx) % ((buf)->nr_pages << PAGE_SHIFT))++/*+*Apaddingpacketthatwillhelptheuserspacetools+*inskippingrelevantsectionsinthecapturedtrace+*datawhichcouldnotbedecoded.TRBEdoesn'tsupport+*formattingthetracedata,unlikethelegacyCoreSight+*sinksandthusweuseETEtracepacketstopadthe+*sectionsofthebuffer.+*/+#define ETE_IGNORE_PACKET 0x70++/*+*Minimumamountofmeaningfultracewillcontain:+*A-Sync,TraceInfo,TraceOn,Address,Atom.+*Thisisabout44bytesofETEtrace.Tobeon+*thesaferside,weassume64bytesistheminimum+*spacerequiredforameaningfulsession,before+*wehita"WRAP"event.+*/+#define TRBE_TRACE_MIN_BUF_SIZE 64++enumtrbe_fault_action{+TRBE_FAULT_ACT_WRAP,+TRBE_FAULT_ACT_SPURIOUS,+TRBE_FAULT_ACT_FATAL,+};++structtrbe_buf{+unsignedlongtrbe_base;+unsignedlongtrbe_limit;+unsignedlongtrbe_write;+intnr_pages;+void**pages;+boolsnapshot;+structtrbe_cpudata*cpudata;+};++structtrbe_cpudata{+booltrbe_dbm;+u64trbe_align;+intcpu;+enumcs_modemode;+structtrbe_buf*buf;+structtrbe_drvdata*drvdata;+};++structtrbe_drvdata{+structtrbe_cpudata__percpu*cpudata;+structperf_output_handle__percpu**handle;+structhlist_nodehotplug_node;+intirq;+cpumask_tsupported_cpus;+enumcpuhp_statetrbe_online;+structplatform_device*pdev;+};++staticinttrbe_alloc_node(structperf_event*event)+{+if(event->cpu==-1)+returnNUMA_NO_NODE;+returncpu_to_node(event->cpu);+}++staticvoidtrbe_drain_buffer(void)+{+asm(TSB_CSYNC);+dsb(nsh);+}++staticvoidtrbe_drain_and_disable_local(void)+{+trbe_drain_buffer();+write_sysreg_s(0,SYS_TRBLIMITR_EL1);+isb();+}++staticvoidtrbe_reset_local(void)+{+trbe_drain_and_disable_local();+write_sysreg_s(0,SYS_TRBPTR_EL1);+write_sysreg_s(0,SYS_TRBBASER_EL1);+write_sysreg_s(0,SYS_TRBSR_EL1);+}++staticvoidtrbe_stop_and_truncate_event(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);++/*+*Wecannotproceedwiththebuffercollectionandwe+*donothaveanydataforthecurrentsession.The+*etm_perfdriverexpectstocloseouttheaux_buffer+*atevent_stop().SodisabletheTRBEhereandleave+*theupdate_buffer()toreturna0size.+*/+trbe_drain_and_disable_local();+perf_aux_output_flag(handle,PERF_AUX_FLAG_TRUNCATED);+*this_cpu_ptr(buf->cpudata->drvdata->handle)=NULL;+}++/*+*TRBEBufferManagement+*+*TheTRBEbufferspansfromthebasepointertillthelimitpointer.Whenenabled,+*itstartswritingtracedatafromthewritepointeronwardtillthelimitpointer.+*Whenthewritepointerreachestheaddressjustbeforethelimitpointer,itgets+*wrappedaroundagaintothebasepointer.ThisiscalledaTRBEwrapevent,which+*generatesamaintenanceinterruptwhenoperatedinWRAPorFILLmode.Thisdriver+*usesFILLmode,wheretheTRBEstopsthetracecollectionatwrapevent.TheIRQ+*handlerupdatestheAUXbufferandre-enablestheTRBEwithupdatedWRITEand+*LIMITpointers.+*+*WraparoundwithanIRQ+*------<------<-------<-----<-----+*||+*------>------>------->----->-----+*+*+---------------+-----------------------++*|||+*+---------------+-----------------------++*BasePointerWritePointerLimitPointer+*+*ThebaseandlimitpointersalwaysneedstobePAGE_SIZEaligned.Butthewrite+*pointercanbealignedtotheimplementationdefinedTRBEtracebufferalignment+*ascapturedintrbe_cpudata->trbe_align.+*+*+*headtailwakeup+*+---------------------------------------+-----~~------+*|$$$$$$$|################|$$$$$$$$$$$$$$||+*+---------------------------------------+-----~~------+*BasePointerWritePointerLimitPointer+*+*Theperf_output_handleindices(head,tail,wakeup)aremonotonicallyincreasing+*valueswhichtracksallthedriverwritesanduserreadsfromtheperfauxiliary+*buffer.Generally[head..tail]istheareawherethedrivercanwriteintounless+*thewakeupisbehindthetail.EnabledTRBEbufferspanneedstobeadjustedand+*configureddependingontheperf_output_handleindices,sothatthedriverdoes+*notoverrideintoareasintheperfauxiliarybufferwhichisbeingoryettobe+*consumedfromtheuserspace.TheenabledTRBEbufferareaisamovingsubsetof+*theallocatedperfauxiliarybuffer.+*/+staticvoidtrbe_pad_buf(structperf_output_handle*handle,intlen)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);+u64head=PERF_IDX2OFF(handle->head,buf);++memset((void*)buf->trbe_base+head,ETE_IGNORE_PACKET,len);+if(!buf->snapshot)+perf_aux_output_skip(handle,len);+}++staticunsignedlongtrbe_snapshot_offset(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);++/*+*TheETEtracehasalignmentsynchronizationpacketsallowing+*thedecodertoresetincaseofanoverfloworcorruption.+*Sowecanusetheentirebufferforthesnapshotmode.+*/+returnbuf->nr_pages*PAGE_SIZE;+}++/*+*TRBELimitCalculation+*+*ThefollowingmarkersareusedtoillustratevariousTRBEbuffersituations.+*+*$$$$-Dataarea,unconsumedcapturedtracedata,nottobeoverridden+*####-Freearea,enabled,tracewillbewritten+*%%%%-Freearea,disabled,tracewillnotbewritten+*====-Freearea,paddedwithETE_IGNORE_PACKET,tracewillbeskipped+*/+staticunsignedlong__trbe_normal_offset(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);+structtrbe_cpudata*cpudata=buf->cpudata;+constu64bufsize=buf->nr_pages*PAGE_SIZE;+u64limit=bufsize;+u64head,tail,wakeup;++head=PERF_IDX2OFF(handle->head,buf);++/*+*head+*------->|+*|+*headTRBEaligntail+*+----|-------|---------------|-------++*|$$$$|=======|###############|$$$$$$$|+*+----|-------|---------------|-------++*trbe_basetrbe_base+nr_pages+*+*Perfauxbufferoutputheadpositioncanbemisaligneddependingon+*variousfactorsincludinguserspacereads.Incasemisaligned,head+*needstobealignedbeforeTRBEcanbeconfigured.Padthealignment+*gapwithETE_IGNORE_PACKETbytesthatwillbeignoredbyusertools+*andskipthissectionthusadvancingthehead.+*/+if(!IS_ALIGNED(head,cpudata->trbe_align)){+unsignedlongdelta=roundup(head,cpudata->trbe_align)-head;++delta=min(delta,handle->size);+trbe_pad_buf(handle,delta);+head=PERF_IDX2OFF(handle->head,buf);+}++/*+*head=tail(size=0)+*+----|-------------------------------++*|$$$$|$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$|+*+----|-------------------------------++*trbe_basetrbe_base+nr_pages+*+*Perfauxbufferdoesnothaveanyspaceforthedrivertowriteinto.+*Justcommunicatetracetruncationeventtotheuserspacebymarking+*itwithPERF_AUX_FLAG_TRUNCATED.+*/+if(!handle->size){+perf_aux_output_flag(handle,PERF_AUX_FLAG_TRUNCATED);+return0;+}++/* Compute the tail and wakeup indices now that we've aligned head */+tail=PERF_IDX2OFF(handle->head+handle->size,buf);+wakeup=PERF_IDX2OFF(handle->wakeup,buf);++/*+*LetscalculatethebufferareawhichTRBEcouldwriteinto.There+*arethreepossiblescenarioshere.Limitneedstobealignedwith+*PAGE_SIZEpertheTRBErequirement.Alwaysavoidclobberingthe+*unconsumeddata.+*+*1)head<tail+*+*headtail+*+----|-----------------------|-------++*|$$$$|#######################|$$$$$$$|+*+----|-----------------------|-------++*trbe_baselimittrbe_base+nr_pages+*+*TRBEcouldwriteinto[head..tail]area.Unlessthetailisrightat+*theendofthebuffer,neitheranwraparoundnoranIRQisexpected+*whilebeingenabled.+*+*2)head==tail+*+*head=tail(size>0)+*+----|-------------------------------++*|%%%%|###############################|+*+----|-------------------------------++*trbe_baselimit=trbe_base+nr_pages+*+*TRBEshouldjustwriteinto[head..base+nr_pages]areaeventhough+*theentirebufferisempty.Reasonbeing,whenthetracereachesthe+*endofthebuffer,itwilljustwraparoundwithanIRQgivingan+*opportunitytoreconfigurethebuffer.+*+*3)tail<head+*+*tailhead+*+----|-----------------------|-------++*|%%%%|$$$$$$$$$$$$$$$$$$$$$$$|#######|+*+----|-----------------------|-------++*trbe_baselimit=trbe_base+nr_pages+*+*TRBEshouldjustwriteinto[head..base+nr_pages]areaeventhough+*the[trbe_base..tail]isalsoempty.Reasonbeing,whenthetrace+*reachestheendofthebuffer,itwilljustwraparoundwithanIRQ+*givinganopportunitytoreconfigurethebuffer.+*/+if(head<tail)+limit=round_down(tail,PAGE_SIZE);++/*+*Wakeupmaybearbitrarilyfarintothefuture.Ifit'snotinthe+*currentgeneration,eitherwe'llwrapbeforehittingit,orit's+*inthepastandhasbeenhandledalready.+*+*Ifthere'sawakeupbeforewewrap,arrangetobewokenupbythe+*pageboundaryfollowingit.Keepthetailboundaryifthat'slower.+*+*headwakeuptail+*+----|---------------|-------|-------++*|$$$$|###############|%%%%%%%|$$$$$$$|+*+----|---------------|-------|-------++*trbe_baselimittrbe_base+nr_pages+*/+if(handle->wakeup<(handle->head+handle->size)&&head<=wakeup)+limit=min(limit,round_up(wakeup,PAGE_SIZE));++/*+*Therearetwosituationwhenthiscanhappeni.elimitisbefore+*theheadandhenceTRBEcannotbeconfigured.+*+*1)head<tail(aligneddownwithPAGE_SIZE)andalsotheyareboth+*withinthesamePAGEsizerange.+*+*PAGE_SIZE+*|----------------------|+*+*limitheadtail+*+------------|------|--------|-------++*|$$$$$$$$$$$$$$$$$$$|========|$$$$$$$|+*+------------|------|--------|-------++*trbe_basetrbe_base+nr_pages+*+*2)head<wakeup(alignedupwithPAGE_SIZE)<tailandalsoboth+*headandwakeuparewithinsamePAGEsizerange.+*+*PAGE_SIZE+*|----------------------|+*+*limitheadwakeuptail+*+----|------|-------|--------|-------++*|$$$$$$$$$$$|=======|========|$$$$$$$|+*+----|------|-------|--------|-------++*trbe_basetrbe_base+nr_pages+*/+if(limit>head)+returnlimit;++trbe_pad_buf(handle,handle->size);+perf_aux_output_flag(handle,PERF_AUX_FLAG_TRUNCATED);+return0;+}++staticunsignedlongtrbe_normal_offset(structperf_output_handle*handle)+{+structtrbe_buf*buf=perf_get_aux(handle);+u64limit=__trbe_normal_offset(handle);+u64head=PERF_IDX2OFF(handle->head,buf);++/*+*Iftheheadistooclosetothelimitandwedon't+*havespaceforameaningfulrun,weratherpadit+*andstartfresh.+*/+if(limit&&(limit-head<TRBE_TRACE_MIN_BUF_SIZE)){+trbe_pad_buf(handle,limit-head);+limit=__trbe_normal_offset(handle);+}+returnlimit;+}++staticunsignedlongcompute_trbe_buffer_limit(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);+unsignedlongoffset;++if(buf->snapshot)+offset=trbe_snapshot_offset(handle);+else+offset=trbe_normal_offset(handle);+returnbuf->trbe_base+offset;+}++staticvoidclr_trbe_status(void)+{+u64trbsr=read_sysreg_s(SYS_TRBSR_EL1);++WARN_ON(is_trbe_enabled());+trbsr&=~TRBSR_IRQ;+trbsr&=~TRBSR_TRG;+trbsr&=~TRBSR_WRAP;+trbsr&=~(TRBSR_EC_MASK<<TRBSR_EC_SHIFT);+trbsr&=~(TRBSR_BSC_MASK<<TRBSR_BSC_SHIFT);+trbsr&=~TRBSR_STOP;+write_sysreg_s(trbsr,SYS_TRBSR_EL1);+}++staticvoidset_trbe_limit_pointer_enabled(unsignedlongaddr)+{+u64trblimitr=read_sysreg_s(SYS_TRBLIMITR_EL1);++WARN_ON(!IS_ALIGNED(addr,(1UL<<TRBLIMITR_LIMIT_SHIFT)));+WARN_ON(!IS_ALIGNED(addr,PAGE_SIZE));++trblimitr&=~TRBLIMITR_NVM;+trblimitr&=~(TRBLIMITR_FILL_MODE_MASK<<TRBLIMITR_FILL_MODE_SHIFT);+trblimitr&=~(TRBLIMITR_TRIG_MODE_MASK<<TRBLIMITR_TRIG_MODE_SHIFT);+trblimitr&=~(TRBLIMITR_LIMIT_MASK<<TRBLIMITR_LIMIT_SHIFT);++/*+*Filltracebuffermodeisusedherewhileconfiguringthe+*TRBEfortracecapture.Inthisparticularmode,thetrace+*collectionisstoppedandamaintenanceinterruptisraised+*whenthecurrentwritepointerwraps.Thispauseintrace+*collectiongivesthesoftwareanopportunitytocapturethe+*tracedataintheinterrupthandler,beforereconfiguring+*theTRBE.+*/+trblimitr|=(TRBE_FILL_MODE_FILL&TRBLIMITR_FILL_MODE_MASK)<<TRBLIMITR_FILL_MODE_SHIFT;++/*+*TriggermodeisnotusedherewhileconfiguringtheTRBEfor+*thetracecapture.Hencejustkeepthisintheignoremode.+*/+trblimitr|=(TRBE_TRIG_MODE_IGNORE&TRBLIMITR_TRIG_MODE_MASK)<<TRBLIMITR_TRIG_MODE_SHIFT;+trblimitr|=(addr&PAGE_MASK);++trblimitr|=TRBLIMITR_ENABLE;+write_sysreg_s(trblimitr,SYS_TRBLIMITR_EL1);
Personally, I prefer the isb() here rather than at the caller site, to make sure
it is all contained here and more importantly we don't execute any other instruction
before the "isb()" as the tracing may be enabled for the kernel.
+}
+
+static void trbe_enable_hw(struct trbe_buf *buf)
+{
+ WARN_ON(buf->trbe_write < buf->trbe_base); > + WARN_ON(buf->trbe_write >= buf->trbe_limit);
+ set_trbe_disabled();
+ isb();
+ clr_trbe_status();
+ set_trbe_base_pointer(buf->trbe_base);
+ set_trbe_write_pointer(buf->trbe_write);
+
+ /*
+ * Synchronize all the register updates
+ * till now before enabling the TRBE.
+ */
+ isb();
+ set_trbe_limit_pointer_enabled(buf->trbe_limit);
+
+ /* Synchronize the TRBE enable event */
+ isb();
+}
+
+static void *arm_trbe_alloc_buffer(struct coresight_device *csdev,
+ struct perf_event *event, void **pages,
+ int nr_pages, bool snapshot)
+{
+ struct trbe_buf *buf;
+ struct page **pglist;
+ int i;
+
+ /*
+ * TRBE LIMIT and TRBE WRITE pointers must be page aligned. But with
+ * just a single page, there is not much room left while writing into
+ * a partially filled TRBE buffer. Hence restrict the minimum buffer
+ * size as two pages.
+ */
+ if (nr_pages < 2)
+ return NULL;
+
+ buf = kzalloc_node(sizeof(*buf), GFP_KERNEL, trbe_alloc_node(event));
+ if (IS_ERR(buf))
+ return ERR_PTR(-ENOMEM);
+
+ pglist = kcalloc(nr_pages, sizeof(*pglist), GFP_KERNEL);
+ if (IS_ERR(pglist)) {
+ kfree(buf);
+ return ERR_PTR(-ENOMEM);
+ }
+
+ for (i = 0; i < nr_pages; i++)
+ pglist[i] = virt_to_page(pages[i]);
+
+ buf->trbe_base = (unsigned long) vmap(pglist, nr_pages, VM_MAP, PAGE_KERNEL);
Please return 0 here. As we are expected to pass an "unsigned" size.
+
+ /*
+ * If the TRBE was disabled due to lack of space in the AUX buffer or a
+ * spurious fault, the driver leaves it disabled, truncating the buffer.
+ * Since the etm_perf driver expects to close out the AUX buffer, the
+ * driver skips it. Thus, just pass in 0 size here to indicate that the
+ * buffer was truncated.
+ */
+ if (!is_trbe_enabled())
+ return 0;
+ /*
+ * perf handle structure needs to be shared with the TRBE IRQ handler for
+ * capturing trace data and restarting the handle. There is a probability
+ * of an undefined reference based crash when etm event is being stopped
+ * while a TRBE IRQ also getting processed. This happens due the release
+ * of perf handle via perf_aux_output_end() in etm_event_stop(). Stopping
+ * the TRBE here will ensure that no IRQ could be generated when the perf
+ * handle gets freed in etm_event_stop().
+ */
+ trbe_drain_and_disable_local();
+ offset = get_trbe_write_pointer() - get_trbe_base_pointer();
+ size = offset - PERF_IDX2OFF(handle->head, buf);
It may be a good idea to verify that the size computation here doesn't overflow.
e.g broken interrupt could potentially leave us with "write == base". I understand
this is a hardware issue, but the software can be resilient to not send bogus
results.
We may add a comment here to explain how this would be handled ? e.g,
/*
* We are unable to restart the trace collection,
* thus leave the TRBE disabled. The etm-perf driver
* is able to detect this with a disconnnected handle
* (handle->event = NULL).
*/
We may want to ensure that handle is not NULL, which can only happen if the
hardware is not following the software.
if (WARN_ON_ONCE(!handle) || !perf_get_aux(handle))
On Wed, Jan 27, 2021 at 02:25:24PM +0530, Anshuman Khandual wrote:
This series enables future IP trace features Embedded Trace Extension (ETE)
and Trace Buffer Extension (TRBE). This series depends on the ETM system
register instruction support series [0] which is available here [1]. This
series which applies on [1] is avaialble here [2] for quick access.
ETE is the PE (CPU) trace unit for CPUs, implementing future architecture
extensions. ETE overlaps with the ETMv4 architecture, with additions to
support the newer architecture features and some restrictions on the
supported features w.r.t ETMv4. The ETE support is added by extending the
ETMv4 driver to recognise the ETE and handle the features as exposed by the
TRCIDRx registers. ETE only supports system instructions access from the
host CPU. The ETE could be integrated with a TRBE (see below), or with the
legacy CoreSight trace bus (e.g, ETRs). Thus the ETE follows same firmware
description as the ETMs and requires a node per instance.
Trace Buffer Extensions (TRBE) implements a per CPU trace buffer, which is
accessible via the system registers and can be combined with the ETE to
provide a 1x1 configuration of source & sink. TRBE is being represented
here as a CoreSight sink. Primary reason is that the ETE source could work
with other traditional CoreSight sink devices. As TRBE captures the trace
data which is produced by ETE, it cannot work alone.
TRBE representation here have some distinct deviations from a traditional
CoreSight sink device. Coresight path between ETE and TRBE are not built
during boot looking at respective DT or ACPI entries.
Unlike traditional sinks, TRBE can generate interrupts to signal including
many other things, buffer got filled. The interrupt is a PPI and should be
communicated from the platform. DT or ACPI entry representing TRBE should
have the PPI number for a given platform. During perf session, the TRBE IRQ
handler should capture trace for perf auxiliary buffer before restarting it
back. System registers being used here to configure ETE and TRBE could be
referred in the link below.
https://developer.arm.com/docs/ddi0601/g/aarch64-system-registers.
This set is giving me several checkpatch.pl warnings... Those about complex
macros and DT bindings are fine but everything else should have been addressed
by now. Since this is your first patchset I will carry on but I expect future
submissions to be clean.
Question:
- Should we implement sysfs based trace sessions for TRBE ?
[0] https://lore.kernel.org/linux-arm-kernel/20210110224850.1880240-1-suzuki.poulose@arm.com/
[1] https://gitlab.arm.com/linux-arm/linux-skp/-/tree/coresight/etm/sysreg-v7
[2] https://gitlab.arm.com/linux-arm/linux-anshuman/-/tree/coresight/ete_trbe_v3
Changes in V3:
- Rebased on coresight/next
- Changed DT bindings for ETE
- Included additional patches for arm64 nvhe, perf aux buffer flags etc
- TRBE changes have been captured in the respective patches
Changes in V2:
https://lore.kernel.org/linux-arm-kernel/1610511498-4058-1-git-send-email-anshuman.khandual@arm.com/
- Converted both ETE and TRBE DT bindings into Yaml
- TRBE changes have been captured in the respective patches
Changes in V1:
https://lore.kernel.org/linux-arm-kernel/1608717823-18387-1-git-send-email-anshuman.khandual@arm.com/
- There are not much ETE changes from Suzuki apart from splitting of the ETE DTS patch
- TRBE changes have been captured in the respective patches
Changes in RFC:
https://lore.kernel.org/linux-arm-kernel/1605012309-24812-1-git-send-email-anshuman.khandual@arm.com/
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Cc: Mike Leach <redacted>
Cc: Linu Cherian <redacted>
Cc: coresight@lists.linaro.org
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-kernel@vger.kernel.org
Anshuman Khandual (3):
coresight: core: Add support for dedicated percpu sinks
arm64: Add TRBE definitions
coresight: sink: Add TRBE driver
Suzuki K Poulose (11):
coresight: etm-perf: Allow an event to use different sinks
coresight: Do not scan for graph if none is present
coresight: etm4x: Add support for PE OS lock
coresight: ete: Add support for ETE sysreg access
coresight: ete: Add support for ETE tracing
dts: bindings: Document device tree bindings for ETE
coresight: etm-perf: Handle stale output handles
arm64: nvhe: Allow TRBE access at EL1
dts: bindings: Document device tree bindings for Arm TRBE
perf: aux: Add flags for the buffer format
coresight: etm-perf: Add support for trace buffer format
Documentation/devicetree/bindings/arm/ete.yaml | 74 ++
Documentation/devicetree/bindings/arm/trbe.yaml | 49 +
Documentation/trace/coresight/coresight-trbe.rst | 39 +
arch/arm64/include/asm/el2_setup.h | 19 +
arch/arm64/include/asm/kvm_arm.h | 2 +
arch/arm64/include/asm/sysreg.h | 51 +
drivers/hwtracing/coresight/Kconfig | 21 +-
drivers/hwtracing/coresight/Makefile | 1 +
drivers/hwtracing/coresight/coresight-core.c | 16 +-
drivers/hwtracing/coresight/coresight-etm-perf.c | 93 +-
drivers/hwtracing/coresight/coresight-etm4x-core.c | 138 ++-
.../hwtracing/coresight/coresight-etm4x-sysfs.c | 19 +-
drivers/hwtracing/coresight/coresight-etm4x.h | 81 +-
drivers/hwtracing/coresight/coresight-platform.c | 6 +
drivers/hwtracing/coresight/coresight-trbe.c | 1025 ++++++++++++++++++++
drivers/hwtracing/coresight/coresight-trbe.h | 160 +++
include/linux/coresight.h | 12 +
include/uapi/linux/perf_event.h | 13 +-
18 files changed, 1759 insertions(+), 60 deletions(-)
create mode 100644 Documentation/devicetree/bindings/arm/ete.yaml
create mode 100644 Documentation/devicetree/bindings/arm/trbe.yaml
create mode 100644 Documentation/trace/coresight/coresight-trbe.rst
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.c
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.h
--
2.7.4
Hi Anshuman,
I have started reviewing this set. As it is quite voluminous comments will
come over serveral days. I will let you know when I am done.
On Wed, Jan 27, 2021 at 02:25:25PM +0530, Anshuman Khandual wrote:
From: Suzuki K Poulose <suzuki.poulose@arm.com>
When there are multiple sinks on the system, in the absence
of a specified sink, it is quite possible that a default sink
for an ETM could be different from that of another ETM. However
we do not support having multiple sinks for an event yet. This
patch allows the event to use the default sinks on the ETMs
where they are scheduled as long as the sinks are of the same
type.
e.g, if we have 1x1 topology with per-CPU ETRs, the event can
use the per-CPU ETR for the session. However, if the sinks
are of different type, e.g TMC-ETR on one and a custom sink
on another, the event will only trace on the first detected
sink.
I found the above changelog very confusing - I read it several times and still
couldn't get all of it. In the end this patch prevents sinks of different types
from being used for session, and this is what the text should reflect.
@@ -249,21 +258,35 @@ static void *etm_setup_aux(struct perf_event *event, void **pages, } /*- * No sink provided - look for a default sink for one of the- * devices. At present we only support topology where all CPUs- * use the same sink [N:1], so only need to find one sink. The- * coresight_build_path later will remove any CPU that does not- * attach to the sink, or if we have not found a sink.+ * No sink provided - look for a default sink for all the devices.+ * We only support multiple sinks, only if all the default sinks+ * are of the same type, so that the sink buffer can be shared+ * as the event moves around. We don't trace on a CPU if it can't
s/can't/can't./
+ *
Extra line
*/
- if (!sink)
- sink = coresight_find_default_sink(csdev);
+ if (!sink_forced) {
+ new_sink = coresight_find_default_sink(csdev);
+ if (!new_sink) {
+ cpumask_clear_cpu(cpu, mask);
+ continue;
+ }
+ /* Skip checks for the first sink */
+ if (!sink) {
+ sink = new_sink;
+ } else if (!sinks_match(new_sink, sink)) {
+ cpumask_clear_cpu(cpu, mask);
+ continue;
+ }
+ } else {
+ new_sink = sink;
+ }
if (!user_sink) {
/* find default sink for this CPU */
sink = coresight_find_default_sink(csdev);
if (!sink) {
cpumask_clear_cpu(cpu, mask);
continue;
}
/* Chech new sink with last sink */
if (last_sink && !sink_match(last_sink, sink)) {
cpumask_clear_cpu(cpu, mask);
continue;
}
last_sink = sink;
} else {
sink = user_sink;
}
quoted hunk
/*
* Building a path doesn't enable it, it simply builds a
* list of devices from source to sink that can be
* referenced later when the path is actually needed.
*/
- path = coresight_build_path(csdev, sink);
+ path = coresight_build_path(csdev, new_sink);
if (IS_ERR(path)) {
cpumask_clear_cpu(cpu, mask);
continue;
@@ -284,7 +307,12 @@ static void *etm_setup_aux(struct perf_event *event, void **pages, if (!sink_ops(sink)->alloc_buffer || !sink_ops(sink)->free_buffer) goto err;- /* Allocate the sink buffer for this session */+ /*+ * Allocate the sink buffer for this session. All the sinks+ * where this event can be scheduled are ensured to be of the+ * same type. Thus the same sink configuration is used by the+ * sinks.+ */ event_data->snk_config = sink_ops(sink)->alloc_buffer(sink, event, pages, nr_pages, overwrite);
On Wed, Jan 27, 2021 at 02:25:26PM +0530, Anshuman Khandual wrote:
quoted hunk
From: Suzuki K Poulose <suzuki.poulose@arm.com>
If a graph node is not found for a given node, of_get_next_endpoint()
will emit the following error message :
OF: graph: no port node found in /<node_name>
If the given component doesn't have any explicit connections (e.g,
ETE) we could simply ignore the graph parsing.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
drivers/hwtracing/coresight/coresight-platform.c | 6 ++++++
1 file changed, 6 insertions(+)
Hi Anshuman
On 1/27/21 8:55 AM, Anshuman Khandual wrote:
quoted
Trace Buffer Extension (TRBE) implements a trace buffer per CPU which is
accessible via the system registers. The TRBE supports different addressing
modes including CPU virtual address and buffer modes including the circular
buffer mode. The TRBE buffer is addressed by a base pointer (TRBBASER_EL1),
an write pointer (TRBPTR_EL1) and a limit pointer (TRBLIMITR_EL1). But the
access to the trace buffer could be prohibited by a higher exception level
(EL3 or EL2), indicated by TRBIDR_EL1.P. The TRBE can also generate a CPU
private interrupt (PPI) on address translation errors and when the buffer
is full. Overall implementation here is inspired from the Arm SPE driver.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
This version looks functionally correct to me. There are some minor
issues with the devm_ allocated memory and some driver hardening comments.
I ran this on a model and have tested this with various scenarios.
Okay.
quoted
---
Changes in V3:
- Added new DT bindings document TRBE.yaml
- Changed TRBLIMITR_TRIG_MODE_SHIFT from 2 to 3
- Dropped isb() from trbe_reset_local()
- Dropped gap between (void *) and buf->trbe_base
- Changed 'int' to 'unsigned int' in is_trbe_available()
- Dropped unused function set_trbe_running(), set_trbe_virtual_mode(),
set_trbe_enabled() and set_trbe_limit_pointer()
- Changed get_trbe_flag_update(), is_trbe_programmable() and
get_trbe_address_align() to accept TRBIDR value
- Changed is_trbe_running(), is_trbe_abort(), is_trbe_wrap(), is_trbe_trg(),
is_trbe_irq(), get_trbe_bsc() and get_trbe_ec() to accept TRBSR value
- Dropped snapshot mode condition in arm_trbe_alloc_buffer()
- Exit arm_trbe_init() when arm64_kernel_unmapped_at_el0() is enabled
- Compute trbe_limit before trbe_write to get the updated handle
- Added trbe_stop_and_truncate_event()
- Dropped trbe_handle_fatal()
Documentation/trace/coresight/coresight-trbe.rst | 39 +
arch/arm64/include/asm/sysreg.h | 1 +
drivers/hwtracing/coresight/Kconfig | 11 +
drivers/hwtracing/coresight/Makefile | 1 +
drivers/hwtracing/coresight/coresight-trbe.c | 1023 ++++++++++++++++++++++
drivers/hwtracing/coresight/coresight-trbe.h | 160 ++++
6 files changed, 1235 insertions(+)
create mode 100644 Documentation/trace/coresight/coresight-trbe.rst
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.c
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.h
@@ -0,0 +1,39 @@+.. SPDX-License-Identifier: GPL-2.0++==============================+Trace Buffer Extension (TRBE).+==============================++ :Author: Anshuman Khandual <anshuman.khandual@arm.com>+ :Date: November 2020++Hardware Description+--------------------++Trace Buffer Extension (TRBE) is a percpu hardware which captures in system+memory, CPU traces generated from a corresponding percpu tracing unit. This+gets plugged in as a coresight sink device because the corresponding trace+genarators (ETE), are plugged in as source device.++The TRBE is not compliant to CoreSight architecture specifications, but is+driven via the CoreSight driver framework to support the ETE (which is+CoreSight compliant) integration.++Sysfs files and directories+---------------------------++The TRBE devices appear on the existing coresight bus alongside the other+coresight devices::++ >$ ls /sys/bus/coresight/devices+ trbe0 trbe1 trbe2 trbe3++The ``trbe<N>`` named TRBEs are associated with a CPU.::++ >$ ls /sys/bus/coresight/devices/trbe0/+ align dbm++*Key file items are:-*+*``align``: TRBE write pointer alignment+*``dbm``: TRBE updates memory with access and dirty flags+
To compile this driver as a module, choose M here: the
module will be called coresight-cti.
+config CORESIGHT_TRBE
+ bool "Trace Buffer Extension (TRBE) driver"
+ depends on ARM64
+ help
+ This driver provides support for percpu Trace Buffer Extension (TRBE).
+ TRBE always needs to be used along with it's corresponding percpu ETE
+ component. ETE generates trace data which is then captured with TRBE.
+ Unlike traditional sink devices, TRBE is a CPU feature accessible via
+ system registers. But it's explicit dependency with trace unit (ETE)
+ requires it to be plugged in as a coresight sink device.
+
config CORESIGHT_CTI_INTEGRATION_REGS
bool "Access CTI CoreSight Integration Registers"
depends on CORESIGHT_CTI
@@ -0,0 +1,1023 @@+// SPDX-License-Identifier: GPL-2.0+/*+*ThisdriverenablesTraceBufferExtension(TRBE)asaper-cpucoresight+*sinkdevicecouldthenpairwithanappropriateper-cpucoresightsource+*device(ETE)thusgeneratingrequiredtracedata.Tracecanbeenabled+*viatheperfframework.+*+*Copyright(C)2020ARMLtd.+*+*Author:AnshumanKhandual<anshuman.khandual@arm.com>+*/+#define DRVNAME "arm_trbe"++#define pr_fmt(fmt) DRVNAME ": " fmt++#include"coresight-trbe.h"++#define PERF_IDX2OFF(idx, buf) ((idx) % ((buf)->nr_pages << PAGE_SHIFT))++/*+*Apaddingpacketthatwillhelptheuserspacetools+*inskippingrelevantsectionsinthecapturedtrace+*datawhichcouldnotbedecoded.TRBEdoesn'tsupport+*formattingthetracedata,unlikethelegacyCoreSight+*sinksandthusweuseETEtracepacketstopadthe+*sectionsofthebuffer.+*/+#define ETE_IGNORE_PACKET 0x70++/*+*Minimumamountofmeaningfultracewillcontain:+*A-Sync,TraceInfo,TraceOn,Address,Atom.+*Thisisabout44bytesofETEtrace.Tobeon+*thesaferside,weassume64bytesistheminimum+*spacerequiredforameaningfulsession,before+*wehita"WRAP"event.+*/+#define TRBE_TRACE_MIN_BUF_SIZE 64++enumtrbe_fault_action{+TRBE_FAULT_ACT_WRAP,+TRBE_FAULT_ACT_SPURIOUS,+TRBE_FAULT_ACT_FATAL,+};++structtrbe_buf{+unsignedlongtrbe_base;+unsignedlongtrbe_limit;+unsignedlongtrbe_write;+intnr_pages;+void**pages;+boolsnapshot;+structtrbe_cpudata*cpudata;+};++structtrbe_cpudata{+booltrbe_dbm;+u64trbe_align;+intcpu;+enumcs_modemode;+structtrbe_buf*buf;+structtrbe_drvdata*drvdata;+};++structtrbe_drvdata{+structtrbe_cpudata__percpu*cpudata;+structperf_output_handle__percpu**handle;+structhlist_nodehotplug_node;+intirq;+cpumask_tsupported_cpus;+enumcpuhp_statetrbe_online;+structplatform_device*pdev;+};++staticinttrbe_alloc_node(structperf_event*event)+{+if(event->cpu==-1)+returnNUMA_NO_NODE;+returncpu_to_node(event->cpu);+}++staticvoidtrbe_drain_buffer(void)+{+asm(TSB_CSYNC);+dsb(nsh);+}++staticvoidtrbe_drain_and_disable_local(void)+{+trbe_drain_buffer();+write_sysreg_s(0,SYS_TRBLIMITR_EL1);+isb();+}++staticvoidtrbe_reset_local(void)+{+trbe_drain_and_disable_local();+write_sysreg_s(0,SYS_TRBPTR_EL1);+write_sysreg_s(0,SYS_TRBBASER_EL1);+write_sysreg_s(0,SYS_TRBSR_EL1);+}++staticvoidtrbe_stop_and_truncate_event(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);++/*+*Wecannotproceedwiththebuffercollectionandwe+*donothaveanydataforthecurrentsession.The+*etm_perfdriverexpectstocloseouttheaux_buffer+*atevent_stop().SodisabletheTRBEhereandleave+*theupdate_buffer()toreturna0size.+*/+trbe_drain_and_disable_local();+perf_aux_output_flag(handle,PERF_AUX_FLAG_TRUNCATED);+*this_cpu_ptr(buf->cpudata->drvdata->handle)=NULL;+}++/*+*TRBEBufferManagement+*+*TheTRBEbufferspansfromthebasepointertillthelimitpointer.Whenenabled,+*itstartswritingtracedatafromthewritepointeronwardtillthelimitpointer.+*Whenthewritepointerreachestheaddressjustbeforethelimitpointer,itgets+*wrappedaroundagaintothebasepointer.ThisiscalledaTRBEwrapevent,which+*generatesamaintenanceinterruptwhenoperatedinWRAPorFILLmode.Thisdriver+*usesFILLmode,wheretheTRBEstopsthetracecollectionatwrapevent.TheIRQ+*handlerupdatestheAUXbufferandre-enablestheTRBEwithupdatedWRITEand+*LIMITpointers.+*+*WraparoundwithanIRQ+*------<------<-------<-----<-----+*||+*------>------>------->----->-----+*+*+---------------+-----------------------++*|||+*+---------------+-----------------------++*BasePointerWritePointerLimitPointer+*+*ThebaseandlimitpointersalwaysneedstobePAGE_SIZEaligned.Butthewrite+*pointercanbealignedtotheimplementationdefinedTRBEtracebufferalignment+*ascapturedintrbe_cpudata->trbe_align.+*+*+*headtailwakeup+*+---------------------------------------+-----~~------+*|$$$$$$$|################|$$$$$$$$$$$$$$||+*+---------------------------------------+-----~~------+*BasePointerWritePointerLimitPointer+*+*Theperf_output_handleindices(head,tail,wakeup)aremonotonicallyincreasing+*valueswhichtracksallthedriverwritesanduserreadsfromtheperfauxiliary+*buffer.Generally[head..tail]istheareawherethedrivercanwriteintounless+*thewakeupisbehindthetail.EnabledTRBEbufferspanneedstobeadjustedand+*configureddependingontheperf_output_handleindices,sothatthedriverdoes+*notoverrideintoareasintheperfauxiliarybufferwhichisbeingoryettobe+*consumedfromtheuserspace.TheenabledTRBEbufferareaisamovingsubsetof+*theallocatedperfauxiliarybuffer.+*/+staticvoidtrbe_pad_buf(structperf_output_handle*handle,intlen)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);+u64head=PERF_IDX2OFF(handle->head,buf);++memset((void*)buf->trbe_base+head,ETE_IGNORE_PACKET,len);+if(!buf->snapshot)+perf_aux_output_skip(handle,len);+}++staticunsignedlongtrbe_snapshot_offset(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);++/*+*TheETEtracehasalignmentsynchronizationpacketsallowing+*thedecodertoresetincaseofanoverfloworcorruption.+*Sowecanusetheentirebufferforthesnapshotmode.+*/+returnbuf->nr_pages*PAGE_SIZE;+}++/*+*TRBELimitCalculation+*+*ThefollowingmarkersareusedtoillustratevariousTRBEbuffersituations.+*+*$$$$-Dataarea,unconsumedcapturedtracedata,nottobeoverridden+*####-Freearea,enabled,tracewillbewritten+*%%%%-Freearea,disabled,tracewillnotbewritten+*====-Freearea,paddedwithETE_IGNORE_PACKET,tracewillbeskipped+*/+staticunsignedlong__trbe_normal_offset(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);+structtrbe_cpudata*cpudata=buf->cpudata;+constu64bufsize=buf->nr_pages*PAGE_SIZE;+u64limit=bufsize;+u64head,tail,wakeup;++head=PERF_IDX2OFF(handle->head,buf);++/*+*head+*------->|+*|+*headTRBEaligntail+*+----|-------|---------------|-------++*|$$$$|=======|###############|$$$$$$$|+*+----|-------|---------------|-------++*trbe_basetrbe_base+nr_pages+*+*Perfauxbufferoutputheadpositioncanbemisaligneddependingon+*variousfactorsincludinguserspacereads.Incasemisaligned,head+*needstobealignedbeforeTRBEcanbeconfigured.Padthealignment+*gapwithETE_IGNORE_PACKETbytesthatwillbeignoredbyusertools+*andskipthissectionthusadvancingthehead.+*/+if(!IS_ALIGNED(head,cpudata->trbe_align)){+unsignedlongdelta=roundup(head,cpudata->trbe_align)-head;++delta=min(delta,handle->size);+trbe_pad_buf(handle,delta);+head=PERF_IDX2OFF(handle->head,buf);+}++/*+*head=tail(size=0)+*+----|-------------------------------++*|$$$$|$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$|+*+----|-------------------------------++*trbe_basetrbe_base+nr_pages+*+*Perfauxbufferdoesnothaveanyspaceforthedrivertowriteinto.+*Justcommunicatetracetruncationeventtotheuserspacebymarking+*itwithPERF_AUX_FLAG_TRUNCATED.+*/+if(!handle->size){+perf_aux_output_flag(handle,PERF_AUX_FLAG_TRUNCATED);+return0;+}++/* Compute the tail and wakeup indices now that we've aligned head */+tail=PERF_IDX2OFF(handle->head+handle->size,buf);+wakeup=PERF_IDX2OFF(handle->wakeup,buf);++/*+*LetscalculatethebufferareawhichTRBEcouldwriteinto.There+*arethreepossiblescenarioshere.Limitneedstobealignedwith+*PAGE_SIZEpertheTRBErequirement.Alwaysavoidclobberingthe+*unconsumeddata.+*+*1)head<tail+*+*headtail+*+----|-----------------------|-------++*|$$$$|#######################|$$$$$$$|+*+----|-----------------------|-------++*trbe_baselimittrbe_base+nr_pages+*+*TRBEcouldwriteinto[head..tail]area.Unlessthetailisrightat+*theendofthebuffer,neitheranwraparoundnoranIRQisexpected+*whilebeingenabled.+*+*2)head==tail+*+*head=tail(size>0)+*+----|-------------------------------++*|%%%%|###############################|+*+----|-------------------------------++*trbe_baselimit=trbe_base+nr_pages+*+*TRBEshouldjustwriteinto[head..base+nr_pages]areaeventhough+*theentirebufferisempty.Reasonbeing,whenthetracereachesthe+*endofthebuffer,itwilljustwraparoundwithanIRQgivingan+*opportunitytoreconfigurethebuffer.+*+*3)tail<head+*+*tailhead+*+----|-----------------------|-------++*|%%%%|$$$$$$$$$$$$$$$$$$$$$$$|#######|+*+----|-----------------------|-------++*trbe_baselimit=trbe_base+nr_pages+*+*TRBEshouldjustwriteinto[head..base+nr_pages]areaeventhough+*the[trbe_base..tail]isalsoempty.Reasonbeing,whenthetrace+*reachestheendofthebuffer,itwilljustwraparoundwithanIRQ+*givinganopportunitytoreconfigurethebuffer.+*/+if(head<tail)+limit=round_down(tail,PAGE_SIZE);++/*+*Wakeupmaybearbitrarilyfarintothefuture.Ifit'snotinthe+*currentgeneration,eitherwe'llwrapbeforehittingit,orit's+*inthepastandhasbeenhandledalready.+*+*Ifthere'sawakeupbeforewewrap,arrangetobewokenupbythe+*pageboundaryfollowingit.Keepthetailboundaryifthat'slower.+*+*headwakeuptail+*+----|---------------|-------|-------++*|$$$$|###############|%%%%%%%|$$$$$$$|+*+----|---------------|-------|-------++*trbe_baselimittrbe_base+nr_pages+*/+if(handle->wakeup<(handle->head+handle->size)&&head<=wakeup)+limit=min(limit,round_up(wakeup,PAGE_SIZE));++/*+*Therearetwosituationwhenthiscanhappeni.elimitisbefore+*theheadandhenceTRBEcannotbeconfigured.+*+*1)head<tail(aligneddownwithPAGE_SIZE)andalsotheyareboth+*withinthesamePAGEsizerange.+*+*PAGE_SIZE+*|----------------------|+*+*limitheadtail+*+------------|------|--------|-------++*|$$$$$$$$$$$$$$$$$$$|========|$$$$$$$|+*+------------|------|--------|-------++*trbe_basetrbe_base+nr_pages+*+*2)head<wakeup(alignedupwithPAGE_SIZE)<tailandalsoboth+*headandwakeuparewithinsamePAGEsizerange.+*+*PAGE_SIZE+*|----------------------|+*+*limitheadwakeuptail+*+----|------|-------|--------|-------++*|$$$$$$$$$$$|=======|========|$$$$$$$|+*+----|------|-------|--------|-------++*trbe_basetrbe_base+nr_pages+*/+if(limit>head)+returnlimit;++trbe_pad_buf(handle,handle->size);+perf_aux_output_flag(handle,PERF_AUX_FLAG_TRUNCATED);+return0;+}++staticunsignedlongtrbe_normal_offset(structperf_output_handle*handle)+{+structtrbe_buf*buf=perf_get_aux(handle);+u64limit=__trbe_normal_offset(handle);+u64head=PERF_IDX2OFF(handle->head,buf);++/*+*Iftheheadistooclosetothelimitandwedon't+*havespaceforameaningfulrun,weratherpadit+*andstartfresh.+*/+if(limit&&(limit-head<TRBE_TRACE_MIN_BUF_SIZE)){+trbe_pad_buf(handle,limit-head);+limit=__trbe_normal_offset(handle);+}+returnlimit;+}++staticunsignedlongcompute_trbe_buffer_limit(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);+unsignedlongoffset;++if(buf->snapshot)+offset=trbe_snapshot_offset(handle);+else+offset=trbe_normal_offset(handle);+returnbuf->trbe_base+offset;+}++staticvoidclr_trbe_status(void)+{+u64trbsr=read_sysreg_s(SYS_TRBSR_EL1);++WARN_ON(is_trbe_enabled());+trbsr&=~TRBSR_IRQ;+trbsr&=~TRBSR_TRG;+trbsr&=~TRBSR_WRAP;+trbsr&=~(TRBSR_EC_MASK<<TRBSR_EC_SHIFT);+trbsr&=~(TRBSR_BSC_MASK<<TRBSR_BSC_SHIFT);+trbsr&=~TRBSR_STOP;+write_sysreg_s(trbsr,SYS_TRBSR_EL1);+}++staticvoidset_trbe_limit_pointer_enabled(unsignedlongaddr)+{+u64trblimitr=read_sysreg_s(SYS_TRBLIMITR_EL1);++WARN_ON(!IS_ALIGNED(addr,(1UL<<TRBLIMITR_LIMIT_SHIFT)));+WARN_ON(!IS_ALIGNED(addr,PAGE_SIZE));++trblimitr&=~TRBLIMITR_NVM;+trblimitr&=~(TRBLIMITR_FILL_MODE_MASK<<TRBLIMITR_FILL_MODE_SHIFT);+trblimitr&=~(TRBLIMITR_TRIG_MODE_MASK<<TRBLIMITR_TRIG_MODE_SHIFT);+trblimitr&=~(TRBLIMITR_LIMIT_MASK<<TRBLIMITR_LIMIT_SHIFT);++/*+*Filltracebuffermodeisusedherewhileconfiguringthe+*TRBEfortracecapture.Inthisparticularmode,thetrace+*collectionisstoppedandamaintenanceinterruptisraised+*whenthecurrentwritepointerwraps.Thispauseintrace+*collectiongivesthesoftwareanopportunitytocapturethe+*tracedataintheinterrupthandler,beforereconfiguring+*theTRBE.+*/+trblimitr|=(TRBE_FILL_MODE_FILL&TRBLIMITR_FILL_MODE_MASK)<<TRBLIMITR_FILL_MODE_SHIFT;++/*+*TriggermodeisnotusedherewhileconfiguringtheTRBEfor+*thetracecapture.Hencejustkeepthisintheignoremode.+*/+trblimitr|=(TRBE_TRIG_MODE_IGNORE&TRBLIMITR_TRIG_MODE_MASK)<<TRBLIMITR_TRIG_MODE_SHIFT;+trblimitr|=(addr&PAGE_MASK);++trblimitr|=TRBLIMITR_ENABLE;+write_sysreg_s(trblimitr,SYS_TRBLIMITR_EL1);
Personally, I prefer the isb() here rather than at the caller site, to make sure
it is all contained here and more importantly we don't execute any other instruction
before the "isb()" as the tracing may be enabled for the kernel.
Sure, will move the isb() here along with its comment.
quoted
+}
+
+static void trbe_enable_hw(struct trbe_buf *buf)
+{
+ WARN_ON(buf->trbe_write < buf->trbe_base); > + WARN_ON(buf->trbe_write >= buf->trbe_limit);
+ set_trbe_disabled();
+ isb();
+ clr_trbe_status();
+ set_trbe_base_pointer(buf->trbe_base);
+ set_trbe_write_pointer(buf->trbe_write);
+
+ /*
+ * Synchronize all the register updates
+ * till now before enabling the TRBE.
+ */
+ isb();
+ set_trbe_limit_pointer_enabled(buf->trbe_limit);
+
+ /* Synchronize the TRBE enable event */
+ isb();
+}
+
+static void *arm_trbe_alloc_buffer(struct coresight_device *csdev,
+ struct perf_event *event, void **pages,
+ int nr_pages, bool snapshot)
+{
+ struct trbe_buf *buf;
+ struct page **pglist;
+ int i;
+
+ /*
+ * TRBE LIMIT and TRBE WRITE pointers must be page aligned. But with
+ * just a single page, there is not much room left while writing into
+ * a partially filled TRBE buffer. Hence restrict the minimum buffer
+ * size as two pages.
+ */
+ if (nr_pages < 2)
+ return NULL;
+
+ buf = kzalloc_node(sizeof(*buf), GFP_KERNEL, trbe_alloc_node(event));
+ if (IS_ERR(buf))
+ return ERR_PTR(-ENOMEM);
+
+ pglist = kcalloc(nr_pages, sizeof(*pglist), GFP_KERNEL);
+ if (IS_ERR(pglist)) {
+ kfree(buf);
+ return ERR_PTR(-ENOMEM);
+ }
+
+ for (i = 0; i < nr_pages; i++)
+ pglist[i] = virt_to_page(pages[i]);
+
+ buf->trbe_base = (unsigned long) vmap(pglist, nr_pages, VM_MAP, PAGE_KERNEL);
Please return 0 here. As we are expected to pass an "unsigned" size.
Sure, will do.
quoted
+
+ /*
+ * If the TRBE was disabled due to lack of space in the AUX buffer or a
+ * spurious fault, the driver leaves it disabled, truncating the buffer.
+ * Since the etm_perf driver expects to close out the AUX buffer, the
+ * driver skips it. Thus, just pass in 0 size here to indicate that the
+ * buffer was truncated.
+ */
+ if (!is_trbe_enabled())
+ return 0;
+ /*
+ * perf handle structure needs to be shared with the TRBE IRQ handler for
+ * capturing trace data and restarting the handle. There is a probability
+ * of an undefined reference based crash when etm event is being stopped
+ * while a TRBE IRQ also getting processed. This happens due the release
+ * of perf handle via perf_aux_output_end() in etm_event_stop(). Stopping
+ * the TRBE here will ensure that no IRQ could be generated when the perf
+ * handle gets freed in etm_event_stop().
+ */
+ trbe_drain_and_disable_local();
+ offset = get_trbe_write_pointer() - get_trbe_base_pointer();
+ size = offset - PERF_IDX2OFF(handle->head, buf);
It may be a good idea to verify that the size computation here doesn't overflow.
e.g broken interrupt could potentially leave us with "write == base". I understand
this is a hardware issue, but the software can be resilient to not send bogus
results.
We may add a comment here to explain how this would be handled ? e.g,
/*
* We are unable to restart the trace collection,
* thus leave the TRBE disabled. The etm-perf driver
* is able to detect this with a disconnnected handle
* (handle->event = NULL).
*/
We may want to ensure that handle is not NULL, which can only happen if the
hardware is not following the software.
if (WARN_ON_ONCE(!handle) || !perf_get_aux(handle))
From: Suzuki K Poulose <suzuki.poulose@arm.com> Date: 2021-02-02 09:44:53
On 2/1/21 11:17 PM, Mathieu Poirier wrote:
Hi Anshuman,
I have started reviewing this set. As it is quite voluminous comments will
come over serveral days. I will let you know when I am done.
On Wed, Jan 27, 2021 at 02:25:25PM +0530, Anshuman Khandual wrote:
quoted
From: Suzuki K Poulose <suzuki.poulose@arm.com>
When there are multiple sinks on the system, in the absence
of a specified sink, it is quite possible that a default sink
for an ETM could be different from that of another ETM. However
we do not support having multiple sinks for an event yet. This
patch allows the event to use the default sinks on the ETMs
where they are scheduled as long as the sinks are of the same
type.
e.g, if we have 1x1 topology with per-CPU ETRs, the event can
use the per-CPU ETR for the session. However, if the sinks
are of different type, e.g TMC-ETR on one and a custom sink
on another, the event will only trace on the first detected
sink.
I found the above changelog very confusing - I read it several times and still
couldn't get all of it. In the end this patch prevents sinks of different types
from being used for session, and this is what the text should reflect.
Sorry about that. Your inference is correct, but it is only a side effect
of the primary motive. How about the following :
"When a sink is not specified by the user, the etm perf driver
finds a suitable sink automatically based on the first ETM, where
this event could be scheduled. Then we allocate the sink buffer based
on the selected sink. This is fine for a CPU bound event as the "sink"
is always guaranteed to be reachable from the ETM (as this is the only
ETM where the event is going to be scheduled). However, if we have a task
bound event, the event could be scheduled on any of the ETMs on the
system. In this case, currently we automatically select a sink and exclude
any ETMs that are not reachable from the selected sink. This is
problematic for 1x1 configurations as we end up in tracing the event
only on the "first" ETM, as the default sink is local to the first
ETM and unreachable from the rest.
However, we could allow the other ETMs to trace if they all have a
sink that is compatible with the "selected" sink and can use the
sink buffer. This can be easily done by verifying that they are
all driven by the same driver and matches the same subtype."
@@ -249,21 +258,35 @@ static void *etm_setup_aux(struct perf_event *event, void **pages, } /*- * No sink provided - look for a default sink for one of the- * devices. At present we only support topology where all CPUs- * use the same sink [N:1], so only need to find one sink. The- * coresight_build_path later will remove any CPU that does not- * attach to the sink, or if we have not found a sink.+ * No sink provided - look for a default sink for all the devices.+ * We only support multiple sinks, only if all the default sinks+ * are of the same type, so that the sink buffer can be shared+ * as the event moves around. We don't trace on a CPU if it can't
s/can't/can't./
quoted
+ *
Extra line
OK
quoted
*/
- if (!sink)
- sink = coresight_find_default_sink(csdev);
+ if (!sink_forced) {
+ new_sink = coresight_find_default_sink(csdev);
+ if (!new_sink) {
+ cpumask_clear_cpu(cpu, mask);
+ continue;
+ }
+ /* Skip checks for the first sink */
+ if (!sink) {
+ sink = new_sink;
+ } else if (!sinks_match(new_sink, sink)) {
+ cpumask_clear_cpu(cpu, mask);
+ continue;
+ }
+ } else {
+ new_sink = sink;
+ }
if (!user_sink) {
/* find default sink for this CPU */
sink = coresight_find_default_sink(csdev);
if (!sink) {
cpumask_clear_cpu(cpu, mask);
continue;
}
/* Chech new sink with last sink */
if (last_sink && !sink_match(last_sink, sink)) {
cpumask_clear_cpu(cpu, mask);
continue;
}
last_sink = sink;
} else {
sink = user_sink;
}
From: Mike Leach <hidden> Date: 2021-02-02 11:11:45
Hi Ansuman,
On Wed, 27 Jan 2021 at 08:55, Anshuman Khandual
[off-list ref] wrote:
quoted hunk
From: Suzuki K Poulose <suzuki.poulose@arm.com>
If a graph node is not found for a given node, of_get_next_endpoint()
will emit the following error message :
OF: graph: no port node found in /<node_name>
If the given component doesn't have any explicit connections (e.g,
ETE) we could simply ignore the graph parsing.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
drivers/hwtracing/coresight/coresight-platform.c | 6 ++++++
1 file changed, 6 insertions(+)
The problem here is that you are masking genuine errors.
The solution is to either call this only if the device type is one
that ports are not required - i.e. ETE, or upgrade the .dts bindings
for the rest of the ETM devices to yaml so that the ports requirement
is checked and validated there.
Regards
Mike
do {
ep = of_graph_get_next_endpoint(node, ep);
if (!ep)
--
2.7.4
--
Mike Leach
Principal Engineer, ARM Ltd.
Manchester Design Centre. UK
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: Suzuki K Poulose <suzuki.poulose@arm.com> Date: 2021-02-02 14:42:48
Hi Mike
On 2/2/21 11:10 AM, Mike Leach wrote:
Hi Ansuman,
On Wed, 27 Jan 2021 at 08:55, Anshuman Khandual
[off-list ref] wrote:
quoted
From: Suzuki K Poulose <suzuki.poulose@arm.com>
If a graph node is not found for a given node, of_get_next_endpoint()
will emit the following error message :
OF: graph: no port node found in /<node_name>
If the given component doesn't have any explicit connections (e.g,
ETE) we could simply ignore the graph parsing.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
drivers/hwtracing/coresight/coresight-platform.c | 6 ++++++
1 file changed, 6 insertions(+)
The problem here is that you are masking genuine errors.
If the graph is not described for a component, where it is
mandatory, it won't be usable by the driver and as such using
the devices will fail.
e.g, if an ETM misses the bindings, tracing will fail. (in either
mode).
The solution is to either call this only if the device type is one
that ports are not required - i.e. ETE, or upgrade the .dts bindings
The proposed change is too invasive and is not worth the benefit
that it brings.
The side effect of this patch is, if someone makes a mistake in the
bindings they don't see the "warning" in the dmesg. But will definitely
hit the issue when trying to use the system.
i.e, Functionally there is no change.
On the other hand issuing a warning message for ETE is confusing for
a well behaved user.
for the rest of the ETM devices to yaml so that the ports requirement
is checked and validated there.
This is a step that we must take, but in a separate series. And I
don't think this will solve handling non-compliant DTs *immediately*,
as there could be :
a) DTS that are not upstream (Quite common for CoreSight)
b) People are getting used to the schema and running schema checks.
So, personally I vote for :
1) Merge this patch in as is
2) Convert the bindings to Yaml in a separate series.
Suzuki
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
On Tue, Feb 02, 2021 at 09:42:34AM +0000, Suzuki K Poulose wrote:
On 2/1/21 11:17 PM, Mathieu Poirier wrote:
quoted
Hi Anshuman,
I have started reviewing this set. As it is quite voluminous comments will
come over serveral days. I will let you know when I am done.
On Wed, Jan 27, 2021 at 02:25:25PM +0530, Anshuman Khandual wrote:
quoted
From: Suzuki K Poulose <suzuki.poulose@arm.com>
When there are multiple sinks on the system, in the absence
of a specified sink, it is quite possible that a default sink
for an ETM could be different from that of another ETM. However
we do not support having multiple sinks for an event yet. This
patch allows the event to use the default sinks on the ETMs
where they are scheduled as long as the sinks are of the same
type.
e.g, if we have 1x1 topology with per-CPU ETRs, the event can
use the per-CPU ETR for the session. However, if the sinks
are of different type, e.g TMC-ETR on one and a custom sink
on another, the event will only trace on the first detected
sink.
I found the above changelog very confusing - I read it several times and still
couldn't get all of it. In the end this patch prevents sinks of different types
from being used for session, and this is what the text should reflect.
Sorry about that. Your inference is correct, but it is only a side effect
of the primary motive. How about the following :
"When a sink is not specified by the user, the etm perf driver
finds a suitable sink automatically based on the first ETM, where
this event could be scheduled. Then we allocate the sink buffer based
on the selected sink. This is fine for a CPU bound event as the "sink"
is always guaranteed to be reachable from the ETM (as this is the only
ETM where the event is going to be scheduled). However, if we have a task
bound event, the event could be scheduled on any of the ETMs on the
system. In this case, currently we automatically select a sink and exclude
any ETMs that are not reachable from the selected sink. This is
problematic for 1x1 configurations as we end up in tracing the event
only on the "first" ETM, as the default sink is local to the first
ETM and unreachable from the rest.
However, we could allow the other ETMs to trace if they all have a
sink that is compatible with the "selected" sink and can use the
sink buffer. This can be easily done by verifying that they are
all driven by the same driver and matches the same subtype."
I think we can tighten this by verifying the dev->sub_type matches too.
We could do that but I'm not sure we need to. I remember spending a few minutes
yesterday thinking about ways to make the test more stringent but in the end I
thought what you had was sufficient, at least for now. I'll leave that one to
you - proceed as you see fit.
@@ -249,21 +258,35 @@ static void *etm_setup_aux(struct perf_event *event, void **pages, } /*- * No sink provided - look for a default sink for one of the- * devices. At present we only support topology where all CPUs- * use the same sink [N:1], so only need to find one sink. The- * coresight_build_path later will remove any CPU that does not- * attach to the sink, or if we have not found a sink.+ * No sink provided - look for a default sink for all the devices.+ * We only support multiple sinks, only if all the default sinks+ * are of the same type, so that the sink buffer can be shared+ * as the event moves around. We don't trace on a CPU if it can't
s/can't/can't./
quoted
+ *
Extra line
OK
quoted
quoted
*/
- if (!sink)
- sink = coresight_find_default_sink(csdev);
+ if (!sink_forced) {
+ new_sink = coresight_find_default_sink(csdev);
+ if (!new_sink) {
+ cpumask_clear_cpu(cpu, mask);
+ continue;
+ }
+ /* Skip checks for the first sink */
+ if (!sink) {
+ sink = new_sink;
+ } else if (!sinks_match(new_sink, sink)) {
+ cpumask_clear_cpu(cpu, mask);
+ continue;
+ }
+ } else {
+ new_sink = sink;
+ }
if (!user_sink) {
/* find default sink for this CPU */
sink = coresight_find_default_sink(csdev);
if (!sink) {
cpumask_clear_cpu(cpu, mask);
continue;
}
/* Chech new sink with last sink */
if (last_sink && !sink_match(last_sink, sink)) {
cpumask_clear_cpu(cpu, mask);
continue;
}
last_sink = sink;
} else {
sink = user_sink;
}
From: Mike Leach <hidden> Date: 2021-02-02 16:39:55
Hi,
On Tue, 2 Feb 2021 at 09:42, Suzuki K Poulose [off-list ref] wrote:
On 2/1/21 11:17 PM, Mathieu Poirier wrote:
quoted
Hi Anshuman,
I have started reviewing this set. As it is quite voluminous comments will
come over serveral days. I will let you know when I am done.
On Wed, Jan 27, 2021 at 02:25:25PM +0530, Anshuman Khandual wrote:
quoted
From: Suzuki K Poulose <suzuki.poulose@arm.com>
When there are multiple sinks on the system, in the absence
of a specified sink, it is quite possible that a default sink
for an ETM could be different from that of another ETM. However
we do not support having multiple sinks for an event yet. This
patch allows the event to use the default sinks on the ETMs
where they are scheduled as long as the sinks are of the same
type.
e.g, if we have 1x1 topology with per-CPU ETRs, the event can
use the per-CPU ETR for the session. However, if the sinks
are of different type, e.g TMC-ETR on one and a custom sink
on another, the event will only trace on the first detected
sink.
I found the above changelog very confusing - I read it several times and still
couldn't get all of it. In the end this patch prevents sinks of different types
from being used for session, and this is what the text should reflect.
Sorry about that. Your inference is correct, but it is only a side effect
of the primary motive. How about the following :
"When a sink is not specified by the user, the etm perf driver
finds a suitable sink automatically based on the first ETM, where
this event could be scheduled. Then we allocate the sink buffer based
on the selected sink. This is fine for a CPU bound event as the "sink"
is always guaranteed to be reachable from the ETM (as this is the only
ETM where the event is going to be scheduled). However, if we have a task
bound event, the event could be scheduled on any of the ETMs on the
system. In this case, currently we automatically select a sink and exclude
any ETMs that are not reachable from the selected sink. This is
problematic for 1x1 configurations as we end up in tracing the event
only on the "first" ETM, as the default sink is local to the first
ETM and unreachable from the rest.
However, we could allow the other ETMs to trace if they all have a
sink that is compatible with the "selected" sink and can use the
sink buffer. This can be easily done by verifying that they are
all driven by the same driver and matches the same subtype."
Not sure that the logic here makes total sense - I can't see _why_
multiple sinks need to be of the same type.
1) This patch is designed to allow multiple sinks to be used in a 1:1
topology system - but there is no specific restriction here - and N:M
should work on the same basis
2) This implies that multiple sinks will work within the coresight
infrastucture.
3) The sink interface -> struct coresight_ops_sink allows sinks to be
abstracted - therefore whichever sink is chosen the coresight
infrastructure calls the operations for the given sink.
4) Each individual sink, will have its own hardware buffer - copied
into the perf buffers at some appropriate point.
Thus if the users specifies a selected sink - we need to eliminate any
source that cannot reach it.
If not we need to find the relevant default sink for the source, which
might be a shared ETR, or per CPU TRBE / ETR, and the abstraction
logic ought to handle getting the captured data to the correct place.
If it doesn't then we are on shaky ground with any multiple sink
solution.
On the face of it - type is irrelevant. If I am missing something -
this patch needs a better explanation.
@@ -222,6 +230,7 @@ static void *etm_setup_aux(struct perf_event *event, void **pages, if (event->attr.config2) { id = (u32)event->attr.config2; sink = coresight_get_sink_by_id(id);
user_sink = coresight_get_sink_by_id(id);
quoted
+ sink_forced = true;
The comment for this block uses the term "selected sink", and the
functions use _default_sink . This may read better if we rename the
bool to "selected_sink" rather than "sink_forced"
Regards
Mike
@@ -249,21 +258,35 @@ static void *etm_setup_aux(struct perf_event *event, void **pages, } /*- * No sink provided - look for a default sink for one of the- * devices. At present we only support topology where all CPUs- * use the same sink [N:1], so only need to find one sink. The- * coresight_build_path later will remove any CPU that does not- * attach to the sink, or if we have not found a sink.+ * No sink provided - look for a default sink for all the devices.+ * We only support multiple sinks, only if all the default sinks+ * are of the same type, so that the sink buffer can be shared+ * as the event moves around. We don't trace on a CPU if it can't
s/can't/can't./
quoted
+ *
Extra line
OK
quoted
quoted
*/
- if (!sink)
- sink = coresight_find_default_sink(csdev);
+ if (!sink_forced) {
+ new_sink = coresight_find_default_sink(csdev);
+ if (!new_sink) {
+ cpumask_clear_cpu(cpu, mask);
+ continue;
+ }
+ /* Skip checks for the first sink */
+ if (!sink) {
+ sink = new_sink;
+ } else if (!sinks_match(new_sink, sink)) {
+ cpumask_clear_cpu(cpu, mask);
+ continue;
+ }
+ } else {
+ new_sink = sink;
+ }
if (!user_sink) {
/* find default sink for this CPU */
sink = coresight_find_default_sink(csdev);
if (!sink) {
cpumask_clear_cpu(cpu, mask);
continue;
}
/* Chech new sink with last sink */
if (last_sink && !sink_match(last_sink, sink)) {
cpumask_clear_cpu(cpu, mask);
continue;
}
last_sink = sink;
} else {
sink = user_sink;
}
Agreed, it is much better readable.
Suzuki
--
Mike Leach
Principal Engineer, ARM Ltd.
Manchester Design Centre. UK
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
On Wed, Jan 27, 2021 at 02:25:27PM +0530, Anshuman Khandual wrote:
From: Suzuki K Poulose <suzuki.poulose@arm.com>
ETE may not implement the OS lock and instead could rely on
the PE OS Lock for the trace unit access. This is indicated
by the TRCOLSR.OSM == 0b100. Add support for handling the
PE OS lock
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
drivers/hwtracing/coresight/coresight-etm4x-core.c | 50 ++++++++++++++++++----
drivers/hwtracing/coresight/coresight-etm4x.h | 15 +++++++
2 files changed, 56 insertions(+), 9 deletions(-)
@@ -114,30 +114,59 @@ void etm4x_sysreg_write(u64 val, u32 offset, bool _relaxed, bool _64bit)}}-staticvoidetm4_os_unlock_csa(structetmv4_drvdata*drvdata,structcsdev_access*csa)+staticvoidetm_detect_os_lock(structetmv4_drvdata*drvdata,+structcsdev_access*csa){-/* Writing 0 to TRCOSLAR unlocks the trace registers */-etm4x_relaxed_write32(csa,0x0,TRCOSLAR);-drvdata->os_unlock=true;+u32oslsr=etm4x_relaxed_read32(csa,TRCOSLSR);++drvdata->os_lock_model=ETM_OSLSR_OSLM(oslsr);+}++staticvoidetm_write_os_lock(structetmv4_drvdata*drvdata,+structcsdev_access*csa,u32val)+{+val=!!val;++switch(drvdata->os_lock_model){+caseETM_OSLOCK_PRESENT:+etm4x_relaxed_write32(csa,val,TRCOSLAR);+break;+caseETM_OSLOCK_PE:+write_sysreg_s(val,SYS_OSLAR_EL1);+break;+default:+pr_warn_once("CPU%d: Unsupported Trace OSLock model: %x\n",+smp_processor_id(),drvdata->os_lock_model);+fallthrough;+caseETM_OSLOCK_NI:+return;+}isb();}+staticinlinevoidetm4_os_unlock_csa(structetmv4_drvdata*drvdata,+structcsdev_access*csa)+{+WARN_ON(drvdata->cpu!=smp_processor_id());++/* Writing 0 to OS Lock unlocks the trace unit registers */+etm_write_os_lock(drvdata,csa,0x0);+drvdata->os_unlock=true;+}+staticvoidetm4_os_unlock(structetmv4_drvdata*drvdata){if(!WARN_ON(!drvdata->csdev))etm4_os_unlock_csa(drvdata,&drvdata->csdev->access);-}staticvoidetm4_os_lock(structetmv4_drvdata*drvdata){if(WARN_ON(!drvdata->csdev))return;--/* Writing 0x1 to TRCOSLAR locks the trace registers */-etm4x_relaxed_write32(&drvdata->csdev->access,0x1,TRCOSLAR);+/* Writing 0x1 to OS Lock locks the trace registers */+etm_write_os_lock(drvdata,&drvdata->csdev->access,0x1);drvdata->os_unlock=false;-isb();}staticvoidetm4_cs_lock(structetmv4_drvdata*drvdata,
@@ -906,6 +935,9 @@ static void etm4_init_arch_data(void *info)if(!etm4_init_csdev_access(drvdata,csa))return;+/* Detect the support for OS Lock before we actuall use it */+etm_detect_os_lock(drvdata,csa);+/* Make sure all registers are accessible */etm4_os_unlock_csa(drvdata,csa);etm4_cs_unlock(drvdata,csa);
On Wed, Jan 27, 2021 at 02:25:28PM +0530, Anshuman Khandual wrote:
quoted hunk
From: Suzuki K Poulose <suzuki.poulose@arm.com>
Add support for handling the system registers for Embedded Trace
Extensions (ETE). ETE shares most of the registers with ETMv4 except
for some and also adds some new registers. Re-arrange the ETMv4x list
to share the common definitions and add the ETE sysreg support.
Cc: Mike Leach <redacted>
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
drivers/hwtracing/coresight/coresight-etm4x-core.c | 32 +++++++++++++
drivers/hwtracing/coresight/coresight-etm4x.h | 52 ++++++++++++++++++----
2 files changed, 75 insertions(+), 9 deletions(-)
+ }
+
+ if (!_relaxed)
+ __iormb(res); /* Imitate the !relaxed I/O helpers */
+
+ return res;
+}
+
+void ete_sysreg_write(u64 val, u32 offset, bool _relaxed, bool _64bit)
+{
+ if (!_relaxed)
+ __iowmb(); /* Imitate the !relaxed I/O helpers */
+ if (!_64bit)
+ val &= GENMASK(31, 0);
+
+ switch (offset) {
+ ETE_WRITE_CASES(val)
+ default :
+ WARN_ONCE(1, "ete: trying to write to unsupported register @%x\n",
+ offset);
Alignment
+ }
+}
The etm4x_sysreg_xyz() equivalent of these use a pr_warn_ratelimited() rather
than a WARN_ONE().
With that:
Reviewed-by: Mathieu Poirier <mathieu.poirier@linaro.org>
@@ -160,10 +162,22 @@#define CASE_NOP(__unused, x) \case(x):/* fall through */+#define ETE_ONLY_SYSREG_LIST(op, val) \+CASE_##op((val),TRCRSR)\+CASE_##op((val),TRCEXTINSELRn(1))\+CASE_##op((val),TRCEXTINSELRn(2))\+CASE_##op((val),TRCEXTINSELRn(3))+/* List of registers accessible via System instructions */-#define ETM_SYSREG_LIST(op, val) \-CASE_##op((val),TRCPRGCTLR)\+#define ETM4x_ONLY_SYSREG_LIST(op, val) \CASE_##op((val),TRCPROCSELR)\+CASE_##op((val),TRCVDCTLR)\+CASE_##op((val),TRCVDSACCTLR)\+CASE_##op((val),TRCVDARCCTLR)\+CASE_##op((val),TRCOSLAR)++#define ETM_COMMON_SYSREG_LIST(op, val) \+CASE_##op((val),TRCPRGCTLR)\CASE_##op((val),TRCSTATR)\CASE_##op((val),TRCCONFIGR)\CASE_##op((val),TRCAUXCTLR)\
On Wed, Jan 27, 2021 at 02:25:27PM +0530, Anshuman Khandual wrote:
quoted hunk
From: Suzuki K Poulose <suzuki.poulose@arm.com>
ETE may not implement the OS lock and instead could rely on
the PE OS Lock for the trace unit access. This is indicated
by the TRCOLSR.OSM == 0b100. Add support for handling the
PE OS lock
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
drivers/hwtracing/coresight/coresight-etm4x-core.c | 50 ++++++++++++++++++----
drivers/hwtracing/coresight/coresight-etm4x.h | 15 +++++++
2 files changed, 56 insertions(+), 9 deletions(-)
@@ -114,30 +114,59 @@ void etm4x_sysreg_write(u64 val, u32 offset, bool _relaxed, bool _64bit)}}-staticvoidetm4_os_unlock_csa(structetmv4_drvdata*drvdata,structcsdev_access*csa)+staticvoidetm_detect_os_lock(structetmv4_drvdata*drvdata,+structcsdev_access*csa){-/* Writing 0 to TRCOSLAR unlocks the trace registers */-etm4x_relaxed_write32(csa,0x0,TRCOSLAR);-drvdata->os_unlock=true;+u32oslsr=etm4x_relaxed_read32(csa,TRCOSLSR);++drvdata->os_lock_model=ETM_OSLSR_OSLM(oslsr);+}++staticvoidetm_write_os_lock(structetmv4_drvdata*drvdata,+structcsdev_access*csa,u32val)+{+val=!!val;++switch(drvdata->os_lock_model){+caseETM_OSLOCK_PRESENT:+etm4x_relaxed_write32(csa,val,TRCOSLAR);+break;+caseETM_OSLOCK_PE:+write_sysreg_s(val,SYS_OSLAR_EL1);+break;+default:+pr_warn_once("CPU%d: Unsupported Trace OSLock model: %x\n",+smp_processor_id(),drvdata->os_lock_model);+fallthrough;+caseETM_OSLOCK_NI:+return;+}isb();}+staticinlinevoidetm4_os_unlock_csa(structetmv4_drvdata*drvdata,+structcsdev_access*csa)+{+WARN_ON(drvdata->cpu!=smp_processor_id());++/* Writing 0 to OS Lock unlocks the trace unit registers */+etm_write_os_lock(drvdata,csa,0x0);+drvdata->os_unlock=true;+}+staticvoidetm4_os_unlock(structetmv4_drvdata*drvdata){if(!WARN_ON(!drvdata->csdev))etm4_os_unlock_csa(drvdata,&drvdata->csdev->access);-}staticvoidetm4_os_lock(structetmv4_drvdata*drvdata){if(WARN_ON(!drvdata->csdev))return;--/* Writing 0x1 to TRCOSLAR locks the trace registers */-etm4x_relaxed_write32(&drvdata->csdev->access,0x1,TRCOSLAR);+/* Writing 0x1 to OS Lock locks the trace registers */+etm_write_os_lock(drvdata,&drvdata->csdev->access,0x1);drvdata->os_unlock=false;-isb();}staticvoidetm4_cs_lock(structetmv4_drvdata*drvdata,
@@ -906,6 +935,9 @@ static void etm4_init_arch_data(void *info)if(!etm4_init_csdev_access(drvdata,csa))return;+/* Detect the support for OS Lock before we actuall use it */
s/actuall/actually
quoted hunk
+ etm_detect_os_lock(drvdata, csa);
+
/* Make sure all registers are accessible */
etm4_os_unlock_csa(drvdata, csa);
etm4_cs_unlock(drvdata, csa);
On Wed, Jan 27, 2021 at 02:25:29PM +0530, Anshuman Khandual wrote:
quoted hunk
From: Suzuki K Poulose <suzuki.poulose@arm.com>
Add ETE as one of the supported device types we support
with ETM4x driver. The devices are named following the
existing convention as ete<N>.
ETE mandates that the trace resource status register is programmed
before the tracing is turned on. For the moment simply write to
it indicating TraceActive.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
drivers/hwtracing/coresight/Kconfig | 10 ++--
drivers/hwtracing/coresight/coresight-etm4x-core.c | 56 +++++++++++++++++-----
.../hwtracing/coresight/coresight-etm4x-sysfs.c | 19 ++++++--
drivers/hwtracing/coresight/coresight-etm4x.h | 16 ++++++-
4 files changed, 79 insertions(+), 22 deletions(-)
@@ -431,6 +431,13 @@ static int etm4_enable_hw(struct etmv4_drvdata *drvdata)etm4x_relaxed_write32(csa,trcpdcr|TRCPDCR_PU,TRCPDCR);}+/*+*ETEmandatesthattheTRCRSRiswrittentobefore+*enablingit.+*/+if(etm4x_is_ete(drvdata))+etm4x_relaxed_write32(csa,TRCRSR_TA,TRCRSR);+/* Enable the trace unit */etm4x_relaxed_write32(csa,1,TRCPRGCTLR);
+ major = ETM_ARCH_MAJOR_VERSION(drvdata->arch);
+ minor = ETM_ARCH_MINOR_VERSION(drvdata->arch);
And here too. Othersiwe it makes a big blob in the middle of the function.
+ if (etm4x_is_ete(drvdata)) {
+ type_name = "ete";
+ /* ETE v1 has major version == 5. Adjust this for logging.*/
+ major -= 4;
I don't have the documentation for the ETE but I would not adjust @major. I
would simply leave it to what the HW gives us since regardless of the name, the
major revision of the IP block is 5.
From: Suzuki K Poulose <suzuki.poulose@arm.com> Date: 2021-02-02 22:43:00
On 2/2/21 4:33 PM, Mike Leach wrote:
Hi,
On Tue, 2 Feb 2021 at 09:42, Suzuki K Poulose [off-list ref] wrote:
quoted
On 2/1/21 11:17 PM, Mathieu Poirier wrote:
quoted
Hi Anshuman,
I have started reviewing this set. As it is quite voluminous comments will
come over serveral days. I will let you know when I am done.
On Wed, Jan 27, 2021 at 02:25:25PM +0530, Anshuman Khandual wrote:
quoted
From: Suzuki K Poulose <suzuki.poulose@arm.com>
When there are multiple sinks on the system, in the absence
of a specified sink, it is quite possible that a default sink
for an ETM could be different from that of another ETM. However
we do not support having multiple sinks for an event yet. This
patch allows the event to use the default sinks on the ETMs
where they are scheduled as long as the sinks are of the same
type.
e.g, if we have 1x1 topology with per-CPU ETRs, the event can
use the per-CPU ETR for the session. However, if the sinks
are of different type, e.g TMC-ETR on one and a custom sink
on another, the event will only trace on the first detected
sink.
I found the above changelog very confusing - I read it several times and still
couldn't get all of it. In the end this patch prevents sinks of different types
from being used for session, and this is what the text should reflect.
Sorry about that. Your inference is correct, but it is only a side effect
of the primary motive. How about the following :
"When a sink is not specified by the user, the etm perf driver
finds a suitable sink automatically based on the first ETM, where
this event could be scheduled. Then we allocate the sink buffer based
on the selected sink. This is fine for a CPU bound event as the "sink"
is always guaranteed to be reachable from the ETM (as this is the only
ETM where the event is going to be scheduled). However, if we have a task
bound event, the event could be scheduled on any of the ETMs on the
system. In this case, currently we automatically select a sink and exclude
any ETMs that are not reachable from the selected sink. This is
problematic for 1x1 configurations as we end up in tracing the event
only on the "first" ETM, as the default sink is local to the first
ETM and unreachable from the rest.
However, we could allow the other ETMs to trace if they all have a
sink that is compatible with the "selected" sink and can use the
sink buffer. This can be easily done by verifying that they are
all driven by the same driver and matches the same subtype."
Not sure that the logic here makes total sense - I can't see _why_
multiple sinks need to be of the same type.
Because we have a single "sink_config" (read, single sink specific
buffer) for an event. i.e, we do the sink_ops->alloc_buffer() only once
and rightly so. This allocates any buffers that is used by a given sink.
e.g, for ETR it allocates an etr_perf_buffer. Now if we wanted the same
event to run on an ETM with TRBE, the TRBE doesn't have any buffer set up to
collect the trace and cant make any sense of etr_perf_buffer.
However, if there is another ETM with a different ETR, the second
ETR can make sense of the sink_config (etr_perf_buffer) and trace the event.
Please remember that this only applies to task bound events where
the event can be scheduled on different ETMs.
1) This patch is designed to allow multiple sinks to be used in a 1:1
topology system - but there is no specific restriction here - and N:M
should work on the same basis
Yes, this should work in any topology.
2) This implies that multiple sinks will work within the coresight
infrastucture.
I am afraid I don't understand the context here.
3) The sink interface -> struct coresight_ops_sink allows sinks to be
abstracted - therefore whichever sink is chosen the coresight
infrastructure calls the operations for the given sink.
Correct. The patch is trying to ensure that a private data
setup by one driver is not interpreted by another driver as
its own private data. (the private data being sink_config)
4) Each individual sink, will have its own hardware buffer - copied
into the perf buffers at some appropriate point.
Correct. Supporting multiple types of sinks for a single event
is complex and not worth the benefit of the extra complexity.
Moreover we don't expect sane systems to have such a
configuration.
Thus if the users specifies a selected sink - we need to eliminate any
source that cannot reach it.
Yes, we do that now.
If not we need to find the relevant default sink for the source, which
might be a shared ETR, or per CPU TRBE / ETR, and the abstraction
logic ought to handle getting the captured data to the correct place.
The abstraction logic works fine, but the per-event private data is
something that makes this complex.
If it doesn't then we are on shaky ground with any multiple sink
solution.
On the face of it - type is irrelevant. If I am missing something -
this patch needs a better explanation.
I hope the explanation above makes it clear. Please let me know
otherwise.
@@ -222,6 +230,7 @@ static void *etm_setup_aux(struct perf_event *event, void **pages, if (event->attr.config2) { id = (u32)event->attr.config2; sink = coresight_get_sink_by_id(id);
user_sink = coresight_get_sink_by_id(id);
quoted
+ sink_forced = true;
The comment for this block uses the term "selected sink", and the
functions use _default_sink . This may read better if we rename the
bool to "selected_sink" rather than "sink_forced"
I have removed the bool and replaced with the user_sink as Mathieu
suggested.
Thanks for the review
Suzuki
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: Suzuki K Poulose <suzuki.poulose@arm.com> Date: 2021-02-02 22:51:17
On 2/2/21 6:56 PM, Mathieu Poirier wrote:
On Wed, Jan 27, 2021 at 02:25:29PM +0530, Anshuman Khandual wrote:
quoted
From: Suzuki K Poulose <suzuki.poulose@arm.com>
Add ETE as one of the supported device types we support
with ETM4x driver. The devices are named following the
existing convention as ete<N>.
ETE mandates that the trace resource status register is programmed
before the tracing is turned on. For the moment simply write to
it indicating TraceActive.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
+ major = ETM_ARCH_MAJOR_VERSION(drvdata->arch);
+ minor = ETM_ARCH_MINOR_VERSION(drvdata->arch);
And here too. Othersiwe it makes a big blob in the middle of the function.
quoted
+ if (etm4x_is_ete(drvdata)) {
+ type_name = "ete";
+ /* ETE v1 has major version == 5. Adjust this for logging.*/
+ major -= 4;
I don't have the documentation for the ETE but I would not adjust @major. I
would simply leave it to what the HW gives us since regardless of the name, the
major revision of the IP block is 5.
At the moment only register definitions are public and can be found here :
https://developer.arm.com/docs/ddi0601/g/aarch64-system-registers/trcdevarch
The ETE is natural extension of the ETM architecture to support future
architecture changes and is designed in a way that the same software
can driver both ETM and ETE without much changes.
From: Suzuki K Poulose <suzuki.poulose@arm.com> Date: 2021-02-03 15:53:52
On 2/2/21 5:52 PM, Mathieu Poirier wrote:
On Wed, Jan 27, 2021 at 02:25:28PM +0530, Anshuman Khandual wrote:
quoted
From: Suzuki K Poulose <suzuki.poulose@arm.com>
Add support for handling the system registers for Embedded Trace
Extensions (ETE). ETE shares most of the registers with ETMv4 except
for some and also adds some new registers. Re-arrange the ETMv4x list
to share the common definitions and add the ETE sysreg support.
Cc: Mike Leach <redacted>
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
drivers/hwtracing/coresight/coresight-etm4x-core.c | 32 +++++++++++++
drivers/hwtracing/coresight/coresight-etm4x.h | 52 ++++++++++++++++++----
2 files changed, 75 insertions(+), 9 deletions(-)
+ }
+
+ if (!_relaxed)
+ __iormb(res); /* Imitate the !relaxed I/O helpers */
+
+ return res;
+}
+
+void ete_sysreg_write(u64 val, u32 offset, bool _relaxed, bool _64bit)
+{
+ if (!_relaxed)
+ __iowmb(); /* Imitate the !relaxed I/O helpers */
+ if (!_64bit)
+ val &= GENMASK(31, 0);
+
+ switch (offset) {
+ ETE_WRITE_CASES(val)
+ default :
+ WARN_ONCE(1, "ete: trying to write to unsupported register @%x\n",
+ offset);
Alignment
quoted
+ }
+}
The etm4x_sysreg_xyz() equivalent of these use a pr_warn_ratelimited() rather
than a WARN_ONE().
With that:
Reviewed-by: Mathieu Poirier <mathieu.poirier@linaro.org>
Converted to pr_warn_ratelimited() to both instances and fixed Alignment
Cheers
Suzuki
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
On Wed, Jan 27, 2021 at 02:25:31PM +0530, Anshuman Khandual wrote:
quoted hunk
From: Suzuki K Poulose <suzuki.poulose@arm.com>
The context associated with an ETM for a given perf event
includes :
- handle -> the perf output handle for the AUX buffer.
- the path for the trace components
- the buffer config for the sink.
The path and the buffer config are part of the "aux_priv" data
(etm_event_data) setup by the setup_aux() callback, and made available
via perf_get_aux(handle).
Now with a sink supporting IRQ, the sink could "end" an output
handle when the buffer reaches the programmed limit and would try
to restart a handle. This could fail if there is not enough
space left the AUX buffer (e.g, the userspace has not consumed
the data). This leaves the "handle" disconnected from the "event"
and also the "perf_get_aux()" cleared. This all happens within
the sink driver, without the etm_perf driver being aware.
Now when the event is actually stopped, etm_event_stop()
will need to access the "event_data". But since the handle
is not valid anymore, we loose the information to stop the
"trace" path. So, we need a reliable way to access the etm_event_data
even when the handle may not be active.
This patch replaces the per_cpu handle array with a per_cpu context
for the ETM, which tracks the "handle" as well as the "etm_event_data".
The context notes the etm_event_data at etm_event_start() and clears
it at etm_event_stop(). This makes sure that we don't access a
stale "etm_event_data" as we are guaranteed that it is not
freed by free_aux() as long as the event is active and tracing,
also provides us with access to the critical information
needed to wind up a session even in the absence of an active
output_handle.
This is not an issue for the legacy sinks as none of them supports
an IRQ and is centrally handled by the etm-perf.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Anshuman Khandual <redacted>
Cc: Leo Yan <redacted>
Cc: Mike Leach <redacted>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
drivers/hwtracing/coresight/coresight-etm-perf.c | 45 +++++++++++++++++++++---
1 file changed, 40 insertions(+), 5 deletions(-)
if (!csdev)
goto fail;
/*
* Something went wrong if an event data is already associated
* with a context.
*/
if (WARN_ONE(ctxt->event_data))
goto fail;
quoted hunk
@@ -374,6 +394,8 @@ static void etm_event_start(struct perf_event *event, int flags) if (source_ops(csdev)->enable(csdev, event, CS_MODE_PERF)) goto fail_disable_path;+ /* Save the event_data for this ETM */+ ctxt->event_data = event_data; out: return;
if (WARN_ON(event_data && event_data != ctxt->event_data))
return;
event_data = ctxt->event_data;
/* Clear the event_data as this ETM is stopping the trace. */
ctxt->event_data = NULL;
With the above:
Reviewed-by: Mathieu Poirier <mathieu.poirier@linaro.org>
quoted hunk
if (event->hw.state == PERF_HES_STOPPED)
return;
+ /* We must have a valid event_data for a running event */
+ if (WARN_ON(!event_data))
+ return;
+
if (!csdev)
return;
@@ -416,7 +445,13 @@ static void etm_event_stop(struct perf_event *event, int mode) /* tell the core */ event->hw.state = PERF_HES_STOPPED;- if (mode & PERF_EF_UPDATE) {+ /*+ * If the handle is not bound to an event anymore+ * (e.g, the sink driver was unable to restart the+ * handle due to lack of buffer space), we don't+ * have to do anything here.+ */+ if (handle->event && (mode & PERF_EF_UPDATE)) { if (WARN_ON_ONCE(handle->event != event)) return;
From: Suzuki K Poulose <suzuki.poulose@arm.com> Date: 2021-02-03 23:37:10
On 2/3/21 7:05 PM, Mathieu Poirier wrote:
On Wed, Jan 27, 2021 at 02:25:31PM +0530, Anshuman Khandual wrote:
quoted
From: Suzuki K Poulose <suzuki.poulose@arm.com>
The context associated with an ETM for a given perf event
includes :
- handle -> the perf output handle for the AUX buffer.
- the path for the trace components
- the buffer config for the sink.
The path and the buffer config are part of the "aux_priv" data
(etm_event_data) setup by the setup_aux() callback, and made available
via perf_get_aux(handle).
Now with a sink supporting IRQ, the sink could "end" an output
handle when the buffer reaches the programmed limit and would try
to restart a handle. This could fail if there is not enough
space left the AUX buffer (e.g, the userspace has not consumed
the data). This leaves the "handle" disconnected from the "event"
and also the "perf_get_aux()" cleared. This all happens within
the sink driver, without the etm_perf driver being aware.
Now when the event is actually stopped, etm_event_stop()
will need to access the "event_data". But since the handle
is not valid anymore, we loose the information to stop the
"trace" path. So, we need a reliable way to access the etm_event_data
even when the handle may not be active.
This patch replaces the per_cpu handle array with a per_cpu context
for the ETM, which tracks the "handle" as well as the "etm_event_data".
The context notes the etm_event_data at etm_event_start() and clears
it at etm_event_stop(). This makes sure that we don't access a
stale "etm_event_data" as we are guaranteed that it is not
freed by free_aux() as long as the event is active and tracing,
also provides us with access to the critical information
needed to wind up a session even in the absence of an active
output_handle.
This is not an issue for the legacy sinks as none of them supports
an IRQ and is centrally handled by the etm-perf.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Anshuman Khandual <redacted>
Cc: Leo Yan <redacted>
Cc: Mike Leach <redacted>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
drivers/hwtracing/coresight/coresight-etm-perf.c | 45 +++++++++++++++++++++---
1 file changed, 40 insertions(+), 5 deletions(-)
if (!csdev)
goto fail;
/*
* Something went wrong if an event data is already associated
* with a context.
*/
if (WARN_ONE(ctxt->event_data))
goto fail;
quoted
@@ -374,6 +394,8 @@ static void etm_event_start(struct perf_event *event, int flags) if (source_ops(csdev)->enable(csdev, event, CS_MODE_PERF)) goto fail_disable_path;+ /* Save the event_data for this ETM */+ ctxt->event_data = event_data; out: return;
if (WARN_ON(event_data && event_data != ctxt->event_data))
return;
event_data = ctxt->event_data;
/* Clear the event_data as this ETM is stopping the trace. */
ctxt->event_data = NULL;
With the above:
@@ -375,6 +375,10 @@ static void etm_event_start(struct perf_event *event, int flags)if(!csdev)gotofail;+/* Have we messed up our tracking ? */+if(WARN_ON(ctxt->event_data))+gotofail;+/**DealwiththeringbufferAPIandgetahandleonthe*session'sinformation.
@@ -432,11 +436,21 @@ static void etm_event_stop(struct perf_event *event, int mode)structcoresight_device*sink,*csdev=per_cpu(csdev_src,cpu);structetm_ctxt*ctxt=this_cpu_ptr(&etm_ctxt);structperf_output_handle*handle=&ctxt->handle;-structetm_event_data*event_data=ctxt->event_data;+structetm_event_data*event_data;structlist_head*path;+/*+*Ifwestillhaveaccesstotheevent_dataviahandle,+*confirmthatwehaven'tmessedupthetracking.+*/+if(handle->event&&+WARN_ON(perf_get_aux(handle)!=ctxt->event_data))+return;++event_data=ctxt->event_data;/* Clear the event_data as this ETM is stopping the trace. */ctxt->event_data=NULL;+if(event->hw.state==PERF_HES_STOPPED)return;
From: Mike Leach <hidden> Date: 2021-02-04 12:28:54
Hi Suzuki,.
On Tue, 2 Feb 2021 at 22:42, Suzuki K Poulose [off-list ref] wrote:
On 2/2/21 4:33 PM, Mike Leach wrote:
quoted
Hi,
On Tue, 2 Feb 2021 at 09:42, Suzuki K Poulose [off-list ref] wrote:
quoted
On 2/1/21 11:17 PM, Mathieu Poirier wrote:
quoted
Hi Anshuman,
I have started reviewing this set. As it is quite voluminous comments will
come over serveral days. I will let you know when I am done.
On Wed, Jan 27, 2021 at 02:25:25PM +0530, Anshuman Khandual wrote:
quoted
From: Suzuki K Poulose <suzuki.poulose@arm.com>
When there are multiple sinks on the system, in the absence
of a specified sink, it is quite possible that a default sink
for an ETM could be different from that of another ETM. However
we do not support having multiple sinks for an event yet. This
patch allows the event to use the default sinks on the ETMs
where they are scheduled as long as the sinks are of the same
type.
e.g, if we have 1x1 topology with per-CPU ETRs, the event can
use the per-CPU ETR for the session. However, if the sinks
are of different type, e.g TMC-ETR on one and a custom sink
on another, the event will only trace on the first detected
sink.
I found the above changelog very confusing - I read it several times and still
couldn't get all of it. In the end this patch prevents sinks of different types
from being used for session, and this is what the text should reflect.
Sorry about that. Your inference is correct, but it is only a side effect
of the primary motive. How about the following :
"When a sink is not specified by the user, the etm perf driver
finds a suitable sink automatically based on the first ETM, where
this event could be scheduled. Then we allocate the sink buffer based
on the selected sink. This is fine for a CPU bound event as the "sink"
is always guaranteed to be reachable from the ETM (as this is the only
ETM where the event is going to be scheduled). However, if we have a task
bound event, the event could be scheduled on any of the ETMs on the
system. In this case, currently we automatically select a sink and exclude
any ETMs that are not reachable from the selected sink. This is
problematic for 1x1 configurations as we end up in tracing the event
only on the "first" ETM, as the default sink is local to the first
ETM and unreachable from the rest.
However, we could allow the other ETMs to trace if they all have a
sink that is compatible with the "selected" sink and can use the
sink buffer. This can be easily done by verifying that they are
all driven by the same driver and matches the same subtype."
Not sure that the logic here makes total sense - I can't see _why_
multiple sinks need to be of the same type.
Because we have a single "sink_config" (read, single sink specific
buffer) for an event. i.e, we do the sink_ops->alloc_buffer() only once
and rightly so. This allocates any buffers that is used by a given sink.
e.g, for ETR it allocates an etr_perf_buffer. Now if we wanted the same
event to run on an ETM with TRBE, the TRBE doesn't have any buffer set up to
collect the trace and cant make any sense of etr_perf_buffer.
However, if there is another ETM with a different ETR, the second
ETR can make sense of the sink_config (etr_perf_buffer) and trace the event.
Please remember that this only applies to task bound events where
the event can be scheduled on different ETMs.
I have had a look around the ETR driver and can see the issue -
something very sink specific is being bound to the more generic event,
which does undermine the abstraction a little bit!
The ETR drivers refer to per-thread and cpu-wide - not cpu-bound /
task-bound - could do to get consistent terminology here. The cpu-wide
version is designed to be called multiple times - but obviously only
allocates a single buffer.
This implies that for the cpu-wide case, we could tolerate sinks of
differing types, as setup_aux will be called once per CPU. Whether we
want to is another question.
Given we don't yet have any systems that sport differing sink types,
its probably not worth allowing.
A short comment explaining the type matching is needed due to
underlying private data structure incompatibility would be useful
though.
Regards
Mike
quoted
1) This patch is designed to allow multiple sinks to be used in a 1:1
topology system - but there is no specific restriction here - and N:M
should work on the same basis
Yes, this should work in any topology.
quoted
2) This implies that multiple sinks will work within the coresight
infrastucture.
I am afraid I don't understand the context here.
quoted
3) The sink interface -> struct coresight_ops_sink allows sinks to be
abstracted - therefore whichever sink is chosen the coresight
infrastructure calls the operations for the given sink.
Correct. The patch is trying to ensure that a private data
setup by one driver is not interpreted by another driver as
its own private data. (the private data being sink_config)
quoted
4) Each individual sink, will have its own hardware buffer - copied
into the perf buffers at some appropriate point.
Correct. Supporting multiple types of sinks for a single event
is complex and not worth the benefit of the extra complexity.
Moreover we don't expect sane systems to have such a
configuration.
quoted
Thus if the users specifies a selected sink - we need to eliminate any
source that cannot reach it.
Yes, we do that now.
quoted
If not we need to find the relevant default sink for the source, which
might be a shared ETR, or per CPU TRBE / ETR, and the abstraction
logic ought to handle getting the captured data to the correct place.
The abstraction logic works fine, but the per-event private data is
something that makes this complex.
quoted
If it doesn't then we are on shaky ground with any multiple sink
solution.
On the face of it - type is irrelevant. If I am missing something -
this patch needs a better explanation.
I hope the explanation above makes it clear. Please let me know
otherwise.
@@ -222,6 +230,7 @@ static void *etm_setup_aux(struct perf_event *event, void **pages, if (event->attr.config2) { id = (u32)event->attr.config2; sink = coresight_get_sink_by_id(id);
user_sink = coresight_get_sink_by_id(id);
quoted
+ sink_forced = true;
The comment for this block uses the term "selected sink", and the
functions use _default_sink . This may read better if we rename the
bool to "selected_sink" rather than "sink_forced"
I have removed the bool and replaced with the user_sink as Mathieu
suggested.
Thanks for the review
Suzuki
--
Mike Leach
Principal Engineer, ARM Ltd.
Manchester Design Centre. UK
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
On Thu, Jan 28, 2021 at 09:16:34AM +0000, Suzuki K Poulose wrote:
On 1/27/21 8:55 AM, Anshuman Khandual wrote:
quoted
Add support for dedicated sinks that are bound to individual CPUs. (e.g,
TRBE). To allow quicker access to the sink for a given CPU bound source,
keep a percpu array of the sink devices. Also, add support for building
a path to the CPU local sink from the ETM.
This adds a new percpu sink type CORESIGHT_DEV_SUBTYPE_SINK_PERCPU_SYSMEM.
This new sink type is exclusively available and can only work with percpu
source type device CORESIGHT_DEV_SUBTYPE_SOURCE_PERCPU_PROC.
This defines a percpu structure that accommodates a single coresight_device
which can be used to store an initialized instance from a sink driver. As
these sinks are exclusively linked and dependent on corresponding percpu
sources devices, they should also be the default sink device during a perf
session.
Outwards device connections are scanned while establishing paths between a
source and a sink device. But such connections are not present for certain
percpu source and sink devices which are exclusively linked and dependent.
Build the path directly and skip connection scanning for such devices.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
Changes in V3:
- Updated coresight_find_default_sink()
drivers/hwtracing/coresight/coresight-core.c | 16 ++++++++++++++--
include/linux/coresight.h | 12 ++++++++++++
2 files changed, 26 insertions(+), 2 deletions(-)
@@ -784,6 +785,13 @@ static int _coresight_build_path(struct coresight_device *csdev,if(csdev==sink)gotoout;+if(coresight_is_percpu_source(csdev)&&coresight_is_percpu_sink(sink)&&+sink==per_cpu(csdev_sink,source_ops(csdev)->cpu_id(csdev))){+_coresight_build_path(sink,sink,path);
The return value for _coresight_build_path() needs to be checked. Otherwise a
failure to allocate a node for the sink will go unoticed and make for a very
hard problem to debug.
quoted
+ found = true;
+ goto out;
+ }
+
/* Not a sink - recursively explore each port found on this element */
for (i = 0; i < csdev->pdata->nr_outport; i++) {
struct coresight_device *child_dev;
@@ -999,8 +1007,12 @@ coresight_find_default_sink(struct coresight_device *csdev) int depth = 0; /* look for a default sink if we have not found for this device */- if (!csdev->def_sink)- csdev->def_sink = coresight_find_sink(csdev, &depth);+ if (!csdev->def_sink) {+ if (coresight_is_percpu_source(csdev))+ csdev->def_sink = per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev));+ if (!csdev->def_sink)+ csdev->def_sink = coresight_find_sink(csdev, &depth);+ } return csdev->def_sink; }
Do we absolutely need to add a new sink type? It is only used in
_coresight_build_path() and that code could be:
if (coresight_is_percpu_source(csdev)) {
sink == per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev));
if (sink && sink == csdev) {
_coresight_build_path(sink, sink, path);
On Wed, Jan 27, 2021 at 02:25:35PM +0530, Anshuman Khandual wrote:
Trace Buffer Extension (TRBE) implements a trace buffer per CPU which is
accessible via the system registers. The TRBE supports different addressing
modes including CPU virtual address and buffer modes including the circular
buffer mode. The TRBE buffer is addressed by a base pointer (TRBBASER_EL1),
an write pointer (TRBPTR_EL1) and a limit pointer (TRBLIMITR_EL1). But the
access to the trace buffer could be prohibited by a higher exception level
(EL3 or EL2), indicated by TRBIDR_EL1.P. The TRBE can also generate a CPU
private interrupt (PPI) on address translation errors and when the buffer
is full. Overall implementation here is inspired from the Arm SPE driver.
I got this message when applying the patch:
Applying: coresight: sink: Add TRBE driver
.git/rebase-apply/patch:76: new blank line at EOF.
+
warning: 1 line adds whitespace errors.
quoted hunk
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
Changes in V3:
- Added new DT bindings document TRBE.yaml
- Changed TRBLIMITR_TRIG_MODE_SHIFT from 2 to 3
- Dropped isb() from trbe_reset_local()
- Dropped gap between (void *) and buf->trbe_base
- Changed 'int' to 'unsigned int' in is_trbe_available()
- Dropped unused function set_trbe_running(), set_trbe_virtual_mode(),
set_trbe_enabled() and set_trbe_limit_pointer()
- Changed get_trbe_flag_update(), is_trbe_programmable() and
get_trbe_address_align() to accept TRBIDR value
- Changed is_trbe_running(), is_trbe_abort(), is_trbe_wrap(), is_trbe_trg(),
is_trbe_irq(), get_trbe_bsc() and get_trbe_ec() to accept TRBSR value
- Dropped snapshot mode condition in arm_trbe_alloc_buffer()
- Exit arm_trbe_init() when arm64_kernel_unmapped_at_el0() is enabled
- Compute trbe_limit before trbe_write to get the updated handle
- Added trbe_stop_and_truncate_event()
- Dropped trbe_handle_fatal()
Documentation/trace/coresight/coresight-trbe.rst | 39 +
arch/arm64/include/asm/sysreg.h | 1 +
drivers/hwtracing/coresight/Kconfig | 11 +
drivers/hwtracing/coresight/Makefile | 1 +
drivers/hwtracing/coresight/coresight-trbe.c | 1023 ++++++++++++++++++++++
drivers/hwtracing/coresight/coresight-trbe.h | 160 ++++
6 files changed, 1235 insertions(+)
create mode 100644 Documentation/trace/coresight/coresight-trbe.rst
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.c
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.h
@@ -0,0 +1,39 @@+.. SPDX-License-Identifier: GPL-2.0++==============================+Trace Buffer Extension (TRBE).+==============================++:Author: Anshuman Khandual <anshuman.khandual@arm.com>+:Date: November 2020++Hardware Description+--------------------++Trace Buffer Extension (TRBE) is a percpu hardware which captures in system+memory, CPU traces generated from a corresponding percpu tracing unit. This+gets plugged in as a coresight sink device because the corresponding trace+genarators (ETE), are plugged in as source device.++The TRBE is not compliant to CoreSight architecture specifications, but is+driven via the CoreSight driver framework to support the ETE (which is+CoreSight compliant) integration.++Sysfs files and directories+---------------------------++The TRBE devices appear on the existing coresight bus alongside the other+coresight devices::++ >$ ls /sys/bus/coresight/devices+ trbe0 trbe1 trbe2 trbe3++The ``trbe<N>`` named TRBEs are associated with a CPU.::++ >$ ls /sys/bus/coresight/devices/trbe0/+ align dbm++*Key file items are:-*+*``align``: TRBE write pointer alignment+*``dbm``: TRBE updates memory with access and dirty flags+
I would make it dependent on ETMv4 as well since it can only be used by that
component.
+ help
+ This driver provides support for percpu Trace Buffer Extension (TRBE).
+ TRBE always needs to be used along with it's corresponding percpu ETE
+ component. ETE generates trace data which is then captured with TRBE.
+ Unlike traditional sink devices, TRBE is a CPU feature accessible via
+ system registers. But it's explicit dependency with trace unit (ETE)
+ requires it to be plugged in as a coresight sink device.
Please add:
"To compile this driver as a module, choose M here: the
module will be called coresight-trbe."
I'm out of time for today, I will continue on Monday.
Mathieu
@@ -0,0 +1,1023 @@+// SPDX-License-Identifier: GPL-2.0+/*+*ThisdriverenablesTraceBufferExtension(TRBE)asaper-cpucoresight+*sinkdevicecouldthenpairwithanappropriateper-cpucoresightsource+*device(ETE)thusgeneratingrequiredtracedata.Tracecanbeenabled+*viatheperfframework.+*+*Copyright(C)2020ARMLtd.+*+*Author:AnshumanKhandual<anshuman.khandual@arm.com>+*/+#define DRVNAME "arm_trbe"++#define pr_fmt(fmt) DRVNAME ": " fmt++#include"coresight-trbe.h"++#define PERF_IDX2OFF(idx, buf) ((idx) % ((buf)->nr_pages << PAGE_SHIFT))++/*+*Apaddingpacketthatwillhelptheuserspacetools+*inskippingrelevantsectionsinthecapturedtrace+*datawhichcouldnotbedecoded.TRBEdoesn'tsupport+*formattingthetracedata,unlikethelegacyCoreSight+*sinksandthusweuseETEtracepacketstopadthe+*sectionsofthebuffer.+*/+#define ETE_IGNORE_PACKET 0x70++/*+*Minimumamountofmeaningfultracewillcontain:+*A-Sync,TraceInfo,TraceOn,Address,Atom.+*Thisisabout44bytesofETEtrace.Tobeon+*thesaferside,weassume64bytesistheminimum+*spacerequiredforameaningfulsession,before+*wehita"WRAP"event.+*/+#define TRBE_TRACE_MIN_BUF_SIZE 64++enumtrbe_fault_action{+TRBE_FAULT_ACT_WRAP,+TRBE_FAULT_ACT_SPURIOUS,+TRBE_FAULT_ACT_FATAL,+};++structtrbe_buf{+unsignedlongtrbe_base;+unsignedlongtrbe_limit;+unsignedlongtrbe_write;+intnr_pages;+void**pages;+boolsnapshot;+structtrbe_cpudata*cpudata;+};++structtrbe_cpudata{+booltrbe_dbm;+u64trbe_align;+intcpu;+enumcs_modemode;+structtrbe_buf*buf;+structtrbe_drvdata*drvdata;+};++structtrbe_drvdata{+structtrbe_cpudata__percpu*cpudata;+structperf_output_handle__percpu**handle;+structhlist_nodehotplug_node;+intirq;+cpumask_tsupported_cpus;+enumcpuhp_statetrbe_online;+structplatform_device*pdev;+};++staticinttrbe_alloc_node(structperf_event*event)+{+if(event->cpu==-1)+returnNUMA_NO_NODE;+returncpu_to_node(event->cpu);+}++staticvoidtrbe_drain_buffer(void)+{+asm(TSB_CSYNC);+dsb(nsh);+}++staticvoidtrbe_drain_and_disable_local(void)+{+trbe_drain_buffer();+write_sysreg_s(0,SYS_TRBLIMITR_EL1);+isb();+}++staticvoidtrbe_reset_local(void)+{+trbe_drain_and_disable_local();+write_sysreg_s(0,SYS_TRBPTR_EL1);+write_sysreg_s(0,SYS_TRBBASER_EL1);+write_sysreg_s(0,SYS_TRBSR_EL1);+}++staticvoidtrbe_stop_and_truncate_event(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);++/*+*Wecannotproceedwiththebuffercollectionandwe+*donothaveanydataforthecurrentsession.The+*etm_perfdriverexpectstocloseouttheaux_buffer+*atevent_stop().SodisabletheTRBEhereandleave+*theupdate_buffer()toreturna0size.+*/+trbe_drain_and_disable_local();+perf_aux_output_flag(handle,PERF_AUX_FLAG_TRUNCATED);+*this_cpu_ptr(buf->cpudata->drvdata->handle)=NULL;+}++/*+*TRBEBufferManagement+*+*TheTRBEbufferspansfromthebasepointertillthelimitpointer.Whenenabled,+*itstartswritingtracedatafromthewritepointeronwardtillthelimitpointer.+*Whenthewritepointerreachestheaddressjustbeforethelimitpointer,itgets+*wrappedaroundagaintothebasepointer.ThisiscalledaTRBEwrapevent,which+*generatesamaintenanceinterruptwhenoperatedinWRAPorFILLmode.Thisdriver+*usesFILLmode,wheretheTRBEstopsthetracecollectionatwrapevent.TheIRQ+*handlerupdatestheAUXbufferandre-enablestheTRBEwithupdatedWRITEand+*LIMITpointers.+*+*WraparoundwithanIRQ+*------<------<-------<-----<-----+*||+*------>------>------->----->-----+*+*+---------------+-----------------------++*|||+*+---------------+-----------------------++*BasePointerWritePointerLimitPointer+*+*ThebaseandlimitpointersalwaysneedstobePAGE_SIZEaligned.Butthewrite+*pointercanbealignedtotheimplementationdefinedTRBEtracebufferalignment+*ascapturedintrbe_cpudata->trbe_align.+*+*+*headtailwakeup+*+---------------------------------------+-----~~------+*|$$$$$$$|################|$$$$$$$$$$$$$$||+*+---------------------------------------+-----~~------+*BasePointerWritePointerLimitPointer+*+*Theperf_output_handleindices(head,tail,wakeup)aremonotonicallyincreasing+*valueswhichtracksallthedriverwritesanduserreadsfromtheperfauxiliary+*buffer.Generally[head..tail]istheareawherethedrivercanwriteintounless+*thewakeupisbehindthetail.EnabledTRBEbufferspanneedstobeadjustedand+*configureddependingontheperf_output_handleindices,sothatthedriverdoes+*notoverrideintoareasintheperfauxiliarybufferwhichisbeingoryettobe+*consumedfromtheuserspace.TheenabledTRBEbufferareaisamovingsubsetof+*theallocatedperfauxiliarybuffer.+*/+staticvoidtrbe_pad_buf(structperf_output_handle*handle,intlen)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);+u64head=PERF_IDX2OFF(handle->head,buf);++memset((void*)buf->trbe_base+head,ETE_IGNORE_PACKET,len);+if(!buf->snapshot)+perf_aux_output_skip(handle,len);+}++staticunsignedlongtrbe_snapshot_offset(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);++/*+*TheETEtracehasalignmentsynchronizationpacketsallowing+*thedecodertoresetincaseofanoverfloworcorruption.+*Sowecanusetheentirebufferforthesnapshotmode.+*/+returnbuf->nr_pages*PAGE_SIZE;+}++/*+*TRBELimitCalculation+*+*ThefollowingmarkersareusedtoillustratevariousTRBEbuffersituations.+*+*$$$$-Dataarea,unconsumedcapturedtracedata,nottobeoverridden+*####-Freearea,enabled,tracewillbewritten+*%%%%-Freearea,disabled,tracewillnotbewritten+*====-Freearea,paddedwithETE_IGNORE_PACKET,tracewillbeskipped+*/+staticunsignedlong__trbe_normal_offset(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);+structtrbe_cpudata*cpudata=buf->cpudata;+constu64bufsize=buf->nr_pages*PAGE_SIZE;+u64limit=bufsize;+u64head,tail,wakeup;++head=PERF_IDX2OFF(handle->head,buf);++/*+*head+*------->|+*|+*headTRBEaligntail+*+----|-------|---------------|-------++*|$$$$|=======|###############|$$$$$$$|+*+----|-------|---------------|-------++*trbe_basetrbe_base+nr_pages+*+*Perfauxbufferoutputheadpositioncanbemisaligneddependingon+*variousfactorsincludinguserspacereads.Incasemisaligned,head+*needstobealignedbeforeTRBEcanbeconfigured.Padthealignment+*gapwithETE_IGNORE_PACKETbytesthatwillbeignoredbyusertools+*andskipthissectionthusadvancingthehead.+*/+if(!IS_ALIGNED(head,cpudata->trbe_align)){+unsignedlongdelta=roundup(head,cpudata->trbe_align)-head;++delta=min(delta,handle->size);+trbe_pad_buf(handle,delta);+head=PERF_IDX2OFF(handle->head,buf);+}++/*+*head=tail(size=0)+*+----|-------------------------------++*|$$$$|$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$|+*+----|-------------------------------++*trbe_basetrbe_base+nr_pages+*+*Perfauxbufferdoesnothaveanyspaceforthedrivertowriteinto.+*Justcommunicatetracetruncationeventtotheuserspacebymarking+*itwithPERF_AUX_FLAG_TRUNCATED.+*/+if(!handle->size){+perf_aux_output_flag(handle,PERF_AUX_FLAG_TRUNCATED);+return0;+}++/* Compute the tail and wakeup indices now that we've aligned head */+tail=PERF_IDX2OFF(handle->head+handle->size,buf);+wakeup=PERF_IDX2OFF(handle->wakeup,buf);++/*+*LetscalculatethebufferareawhichTRBEcouldwriteinto.There+*arethreepossiblescenarioshere.Limitneedstobealignedwith+*PAGE_SIZEpertheTRBErequirement.Alwaysavoidclobberingthe+*unconsumeddata.+*+*1)head<tail+*+*headtail+*+----|-----------------------|-------++*|$$$$|#######################|$$$$$$$|+*+----|-----------------------|-------++*trbe_baselimittrbe_base+nr_pages+*+*TRBEcouldwriteinto[head..tail]area.Unlessthetailisrightat+*theendofthebuffer,neitheranwraparoundnoranIRQisexpected+*whilebeingenabled.+*+*2)head==tail+*+*head=tail(size>0)+*+----|-------------------------------++*|%%%%|###############################|+*+----|-------------------------------++*trbe_baselimit=trbe_base+nr_pages+*+*TRBEshouldjustwriteinto[head..base+nr_pages]areaeventhough+*theentirebufferisempty.Reasonbeing,whenthetracereachesthe+*endofthebuffer,itwilljustwraparoundwithanIRQgivingan+*opportunitytoreconfigurethebuffer.+*+*3)tail<head+*+*tailhead+*+----|-----------------------|-------++*|%%%%|$$$$$$$$$$$$$$$$$$$$$$$|#######|+*+----|-----------------------|-------++*trbe_baselimit=trbe_base+nr_pages+*+*TRBEshouldjustwriteinto[head..base+nr_pages]areaeventhough+*the[trbe_base..tail]isalsoempty.Reasonbeing,whenthetrace+*reachestheendofthebuffer,itwilljustwraparoundwithanIRQ+*givinganopportunitytoreconfigurethebuffer.+*/+if(head<tail)+limit=round_down(tail,PAGE_SIZE);++/*+*Wakeupmaybearbitrarilyfarintothefuture.Ifit'snotinthe+*currentgeneration,eitherwe'llwrapbeforehittingit,orit's+*inthepastandhasbeenhandledalready.+*+*Ifthere'sawakeupbeforewewrap,arrangetobewokenupbythe+*pageboundaryfollowingit.Keepthetailboundaryifthat'slower.+*+*headwakeuptail+*+----|---------------|-------|-------++*|$$$$|###############|%%%%%%%|$$$$$$$|+*+----|---------------|-------|-------++*trbe_baselimittrbe_base+nr_pages+*/+if(handle->wakeup<(handle->head+handle->size)&&head<=wakeup)+limit=min(limit,round_up(wakeup,PAGE_SIZE));++/*+*Therearetwosituationwhenthiscanhappeni.elimitisbefore+*theheadandhenceTRBEcannotbeconfigured.+*+*1)head<tail(aligneddownwithPAGE_SIZE)andalsotheyareboth+*withinthesamePAGEsizerange.+*+*PAGE_SIZE+*|----------------------|+*+*limitheadtail+*+------------|------|--------|-------++*|$$$$$$$$$$$$$$$$$$$|========|$$$$$$$|+*+------------|------|--------|-------++*trbe_basetrbe_base+nr_pages+*+*2)head<wakeup(alignedupwithPAGE_SIZE)<tailandalsoboth+*headandwakeuparewithinsamePAGEsizerange.+*+*PAGE_SIZE+*|----------------------|+*+*limitheadwakeuptail+*+----|------|-------|--------|-------++*|$$$$$$$$$$$|=======|========|$$$$$$$|+*+----|------|-------|--------|-------++*trbe_basetrbe_base+nr_pages+*/+if(limit>head)+returnlimit;++trbe_pad_buf(handle,handle->size);+perf_aux_output_flag(handle,PERF_AUX_FLAG_TRUNCATED);+return0;+}++staticunsignedlongtrbe_normal_offset(structperf_output_handle*handle)+{+structtrbe_buf*buf=perf_get_aux(handle);+u64limit=__trbe_normal_offset(handle);+u64head=PERF_IDX2OFF(handle->head,buf);++/*+*Iftheheadistooclosetothelimitandwedon't+*havespaceforameaningfulrun,weratherpadit+*andstartfresh.+*/+if(limit&&(limit-head<TRBE_TRACE_MIN_BUF_SIZE)){+trbe_pad_buf(handle,limit-head);+limit=__trbe_normal_offset(handle);+}+returnlimit;+}++staticunsignedlongcompute_trbe_buffer_limit(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);+unsignedlongoffset;++if(buf->snapshot)+offset=trbe_snapshot_offset(handle);+else+offset=trbe_normal_offset(handle);+returnbuf->trbe_base+offset;+}++staticvoidclr_trbe_status(void)+{+u64trbsr=read_sysreg_s(SYS_TRBSR_EL1);++WARN_ON(is_trbe_enabled());+trbsr&=~TRBSR_IRQ;+trbsr&=~TRBSR_TRG;+trbsr&=~TRBSR_WRAP;+trbsr&=~(TRBSR_EC_MASK<<TRBSR_EC_SHIFT);+trbsr&=~(TRBSR_BSC_MASK<<TRBSR_BSC_SHIFT);+trbsr&=~TRBSR_STOP;+write_sysreg_s(trbsr,SYS_TRBSR_EL1);+}++staticvoidset_trbe_limit_pointer_enabled(unsignedlongaddr)+{+u64trblimitr=read_sysreg_s(SYS_TRBLIMITR_EL1);++WARN_ON(!IS_ALIGNED(addr,(1UL<<TRBLIMITR_LIMIT_SHIFT)));+WARN_ON(!IS_ALIGNED(addr,PAGE_SIZE));++trblimitr&=~TRBLIMITR_NVM;+trblimitr&=~(TRBLIMITR_FILL_MODE_MASK<<TRBLIMITR_FILL_MODE_SHIFT);+trblimitr&=~(TRBLIMITR_TRIG_MODE_MASK<<TRBLIMITR_TRIG_MODE_SHIFT);+trblimitr&=~(TRBLIMITR_LIMIT_MASK<<TRBLIMITR_LIMIT_SHIFT);++/*+*Filltracebuffermodeisusedherewhileconfiguringthe+*TRBEfortracecapture.Inthisparticularmode,thetrace+*collectionisstoppedandamaintenanceinterruptisraised+*whenthecurrentwritepointerwraps.Thispauseintrace+*collectiongivesthesoftwareanopportunitytocapturethe+*tracedataintheinterrupthandler,beforereconfiguring+*theTRBE.+*/+trblimitr|=(TRBE_FILL_MODE_FILL&TRBLIMITR_FILL_MODE_MASK)<<TRBLIMITR_FILL_MODE_SHIFT;++/*+*TriggermodeisnotusedherewhileconfiguringtheTRBEfor+*thetracecapture.Hencejustkeepthisintheignoremode.+*/+trblimitr|=(TRBE_TRIG_MODE_IGNORE&TRBLIMITR_TRIG_MODE_MASK)<<TRBLIMITR_TRIG_MODE_SHIFT;+trblimitr|=(addr&PAGE_MASK);++trblimitr|=TRBLIMITR_ENABLE;+write_sysreg_s(trblimitr,SYS_TRBLIMITR_EL1);+}++staticvoidtrbe_enable_hw(structtrbe_buf*buf)+{+WARN_ON(buf->trbe_write<buf->trbe_base);+WARN_ON(buf->trbe_write>=buf->trbe_limit);+set_trbe_disabled();+isb();+clr_trbe_status();+set_trbe_base_pointer(buf->trbe_base);+set_trbe_write_pointer(buf->trbe_write);++/*+*Synchronizealltheregisterupdates+*tillnowbeforeenablingtheTRBE.+*/+isb();+set_trbe_limit_pointer_enabled(buf->trbe_limit);++/* Synchronize the TRBE enable event */+isb();+}++staticvoid*arm_trbe_alloc_buffer(structcoresight_device*csdev,+structperf_event*event,void**pages,+intnr_pages,boolsnapshot)+{+structtrbe_buf*buf;+structpage**pglist;+inti;++/*+*TRBELIMITandTRBEWRITEpointersmustbepagealigned.Butwith+*justasinglepage,thereisnotmuchroomleftwhilewritinginto+*apartiallyfilledTRBEbuffer.Hencerestricttheminimumbuffer+*sizeastwopages.+*/+if(nr_pages<2)+returnNULL;++buf=kzalloc_node(sizeof(*buf),GFP_KERNEL,trbe_alloc_node(event));+if(IS_ERR(buf))+returnERR_PTR(-ENOMEM);++pglist=kcalloc(nr_pages,sizeof(*pglist),GFP_KERNEL);+if(IS_ERR(pglist)){+kfree(buf);+returnERR_PTR(-ENOMEM);+}++for(i=0;i<nr_pages;i++)+pglist[i]=virt_to_page(pages[i]);++buf->trbe_base=(unsignedlong)vmap(pglist,nr_pages,VM_MAP,PAGE_KERNEL);+if(IS_ERR((void*)buf->trbe_base)){+kfree(pglist);+kfree(buf);+returnERR_PTR(buf->trbe_base);+}+buf->trbe_limit=buf->trbe_base+nr_pages*PAGE_SIZE;+buf->trbe_write=buf->trbe_base;+buf->snapshot=snapshot;+buf->nr_pages=nr_pages;+buf->pages=pages;+kfree(pglist);+returnbuf;+}++voidarm_trbe_free_buffer(void*config)+{+structtrbe_buf*buf=config;++vunmap((void*)buf->trbe_base);+kfree(buf);+}++staticunsignedlongarm_trbe_update_buffer(structcoresight_device*csdev,+structperf_output_handle*handle,+void*config)+{+structtrbe_drvdata*drvdata=dev_get_drvdata(csdev->dev.parent);+structtrbe_cpudata*cpudata=dev_get_drvdata(&csdev->dev);+structtrbe_buf*buf=config;+unsignedlongsize,offset;++WARN_ON(buf->cpudata!=cpudata);+WARN_ON(cpudata->cpu!=smp_processor_id());+WARN_ON(cpudata->drvdata!=drvdata);+if(cpudata->mode!=CS_MODE_PERF)+return-EINVAL;++/*+*IftheTRBEwasdisabledduetolackofspaceintheAUXbufferora+*spuriousfault,thedriverleavesitdisabled,truncatingthebuffer.+*Sincetheetm_perfdriverexpectstocloseouttheAUXbuffer,the+*driverskipsit.Thus,justpassin0sizeheretoindicatethatthe+*bufferwastruncated.+*/+if(!is_trbe_enabled())+return0;+/*+*perfhandlestructureneedstobesharedwiththeTRBEIRQhandlerfor+*capturingtracedataandrestartingthehandle.Thereisaprobability+*ofanundefinedreferencebasedcrashwhenetmeventisbeingstopped+*whileaTRBEIRQalsogettingprocessed.Thishappensduetherelease+*ofperfhandleviaperf_aux_output_end()inetm_event_stop().Stopping+*theTRBEherewillensurethatnoIRQcouldbegeneratedwhentheperf+*handlegetsfreedinetm_event_stop().+*/+trbe_drain_and_disable_local();+offset=get_trbe_write_pointer()-get_trbe_base_pointer();+size=offset-PERF_IDX2OFF(handle->head,buf);+if(buf->snapshot)+handle->head+=size;+returnsize;+}++staticintarm_trbe_enable(structcoresight_device*csdev,u32mode,void*data)+{+structtrbe_drvdata*drvdata=dev_get_drvdata(csdev->dev.parent);+structtrbe_cpudata*cpudata=dev_get_drvdata(&csdev->dev);+structperf_output_handle*handle=data;+structtrbe_buf*buf=etm_perf_sink_config(handle);++WARN_ON(cpudata->cpu!=smp_processor_id());+WARN_ON(cpudata->drvdata!=drvdata);+if(mode!=CS_MODE_PERF)+return-EINVAL;++*this_cpu_ptr(drvdata->handle)=handle;+cpudata->buf=buf;+cpudata->mode=mode;+buf->cpudata=cpudata;+buf->trbe_limit=compute_trbe_buffer_limit(handle);+buf->trbe_write=buf->trbe_base+PERF_IDX2OFF(handle->head,buf);+if(buf->trbe_limit==buf->trbe_base){+trbe_stop_and_truncate_event(handle);+return0;+}+trbe_enable_hw(buf);+return0;+}++staticintarm_trbe_disable(structcoresight_device*csdev)+{+structtrbe_drvdata*drvdata=dev_get_drvdata(csdev->dev.parent);+structtrbe_cpudata*cpudata=dev_get_drvdata(&csdev->dev);+structtrbe_buf*buf=cpudata->buf;++WARN_ON(buf->cpudata!=cpudata);+WARN_ON(cpudata->cpu!=smp_processor_id());+WARN_ON(cpudata->drvdata!=drvdata);+if(cpudata->mode!=CS_MODE_PERF)+return-EINVAL;++trbe_drain_and_disable_local();+buf->cpudata=NULL;+cpudata->buf=NULL;+cpudata->mode=CS_MODE_DISABLED;+return0;+}++staticvoidtrbe_handle_spurious(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);++buf->trbe_limit=compute_trbe_buffer_limit(handle);+buf->trbe_write=buf->trbe_base+PERF_IDX2OFF(handle->head,buf);+if(buf->trbe_limit==buf->trbe_base){+trbe_drain_and_disable_local();+return;+}+trbe_enable_hw(buf);+}++staticvoidtrbe_handle_overflow(structperf_output_handle*handle)+{+structperf_event*event=handle->event;+structtrbe_buf*buf=etm_perf_sink_config(handle);+unsignedlongoffset,size;+structetm_event_data*event_data;++offset=get_trbe_limit_pointer()-get_trbe_base_pointer();+size=offset-PERF_IDX2OFF(handle->head,buf);+if(buf->snapshot)+handle->head=offset;+perf_aux_output_end(handle,size);++event_data=perf_aux_output_begin(handle,event);+if(!event_data){+trbe_drain_and_disable_local();+*this_cpu_ptr(buf->cpudata->drvdata->handle)=NULL;+return;+}+buf->trbe_limit=compute_trbe_buffer_limit(handle);+buf->trbe_write=buf->trbe_base+PERF_IDX2OFF(handle->head,buf);+if(buf->trbe_limit==buf->trbe_base){+trbe_stop_and_truncate_event(handle);+return;+}+*this_cpu_ptr(buf->cpudata->drvdata->handle)=handle;+trbe_enable_hw(buf);+}++staticboolis_perf_trbe(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);+structtrbe_cpudata*cpudata=buf->cpudata;+structtrbe_drvdata*drvdata=cpudata->drvdata;+intcpu=smp_processor_id();++WARN_ON(buf->trbe_base!=get_trbe_base_pointer());+WARN_ON(buf->trbe_limit!=get_trbe_limit_pointer());++if(cpudata->mode!=CS_MODE_PERF)+returnfalse;++if(cpudata->cpu!=cpu)+returnfalse;++if(!cpumask_test_cpu(cpu,&drvdata->supported_cpus))+returnfalse;++returntrue;+}++staticenumtrbe_fault_actiontrbe_get_fault_act(structperf_output_handle*handle)+{+u64trbsr=read_sysreg_s(SYS_TRBSR_EL1);+intec=get_trbe_ec(trbsr);+intbsc=get_trbe_bsc(trbsr);++WARN_ON(is_trbe_running(trbsr));+if(is_trbe_trg(trbsr)||is_trbe_abort(trbsr))+returnTRBE_FAULT_ACT_FATAL;++if((ec==TRBE_EC_STAGE1_ABORT)||(ec==TRBE_EC_STAGE2_ABORT))+returnTRBE_FAULT_ACT_FATAL;++if(is_trbe_wrap(trbsr)&&(ec==TRBE_EC_OTHERS)&&(bsc==TRBE_BSC_FILLED)){+if(get_trbe_write_pointer()==get_trbe_base_pointer())+returnTRBE_FAULT_ACT_WRAP;+}+returnTRBE_FAULT_ACT_SPURIOUS;+}++staticirqreturn_tarm_trbe_irq_handler(intirq,void*dev)+{+structperf_output_handle**handle_ptr=dev;+structperf_output_handle*handle=*handle_ptr;+enumtrbe_fault_actionact;++WARN_ON(!is_trbe_irq(read_sysreg_s(SYS_TRBSR_EL1)));+clr_trbe_irq();++/*+*EnsurethetraceisvisibletotheCPUsand+*anyexternalabortshavebeenresolved.+*/+trbe_drain_buffer();+isb();++if(!perf_get_aux(handle))+returnIRQ_NONE;++if(!is_perf_trbe(handle))+returnIRQ_NONE;++irq_work_run();++act=trbe_get_fault_act(handle);+switch(act){+caseTRBE_FAULT_ACT_WRAP:+trbe_handle_overflow(handle);+break;+caseTRBE_FAULT_ACT_SPURIOUS:+trbe_handle_spurious(handle);+break;+caseTRBE_FAULT_ACT_FATAL:+trbe_stop_and_truncate_event(handle);+break;+}+returnIRQ_HANDLED;+}++staticconststructcoresight_ops_sinkarm_trbe_sink_ops={+.enable=arm_trbe_enable,+.disable=arm_trbe_disable,+.alloc_buffer=arm_trbe_alloc_buffer,+.free_buffer=arm_trbe_free_buffer,+.update_buffer=arm_trbe_update_buffer,+};++staticconststructcoresight_opsarm_trbe_cs_ops={+.sink_ops=&arm_trbe_sink_ops,+};++staticssize_talign_show(structdevice*dev,structdevice_attribute*attr,char*buf)+{+structtrbe_cpudata*cpudata=dev_get_drvdata(dev);++returnsprintf(buf,"%llx\n",cpudata->trbe_align);+}+staticDEVICE_ATTR_RO(align);++staticssize_tdbm_show(structdevice*dev,structdevice_attribute*attr,char*buf)+{+structtrbe_cpudata*cpudata=dev_get_drvdata(dev);++returnsprintf(buf,"%d\n",cpudata->trbe_dbm);+}+staticDEVICE_ATTR_RO(dbm);++staticstructattribute*arm_trbe_attrs[]={+&dev_attr_align.attr,+&dev_attr_dbm.attr,+NULL,+};++staticconststructattribute_grouparm_trbe_group={+.attrs=arm_trbe_attrs,+};++staticconststructattribute_group*arm_trbe_groups[]={+&arm_trbe_group,+NULL,+};++staticvoidarm_trbe_probe_coresight_cpu(void*info)+{+structtrbe_drvdata*drvdata=info;+structcoresight_descdesc={0};+intcpu=smp_processor_id();+structtrbe_cpudata*cpudata=per_cpu_ptr(drvdata->cpudata,cpu);+structcoresight_device*trbe_csdev=per_cpu(csdev_sink,cpu);+u64trbidr=read_sysreg_s(SYS_TRBIDR_EL1);+structdevice*dev;++if(WARN_ON(!cpudata))+gotocpu_clear;++if(trbe_csdev)+return;++cpudata->cpu=smp_processor_id();+cpudata->drvdata=drvdata;+dev=&cpudata->drvdata->pdev->dev;++if(!is_trbe_available()){+pr_err("TRBE is not implemented on cpu %d\n",cpudata->cpu);+gotocpu_clear;+}++if(!is_trbe_programmable(trbidr)){+pr_err("TRBE is owned in higher exception level on cpu %d\n",cpudata->cpu);+gotocpu_clear;+}+desc.name=devm_kasprintf(dev,GFP_KERNEL,"%s%d",DRVNAME,smp_processor_id());+if(IS_ERR(desc.name))+gotocpu_clear;++desc.type=CORESIGHT_DEV_TYPE_SINK;+desc.subtype.sink_subtype=CORESIGHT_DEV_SUBTYPE_SINK_PERCPU_SYSMEM;+desc.ops=&arm_trbe_cs_ops;+desc.pdata=dev_get_platdata(dev);+desc.groups=arm_trbe_groups;+desc.dev=dev;+trbe_csdev=coresight_register(&desc);+if(IS_ERR(trbe_csdev))+gotocpu_clear;++dev_set_drvdata(&trbe_csdev->dev,cpudata);+cpudata->trbe_dbm=get_trbe_flag_update(trbidr);+cpudata->trbe_align=1ULL<<get_trbe_address_align(trbidr);+if(cpudata->trbe_align>SZ_2K){+pr_err("Unsupported alignment on cpu %d\n",cpudata->cpu);+gotocpu_clear;+}+per_cpu(csdev_sink,cpu)=trbe_csdev;+trbe_reset_local();+enable_percpu_irq(drvdata->irq,IRQ_TYPE_NONE);+return;+cpu_clear:+cpumask_clear_cpu(cpudata->cpu,&cpudata->drvdata->supported_cpus);+}++staticvoidarm_trbe_remove_coresight_cpu(void*info)+{+intcpu=smp_processor_id();+structtrbe_drvdata*drvdata=info;+structtrbe_cpudata*cpudata=per_cpu_ptr(drvdata->cpudata,cpu);+structcoresight_device*trbe_csdev=per_cpu(csdev_sink,cpu);++if(trbe_csdev){+coresight_unregister(trbe_csdev);+cpudata->drvdata=NULL;+per_cpu(csdev_sink,cpu)=NULL;+}+disable_percpu_irq(drvdata->irq);+trbe_reset_local();+}++staticintarm_trbe_probe_coresight(structtrbe_drvdata*drvdata)+{+drvdata->cpudata=alloc_percpu(typeof(*drvdata->cpudata));+if(IS_ERR(drvdata->cpudata))+returnPTR_ERR(drvdata->cpudata);++arm_trbe_probe_coresight_cpu(drvdata);+smp_call_function_many(&drvdata->supported_cpus,arm_trbe_probe_coresight_cpu,drvdata,1);+return0;+}++staticintarm_trbe_remove_coresight(structtrbe_drvdata*drvdata)+{+arm_trbe_remove_coresight_cpu(drvdata);+smp_call_function_many(&drvdata->supported_cpus,arm_trbe_remove_coresight_cpu,drvdata,1);+free_percpu(drvdata->cpudata);+return0;+}++staticintarm_trbe_cpu_startup(unsignedintcpu,structhlist_node*node)+{+structtrbe_drvdata*drvdata=hlist_entry_safe(node,structtrbe_drvdata,hotplug_node);++if(cpumask_test_cpu(cpu,&drvdata->supported_cpus)){+if(!per_cpu(csdev_sink,cpu)){+arm_trbe_probe_coresight_cpu(drvdata);+}else{+trbe_reset_local();+enable_percpu_irq(drvdata->irq,IRQ_TYPE_NONE);+}+}+return0;+}++staticintarm_trbe_cpu_teardown(unsignedintcpu,structhlist_node*node)+{+structtrbe_drvdata*drvdata=hlist_entry_safe(node,structtrbe_drvdata,hotplug_node);++if(cpumask_test_cpu(cpu,&drvdata->supported_cpus)){+disable_percpu_irq(drvdata->irq);+trbe_reset_local();+}+return0;+}++staticintarm_trbe_probe_cpuhp(structtrbe_drvdata*drvdata)+{+enumcpuhp_statetrbe_online;++trbe_online=cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,DRVNAME,+arm_trbe_cpu_startup,arm_trbe_cpu_teardown);+if(trbe_online<0)+return-EINVAL;++if(cpuhp_state_add_instance(trbe_online,&drvdata->hotplug_node))+return-EINVAL;++drvdata->trbe_online=trbe_online;+return0;+}++staticvoidarm_trbe_remove_cpuhp(structtrbe_drvdata*drvdata)+{+cpuhp_remove_multi_state(drvdata->trbe_online);+}++staticintarm_trbe_probe_irq(structplatform_device*pdev,+structtrbe_drvdata*drvdata)+{+drvdata->irq=platform_get_irq(pdev,0);+if(!drvdata->irq){+pr_err("IRQ not found for the platform device\n");+return-ENXIO;+}++if(!irq_is_percpu(drvdata->irq)){+pr_err("IRQ is not a PPI\n");+return-EINVAL;+}++if(irq_get_percpu_devid_partition(drvdata->irq,&drvdata->supported_cpus))+return-EINVAL;++drvdata->handle=alloc_percpu(typeof(*drvdata->handle));+if(!drvdata->handle)+return-ENOMEM;++if(request_percpu_irq(drvdata->irq,arm_trbe_irq_handler,DRVNAME,drvdata->handle)){+free_percpu(drvdata->handle);+return-EINVAL;+}+return0;+}++staticvoidarm_trbe_remove_irq(structtrbe_drvdata*drvdata)+{+free_percpu_irq(drvdata->irq,drvdata->handle);+free_percpu(drvdata->handle);+}++staticintarm_trbe_device_probe(structplatform_device*pdev)+{+structcoresight_platform_data*pdata;+structtrbe_drvdata*drvdata;+structdevice*dev=&pdev->dev;+intret;++drvdata=devm_kzalloc(dev,sizeof(*drvdata),GFP_KERNEL);+if(IS_ERR(drvdata))+return-ENOMEM;++pdata=coresight_get_platform_data(dev);+if(IS_ERR(pdata)){+kfree(drvdata);+return-ENOMEM;+}++dev_set_drvdata(dev,drvdata);+dev->platform_data=pdata;+drvdata->pdev=pdev;+ret=arm_trbe_probe_irq(pdev,drvdata);+if(ret)+gotoirq_failed;++ret=arm_trbe_probe_coresight(drvdata);+if(ret)+gotoprobe_failed;++ret=arm_trbe_probe_cpuhp(drvdata);+if(ret)+gotocpuhp_failed;++return0;+cpuhp_failed:+arm_trbe_remove_coresight(drvdata);+probe_failed:+arm_trbe_remove_irq(drvdata);+irq_failed:+kfree(pdata);+kfree(drvdata);+returnret;+}++staticintarm_trbe_device_remove(structplatform_device*pdev)+{+structcoresight_platform_data*pdata=dev_get_platdata(&pdev->dev);+structtrbe_drvdata*drvdata=platform_get_drvdata(pdev);++arm_trbe_remove_coresight(drvdata);+arm_trbe_remove_cpuhp(drvdata);+arm_trbe_remove_irq(drvdata);+kfree(pdata);+kfree(drvdata);+return0;+}++staticconststructof_device_idarm_trbe_of_match[]={+{.compatible="arm,trace-buffer-extension"},+{},+};+MODULE_DEVICE_TABLE(of,arm_trbe_of_match);++staticstructplatform_driverarm_trbe_driver={+.driver={+.name=DRVNAME,+.of_match_table=of_match_ptr(arm_trbe_of_match),+.suppress_bind_attrs=true,+},+.probe=arm_trbe_device_probe,+.remove=arm_trbe_device_remove,+};++staticint__initarm_trbe_init(void)+{+intret;++if(arm64_kernel_unmapped_at_el0()){+pr_err("TRBE wouldn't work if kernel gets unmapped at EL0\n");+return-EOPNOTSUPP;+}++ret=platform_driver_register(&arm_trbe_driver);+if(!ret)+return0;++pr_err("Error registering %s platform driver\n",DRVNAME);+returnret;+}++staticvoid__exitarm_trbe_exit(void)+{+platform_driver_unregister(&arm_trbe_driver);+}+module_init(arm_trbe_init);+module_exit(arm_trbe_exit);++MODULE_AUTHOR("Anshuman Khandual <anshuman.khandual@arm.com>");+MODULE_DESCRIPTION("Arm Trace Buffer Extension (TRBE) driver");+MODULE_LICENSE("GPL v2");
On Wed, Jan 27, 2021 at 02:25:35PM +0530, Anshuman Khandual wrote:
quoted
Trace Buffer Extension (TRBE) implements a trace buffer per CPU which is
accessible via the system registers. The TRBE supports different addressing
modes including CPU virtual address and buffer modes including the circular
buffer mode. The TRBE buffer is addressed by a base pointer (TRBBASER_EL1),
an write pointer (TRBPTR_EL1) and a limit pointer (TRBLIMITR_EL1). But the
access to the trace buffer could be prohibited by a higher exception level
(EL3 or EL2), indicated by TRBIDR_EL1.P. The TRBE can also generate a CPU
private interrupt (PPI) on address translation errors and when the buffer
is full. Overall implementation here is inspired from the Arm SPE driver.
I got this message when applying the patch:
Applying: coresight: sink: Add TRBE driver
.git/rebase-apply/patch:76: new blank line at EOF.
+
warning: 1 line adds whitespace errors.
It could be the additional blank line at the end of documentation file
i.e Documentation/trace/coresight/coresight-trbe.rst, will drop it.
quoted
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
Changes in V3:
- Added new DT bindings document TRBE.yaml
- Changed TRBLIMITR_TRIG_MODE_SHIFT from 2 to 3
- Dropped isb() from trbe_reset_local()
- Dropped gap between (void *) and buf->trbe_base
- Changed 'int' to 'unsigned int' in is_trbe_available()
- Dropped unused function set_trbe_running(), set_trbe_virtual_mode(),
set_trbe_enabled() and set_trbe_limit_pointer()
- Changed get_trbe_flag_update(), is_trbe_programmable() and
get_trbe_address_align() to accept TRBIDR value
- Changed is_trbe_running(), is_trbe_abort(), is_trbe_wrap(), is_trbe_trg(),
is_trbe_irq(), get_trbe_bsc() and get_trbe_ec() to accept TRBSR value
- Dropped snapshot mode condition in arm_trbe_alloc_buffer()
- Exit arm_trbe_init() when arm64_kernel_unmapped_at_el0() is enabled
- Compute trbe_limit before trbe_write to get the updated handle
- Added trbe_stop_and_truncate_event()
- Dropped trbe_handle_fatal()
Documentation/trace/coresight/coresight-trbe.rst | 39 +
arch/arm64/include/asm/sysreg.h | 1 +
drivers/hwtracing/coresight/Kconfig | 11 +
drivers/hwtracing/coresight/Makefile | 1 +
drivers/hwtracing/coresight/coresight-trbe.c | 1023 ++++++++++++++++++++++
drivers/hwtracing/coresight/coresight-trbe.h | 160 ++++
6 files changed, 1235 insertions(+)
create mode 100644 Documentation/trace/coresight/coresight-trbe.rst
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.c
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.h
@@ -0,0 +1,39 @@+.. SPDX-License-Identifier: GPL-2.0++==============================+Trace Buffer Extension (TRBE).+==============================++:Author: Anshuman Khandual <anshuman.khandual@arm.com>+:Date: November 2020++Hardware Description+--------------------++Trace Buffer Extension (TRBE) is a percpu hardware which captures in system+memory, CPU traces generated from a corresponding percpu tracing unit. This+gets plugged in as a coresight sink device because the corresponding trace+genarators (ETE), are plugged in as source device.++The TRBE is not compliant to CoreSight architecture specifications, but is+driven via the CoreSight driver framework to support the ETE (which is+CoreSight compliant) integration.++Sysfs files and directories+---------------------------++The TRBE devices appear on the existing coresight bus alongside the other+coresight devices::++ >$ ls /sys/bus/coresight/devices+ trbe0 trbe1 trbe2 trbe3++The ``trbe<N>`` named TRBEs are associated with a CPU.::++ >$ ls /sys/bus/coresight/devices/trbe0/+ align dbm++*Key file items are:-*+*``align``: TRBE write pointer alignment+*``dbm``: TRBE updates memory with access and dirty flags+
Sure, will add the following new sysfs doc file in this regard.
Marked the KernelVersion as 5.12, will change if required.
new file mode 100644
index 0000000..5cb090f
@@ -0,0 +1,14 @@+What: /sys/bus/coresight/devices/trbe<cpu>/align+Date: Feb 2021+KernelVersion: 5.12+Contact: Anshuman Khandual <anshuman.khandual@arm.com>+Description: (Read) Shows the TRBE write pointer alignment. This value+ is fetched from the TRBIDR register.++What: /sys/bus/coresight/devices/trbe<cpu>/dbm+Date: Feb 2021+KernelVersion: 5.12+Contact: Anshuman Khandual <anshuman.khandual@arm.com>+Description: (Read) Shows if TRBE updates in the memory are with access+ and dirty flag updates as well. This value is fetched from+ the TRBIDR register.
I would make it dependent on ETMv4 as well since it can only be used by that
component.
Sure, will also add CORESIGHT_SOURCE_ETM4X as dependency.
quoted
+ help
+ This driver provides support for percpu Trace Buffer Extension (TRBE).
+ TRBE always needs to be used along with it's corresponding percpu ETE
+ component. ETE generates trace data which is then captured with TRBE.
+ Unlike traditional sink devices, TRBE is a CPU feature accessible via
+ system registers. But it's explicit dependency with trace unit (ETE)
+ requires it to be plugged in as a coresight sink device.
Please add:
"To compile this driver as a module, choose M here: the
module will be called coresight-trbe."
Added.
I'm out of time for today, I will continue on Monday.
On Fri, Feb 05, 2021 at 10:53:30AM -0700, Mathieu Poirier wrote:
On Wed, Jan 27, 2021 at 02:25:35PM +0530, Anshuman Khandual wrote:
quoted
Trace Buffer Extension (TRBE) implements a trace buffer per CPU which is
accessible via the system registers. The TRBE supports different addressing
modes including CPU virtual address and buffer modes including the circular
buffer mode. The TRBE buffer is addressed by a base pointer (TRBBASER_EL1),
an write pointer (TRBPTR_EL1) and a limit pointer (TRBLIMITR_EL1). But the
access to the trace buffer could be prohibited by a higher exception level
(EL3 or EL2), indicated by TRBIDR_EL1.P. The TRBE can also generate a CPU
private interrupt (PPI) on address translation errors and when the buffer
is full. Overall implementation here is inspired from the Arm SPE driver.
I got this message when applying the patch:
Applying: coresight: sink: Add TRBE driver
.git/rebase-apply/patch:76: new blank line at EOF.
+
warning: 1 line adds whitespace errors.
quoted
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
Changes in V3:
- Added new DT bindings document TRBE.yaml
- Changed TRBLIMITR_TRIG_MODE_SHIFT from 2 to 3
- Dropped isb() from trbe_reset_local()
- Dropped gap between (void *) and buf->trbe_base
- Changed 'int' to 'unsigned int' in is_trbe_available()
- Dropped unused function set_trbe_running(), set_trbe_virtual_mode(),
set_trbe_enabled() and set_trbe_limit_pointer()
- Changed get_trbe_flag_update(), is_trbe_programmable() and
get_trbe_address_align() to accept TRBIDR value
- Changed is_trbe_running(), is_trbe_abort(), is_trbe_wrap(), is_trbe_trg(),
is_trbe_irq(), get_trbe_bsc() and get_trbe_ec() to accept TRBSR value
- Dropped snapshot mode condition in arm_trbe_alloc_buffer()
- Exit arm_trbe_init() when arm64_kernel_unmapped_at_el0() is enabled
- Compute trbe_limit before trbe_write to get the updated handle
- Added trbe_stop_and_truncate_event()
- Dropped trbe_handle_fatal()
Documentation/trace/coresight/coresight-trbe.rst | 39 +
arch/arm64/include/asm/sysreg.h | 1 +
drivers/hwtracing/coresight/Kconfig | 11 +
drivers/hwtracing/coresight/Makefile | 1 +
drivers/hwtracing/coresight/coresight-trbe.c | 1023 ++++++++++++++++++++++
drivers/hwtracing/coresight/coresight-trbe.h | 160 ++++
6 files changed, 1235 insertions(+)
create mode 100644 Documentation/trace/coresight/coresight-trbe.rst
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.c
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.h
@@ -0,0 +1,39 @@+.. SPDX-License-Identifier: GPL-2.0++==============================+Trace Buffer Extension (TRBE).+==============================++:Author: Anshuman Khandual <anshuman.khandual@arm.com>+:Date: November 2020++Hardware Description+--------------------++Trace Buffer Extension (TRBE) is a percpu hardware which captures in system+memory, CPU traces generated from a corresponding percpu tracing unit. This+gets plugged in as a coresight sink device because the corresponding trace+genarators (ETE), are plugged in as source device.++The TRBE is not compliant to CoreSight architecture specifications, but is+driven via the CoreSight driver framework to support the ETE (which is+CoreSight compliant) integration.++Sysfs files and directories+---------------------------++The TRBE devices appear on the existing coresight bus alongside the other+coresight devices::++ >$ ls /sys/bus/coresight/devices+ trbe0 trbe1 trbe2 trbe3++The ``trbe<N>`` named TRBEs are associated with a CPU.::++ >$ ls /sys/bus/coresight/devices/trbe0/+ align dbm++*Key file items are:-*+*``align``: TRBE write pointer alignment+*``dbm``: TRBE updates memory with access and dirty flags+
I would make it dependent on ETMv4 as well since it can only be used by that
component.
quoted
+ help
+ This driver provides support for percpu Trace Buffer Extension (TRBE).
+ TRBE always needs to be used along with it's corresponding percpu ETE
+ component. ETE generates trace data which is then captured with TRBE.
+ Unlike traditional sink devices, TRBE is a CPU feature accessible via
+ system registers. But it's explicit dependency with trace unit (ETE)
+ requires it to be plugged in as a coresight sink device.
Please add:
"To compile this driver as a module, choose M here: the
module will be called coresight-trbe."
I'm out of time for today, I will continue on Monday.
Mathieu
@@ -0,0 +1,1023 @@+// SPDX-License-Identifier: GPL-2.0+/*+*ThisdriverenablesTraceBufferExtension(TRBE)asaper-cpucoresight+*sinkdevicecouldthenpairwithanappropriateper-cpucoresightsource+*device(ETE)thusgeneratingrequiredtracedata.Tracecanbeenabled+*viatheperfframework.+*+*Copyright(C)2020ARMLtd.+*+*Author:AnshumanKhandual<anshuman.khandual@arm.com>+*/+#define DRVNAME "arm_trbe"++#define pr_fmt(fmt) DRVNAME ": " fmt++#include"coresight-trbe.h"++#define PERF_IDX2OFF(idx, buf) ((idx) % ((buf)->nr_pages << PAGE_SHIFT))++/*+*Apaddingpacketthatwillhelptheuserspacetools+*inskippingrelevantsectionsinthecapturedtrace+*datawhichcouldnotbedecoded.TRBEdoesn'tsupport+*formattingthetracedata,unlikethelegacyCoreSight+*sinksandthusweuseETEtracepacketstopadthe+*sectionsofthebuffer.+*/+#define ETE_IGNORE_PACKET 0x70++/*+*Minimumamountofmeaningfultracewillcontain:+*A-Sync,TraceInfo,TraceOn,Address,Atom.+*Thisisabout44bytesofETEtrace.Tobeon+*thesaferside,weassume64bytesistheminimum+*spacerequiredforameaningfulsession,before+*wehita"WRAP"event.+*/+#define TRBE_TRACE_MIN_BUF_SIZE 64++enumtrbe_fault_action{+TRBE_FAULT_ACT_WRAP,+TRBE_FAULT_ACT_SPURIOUS,+TRBE_FAULT_ACT_FATAL,+};++structtrbe_buf{+unsignedlongtrbe_base;+unsignedlongtrbe_limit;+unsignedlongtrbe_write;+intnr_pages;+void**pages;+boolsnapshot;+structtrbe_cpudata*cpudata;+};++structtrbe_cpudata{+booltrbe_dbm;+u64trbe_align;+intcpu;+enumcs_modemode;+structtrbe_buf*buf;+structtrbe_drvdata*drvdata;+};++structtrbe_drvdata{+structtrbe_cpudata__percpu*cpudata;+structperf_output_handle__percpu**handle;+structhlist_nodehotplug_node;+intirq;+cpumask_tsupported_cpus;+enumcpuhp_statetrbe_online;+structplatform_device*pdev;+};++staticinttrbe_alloc_node(structperf_event*event)+{+if(event->cpu==-1)+returnNUMA_NO_NODE;+returncpu_to_node(event->cpu);+}++staticvoidtrbe_drain_buffer(void)+{+asm(TSB_CSYNC);+dsb(nsh);+}++staticvoidtrbe_drain_and_disable_local(void)+{+trbe_drain_buffer();+write_sysreg_s(0,SYS_TRBLIMITR_EL1);+isb();+}++staticvoidtrbe_reset_local(void)+{+trbe_drain_and_disable_local();+write_sysreg_s(0,SYS_TRBPTR_EL1);+write_sysreg_s(0,SYS_TRBBASER_EL1);+write_sysreg_s(0,SYS_TRBSR_EL1);+}++staticvoidtrbe_stop_and_truncate_event(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);++/*+*Wecannotproceedwiththebuffercollectionandwe+*donothaveanydataforthecurrentsession.The+*etm_perfdriverexpectstocloseouttheaux_buffer+*atevent_stop().SodisabletheTRBEhereandleave+*theupdate_buffer()toreturna0size.+*/+trbe_drain_and_disable_local();+perf_aux_output_flag(handle,PERF_AUX_FLAG_TRUNCATED);+*this_cpu_ptr(buf->cpudata->drvdata->handle)=NULL;+}++/*+*TRBEBufferManagement+*+*TheTRBEbufferspansfromthebasepointertillthelimitpointer.Whenenabled,+*itstartswritingtracedatafromthewritepointeronwardtillthelimitpointer.+*Whenthewritepointerreachestheaddressjustbeforethelimitpointer,itgets+*wrappedaroundagaintothebasepointer.ThisiscalledaTRBEwrapevent,which+*generatesamaintenanceinterruptwhenoperatedinWRAPorFILLmode.Thisdriver+*usesFILLmode,wheretheTRBEstopsthetracecollectionatwrapevent.TheIRQ+*handlerupdatestheAUXbufferandre-enablestheTRBEwithupdatedWRITEand+*LIMITpointers.+*+*WraparoundwithanIRQ+*------<------<-------<-----<-----+*||+*------>------>------->----->-----+*+*+---------------+-----------------------++*|||+*+---------------+-----------------------++*BasePointerWritePointerLimitPointer+*+*ThebaseandlimitpointersalwaysneedstobePAGE_SIZEaligned.Butthewrite+*pointercanbealignedtotheimplementationdefinedTRBEtracebufferalignment+*ascapturedintrbe_cpudata->trbe_align.+*+*+*headtailwakeup+*+---------------------------------------+-----~~------+*|$$$$$$$|################|$$$$$$$$$$$$$$||+*+---------------------------------------+-----~~------+*BasePointerWritePointerLimitPointer+*+*Theperf_output_handleindices(head,tail,wakeup)aremonotonicallyincreasing+*valueswhichtracksallthedriverwritesanduserreadsfromtheperfauxiliary+*buffer.Generally[head..tail]istheareawherethedrivercanwriteintounless+*thewakeupisbehindthetail.EnabledTRBEbufferspanneedstobeadjustedand+*configureddependingontheperf_output_handleindices,sothatthedriverdoes+*notoverrideintoareasintheperfauxiliarybufferwhichisbeingoryettobe+*consumedfromtheuserspace.TheenabledTRBEbufferareaisamovingsubsetof+*theallocatedperfauxiliarybuffer.+*/+staticvoidtrbe_pad_buf(structperf_output_handle*handle,intlen)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);+u64head=PERF_IDX2OFF(handle->head,buf);++memset((void*)buf->trbe_base+head,ETE_IGNORE_PACKET,len);+if(!buf->snapshot)+perf_aux_output_skip(handle,len);+}++staticunsignedlongtrbe_snapshot_offset(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);++/*+*TheETEtracehasalignmentsynchronizationpacketsallowing+*thedecodertoresetincaseofanoverfloworcorruption.+*Sowecanusetheentirebufferforthesnapshotmode.+*/+returnbuf->nr_pages*PAGE_SIZE;+}++/*+*TRBELimitCalculation+*+*ThefollowingmarkersareusedtoillustratevariousTRBEbuffersituations.+*+*$$$$-Dataarea,unconsumedcapturedtracedata,nottobeoverridden+*####-Freearea,enabled,tracewillbewritten+*%%%%-Freearea,disabled,tracewillnotbewritten+*====-Freearea,paddedwithETE_IGNORE_PACKET,tracewillbeskipped+*/+staticunsignedlong__trbe_normal_offset(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);+structtrbe_cpudata*cpudata=buf->cpudata;+constu64bufsize=buf->nr_pages*PAGE_SIZE;+u64limit=bufsize;+u64head,tail,wakeup;++head=PERF_IDX2OFF(handle->head,buf);++/*+*head+*------->|+*|+*headTRBEaligntail+*+----|-------|---------------|-------++*|$$$$|=======|###############|$$$$$$$|+*+----|-------|---------------|-------++*trbe_basetrbe_base+nr_pages+*+*Perfauxbufferoutputheadpositioncanbemisaligneddependingon+*variousfactorsincludinguserspacereads.Incasemisaligned,head+*needstobealignedbeforeTRBEcanbeconfigured.Padthealignment+*gapwithETE_IGNORE_PACKETbytesthatwillbeignoredbyusertools+*andskipthissectionthusadvancingthehead.+*/+if(!IS_ALIGNED(head,cpudata->trbe_align)){+unsignedlongdelta=roundup(head,cpudata->trbe_align)-head;++delta=min(delta,handle->size);+trbe_pad_buf(handle,delta);+head=PERF_IDX2OFF(handle->head,buf);+}++/*+*head=tail(size=0)+*+----|-------------------------------++*|$$$$|$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$|+*+----|-------------------------------++*trbe_basetrbe_base+nr_pages+*+*Perfauxbufferdoesnothaveanyspaceforthedrivertowriteinto.+*Justcommunicatetracetruncationeventtotheuserspacebymarking+*itwithPERF_AUX_FLAG_TRUNCATED.+*/+if(!handle->size){+perf_aux_output_flag(handle,PERF_AUX_FLAG_TRUNCATED);+return0;+}++/* Compute the tail and wakeup indices now that we've aligned head */+tail=PERF_IDX2OFF(handle->head+handle->size,buf);+wakeup=PERF_IDX2OFF(handle->wakeup,buf);++/*+*LetscalculatethebufferareawhichTRBEcouldwriteinto.There+*arethreepossiblescenarioshere.Limitneedstobealignedwith+*PAGE_SIZEpertheTRBErequirement.Alwaysavoidclobberingthe+*unconsumeddata.+*+*1)head<tail+*+*headtail+*+----|-----------------------|-------++*|$$$$|#######################|$$$$$$$|+*+----|-----------------------|-------++*trbe_baselimittrbe_base+nr_pages+*+*TRBEcouldwriteinto[head..tail]area.Unlessthetailisrightat+*theendofthebuffer,neitheranwraparoundnoranIRQisexpected+*whilebeingenabled.+*+*2)head==tail+*+*head=tail(size>0)+*+----|-------------------------------++*|%%%%|###############################|+*+----|-------------------------------++*trbe_baselimit=trbe_base+nr_pages+*+*TRBEshouldjustwriteinto[head..base+nr_pages]areaeventhough+*theentirebufferisempty.Reasonbeing,whenthetracereachesthe+*endofthebuffer,itwilljustwraparoundwithanIRQgivingan+*opportunitytoreconfigurethebuffer.+*+*3)tail<head+*+*tailhead+*+----|-----------------------|-------++*|%%%%|$$$$$$$$$$$$$$$$$$$$$$$|#######|+*+----|-----------------------|-------++*trbe_baselimit=trbe_base+nr_pages+*+*TRBEshouldjustwriteinto[head..base+nr_pages]areaeventhough+*the[trbe_base..tail]isalsoempty.Reasonbeing,whenthetrace+*reachestheendofthebuffer,itwilljustwraparoundwithanIRQ+*givinganopportunitytoreconfigurethebuffer.+*/+if(head<tail)+limit=round_down(tail,PAGE_SIZE);++/*+*Wakeupmaybearbitrarilyfarintothefuture.Ifit'snotinthe+*currentgeneration,eitherwe'llwrapbeforehittingit,orit's+*inthepastandhasbeenhandledalready.+*+*Ifthere'sawakeupbeforewewrap,arrangetobewokenupbythe+*pageboundaryfollowingit.Keepthetailboundaryifthat'slower.+*+*headwakeuptail+*+----|---------------|-------|-------++*|$$$$|###############|%%%%%%%|$$$$$$$|+*+----|---------------|-------|-------++*trbe_baselimittrbe_base+nr_pages+*/+if(handle->wakeup<(handle->head+handle->size)&&head<=wakeup)+limit=min(limit,round_up(wakeup,PAGE_SIZE));++/*+*Therearetwosituationwhenthiscanhappeni.elimitisbefore+*theheadandhenceTRBEcannotbeconfigured.+*+*1)head<tail(aligneddownwithPAGE_SIZE)andalsotheyareboth+*withinthesamePAGEsizerange.+*+*PAGE_SIZE+*|----------------------|+*+*limitheadtail+*+------------|------|--------|-------++*|$$$$$$$$$$$$$$$$$$$|========|$$$$$$$|+*+------------|------|--------|-------++*trbe_basetrbe_base+nr_pages+*+*2)head<wakeup(alignedupwithPAGE_SIZE)<tailandalsoboth+*headandwakeuparewithinsamePAGEsizerange.+*+*PAGE_SIZE+*|----------------------|+*+*limitheadwakeuptail+*+----|------|-------|--------|-------++*|$$$$$$$$$$$|=======|========|$$$$$$$|+*+----|------|-------|--------|-------++*trbe_basetrbe_base+nr_pages+*/+if(limit>head)+returnlimit;++trbe_pad_buf(handle,handle->size);+perf_aux_output_flag(handle,PERF_AUX_FLAG_TRUNCATED);+return0;+}++staticunsignedlongtrbe_normal_offset(structperf_output_handle*handle)+{+structtrbe_buf*buf=perf_get_aux(handle);+u64limit=__trbe_normal_offset(handle);+u64head=PERF_IDX2OFF(handle->head,buf);++/*+*Iftheheadistooclosetothelimitandwedon't+*havespaceforameaningfulrun,weratherpadit+*andstartfresh.+*/+if(limit&&(limit-head<TRBE_TRACE_MIN_BUF_SIZE)){+trbe_pad_buf(handle,limit-head);+limit=__trbe_normal_offset(handle);+}+returnlimit;+}++staticunsignedlongcompute_trbe_buffer_limit(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);+unsignedlongoffset;++if(buf->snapshot)+offset=trbe_snapshot_offset(handle);+else+offset=trbe_normal_offset(handle);+returnbuf->trbe_base+offset;+}++staticvoidclr_trbe_status(void)+{+u64trbsr=read_sysreg_s(SYS_TRBSR_EL1);++WARN_ON(is_trbe_enabled());+trbsr&=~TRBSR_IRQ;+trbsr&=~TRBSR_TRG;+trbsr&=~TRBSR_WRAP;+trbsr&=~(TRBSR_EC_MASK<<TRBSR_EC_SHIFT);+trbsr&=~(TRBSR_BSC_MASK<<TRBSR_BSC_SHIFT);+trbsr&=~TRBSR_STOP;+write_sysreg_s(trbsr,SYS_TRBSR_EL1);+}++staticvoidset_trbe_limit_pointer_enabled(unsignedlongaddr)+{+u64trblimitr=read_sysreg_s(SYS_TRBLIMITR_EL1);++WARN_ON(!IS_ALIGNED(addr,(1UL<<TRBLIMITR_LIMIT_SHIFT)));+WARN_ON(!IS_ALIGNED(addr,PAGE_SIZE));++trblimitr&=~TRBLIMITR_NVM;+trblimitr&=~(TRBLIMITR_FILL_MODE_MASK<<TRBLIMITR_FILL_MODE_SHIFT);+trblimitr&=~(TRBLIMITR_TRIG_MODE_MASK<<TRBLIMITR_TRIG_MODE_SHIFT);+trblimitr&=~(TRBLIMITR_LIMIT_MASK<<TRBLIMITR_LIMIT_SHIFT);++/*+*Filltracebuffermodeisusedherewhileconfiguringthe+*TRBEfortracecapture.Inthisparticularmode,thetrace+*collectionisstoppedandamaintenanceinterruptisraised+*whenthecurrentwritepointerwraps.Thispauseintrace+*collectiongivesthesoftwareanopportunitytocapturethe+*tracedataintheinterrupthandler,beforereconfiguring+*theTRBE.+*/+trblimitr|=(TRBE_FILL_MODE_FILL&TRBLIMITR_FILL_MODE_MASK)<<TRBLIMITR_FILL_MODE_SHIFT;++/*+*TriggermodeisnotusedherewhileconfiguringtheTRBEfor+*thetracecapture.Hencejustkeepthisintheignoremode.+*/+trblimitr|=(TRBE_TRIG_MODE_IGNORE&TRBLIMITR_TRIG_MODE_MASK)<<TRBLIMITR_TRIG_MODE_SHIFT;+trblimitr|=(addr&PAGE_MASK);++trblimitr|=TRBLIMITR_ENABLE;+write_sysreg_s(trblimitr,SYS_TRBLIMITR_EL1);+}++staticvoidtrbe_enable_hw(structtrbe_buf*buf)+{+WARN_ON(buf->trbe_write<buf->trbe_base);+WARN_ON(buf->trbe_write>=buf->trbe_limit);+set_trbe_disabled();+isb();+clr_trbe_status();+set_trbe_base_pointer(buf->trbe_base);+set_trbe_write_pointer(buf->trbe_write);++/*+*Synchronizealltheregisterupdates+*tillnowbeforeenablingtheTRBE.+*/+isb();+set_trbe_limit_pointer_enabled(buf->trbe_limit);++/* Synchronize the TRBE enable event */+isb();+}++staticvoid*arm_trbe_alloc_buffer(structcoresight_device*csdev,+structperf_event*event,void**pages,+intnr_pages,boolsnapshot)+{+structtrbe_buf*buf;+structpage**pglist;+inti;++/*+*TRBELIMITandTRBEWRITEpointersmustbepagealigned.Butwith+*justasinglepage,thereisnotmuchroomleftwhilewritinginto+*apartiallyfilledTRBEbuffer.Hencerestricttheminimumbuffer+*sizeastwopages.+*/+if(nr_pages<2)+returnNULL;++buf=kzalloc_node(sizeof(*buf),GFP_KERNEL,trbe_alloc_node(event));+if(IS_ERR(buf))+returnERR_PTR(-ENOMEM);++pglist=kcalloc(nr_pages,sizeof(*pglist),GFP_KERNEL);+if(IS_ERR(pglist)){+kfree(buf);+returnERR_PTR(-ENOMEM);+}++for(i=0;i<nr_pages;i++)+pglist[i]=virt_to_page(pages[i]);++buf->trbe_base=(unsignedlong)vmap(pglist,nr_pages,VM_MAP,PAGE_KERNEL);+if(IS_ERR((void*)buf->trbe_base)){+kfree(pglist);+kfree(buf);+returnERR_PTR(buf->trbe_base);+}+buf->trbe_limit=buf->trbe_base+nr_pages*PAGE_SIZE;+buf->trbe_write=buf->trbe_base;+buf->snapshot=snapshot;+buf->nr_pages=nr_pages;+buf->pages=pages;+kfree(pglist);+returnbuf;+}++voidarm_trbe_free_buffer(void*config)+{+structtrbe_buf*buf=config;++vunmap((void*)buf->trbe_base);+kfree(buf);+}++staticunsignedlongarm_trbe_update_buffer(structcoresight_device*csdev,+structperf_output_handle*handle,+void*config)+{+structtrbe_drvdata*drvdata=dev_get_drvdata(csdev->dev.parent);+structtrbe_cpudata*cpudata=dev_get_drvdata(&csdev->dev);+structtrbe_buf*buf=config;+unsignedlongsize,offset;++WARN_ON(buf->cpudata!=cpudata);+WARN_ON(cpudata->cpu!=smp_processor_id());+WARN_ON(cpudata->drvdata!=drvdata);+if(cpudata->mode!=CS_MODE_PERF)+return-EINVAL;++/*+*IftheTRBEwasdisabledduetolackofspaceintheAUXbufferora+*spuriousfault,thedriverleavesitdisabled,truncatingthebuffer.+*Sincetheetm_perfdriverexpectstocloseouttheAUXbuffer,the+*driverskipsit.Thus,justpassin0sizeheretoindicatethatthe+*bufferwastruncated.+*/+if(!is_trbe_enabled())+return0;+/*+*perfhandlestructureneedstobesharedwiththeTRBEIRQhandlerfor+*capturingtracedataandrestartingthehandle.Thereisaprobability+*ofanundefinedreferencebasedcrashwhenetmeventisbeingstopped+*whileaTRBEIRQalsogettingprocessed.Thishappensduetherelease+*ofperfhandleviaperf_aux_output_end()inetm_event_stop().Stopping+*theTRBEherewillensurethatnoIRQcouldbegeneratedwhentheperf+*handlegetsfreedinetm_event_stop().+*/+trbe_drain_and_disable_local();+offset=get_trbe_write_pointer()-get_trbe_base_pointer();+size=offset-PERF_IDX2OFF(handle->head,buf);+if(buf->snapshot)+handle->head+=size;+returnsize;+}++staticintarm_trbe_enable(structcoresight_device*csdev,u32mode,void*data)+{+structtrbe_drvdata*drvdata=dev_get_drvdata(csdev->dev.parent);+structtrbe_cpudata*cpudata=dev_get_drvdata(&csdev->dev);+structperf_output_handle*handle=data;+structtrbe_buf*buf=etm_perf_sink_config(handle);++WARN_ON(cpudata->cpu!=smp_processor_id());+WARN_ON(cpudata->drvdata!=drvdata);+if(mode!=CS_MODE_PERF)+return-EINVAL;++*this_cpu_ptr(drvdata->handle)=handle;+cpudata->buf=buf;+cpudata->mode=mode;+buf->cpudata=cpudata;+buf->trbe_limit=compute_trbe_buffer_limit(handle);+buf->trbe_write=buf->trbe_base+PERF_IDX2OFF(handle->head,buf);+if(buf->trbe_limit==buf->trbe_base){+trbe_stop_and_truncate_event(handle);+return0;+}+trbe_enable_hw(buf);+return0;+}++staticintarm_trbe_disable(structcoresight_device*csdev)+{+structtrbe_drvdata*drvdata=dev_get_drvdata(csdev->dev.parent);+structtrbe_cpudata*cpudata=dev_get_drvdata(&csdev->dev);+structtrbe_buf*buf=cpudata->buf;++WARN_ON(buf->cpudata!=cpudata);+WARN_ON(cpudata->cpu!=smp_processor_id());+WARN_ON(cpudata->drvdata!=drvdata);+if(cpudata->mode!=CS_MODE_PERF)+return-EINVAL;++trbe_drain_and_disable_local();+buf->cpudata=NULL;+cpudata->buf=NULL;+cpudata->mode=CS_MODE_DISABLED;+return0;+}++staticvoidtrbe_handle_spurious(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);++buf->trbe_limit=compute_trbe_buffer_limit(handle);+buf->trbe_write=buf->trbe_base+PERF_IDX2OFF(handle->head,buf);+if(buf->trbe_limit==buf->trbe_base){+trbe_drain_and_disable_local();+return;+}+trbe_enable_hw(buf);+}++staticvoidtrbe_handle_overflow(structperf_output_handle*handle)+{+structperf_event*event=handle->event;+structtrbe_buf*buf=etm_perf_sink_config(handle);+unsignedlongoffset,size;+structetm_event_data*event_data;++offset=get_trbe_limit_pointer()-get_trbe_base_pointer();+size=offset-PERF_IDX2OFF(handle->head,buf);+if(buf->snapshot)+handle->head=offset;+perf_aux_output_end(handle,size);++event_data=perf_aux_output_begin(handle,event);+if(!event_data){+trbe_drain_and_disable_local();+*this_cpu_ptr(buf->cpudata->drvdata->handle)=NULL;+return;+}+buf->trbe_limit=compute_trbe_buffer_limit(handle);+buf->trbe_write=buf->trbe_base+PERF_IDX2OFF(handle->head,buf);+if(buf->trbe_limit==buf->trbe_base){+trbe_stop_and_truncate_event(handle);+return;+}+*this_cpu_ptr(buf->cpudata->drvdata->handle)=handle;+trbe_enable_hw(buf);+}++staticboolis_perf_trbe(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);+structtrbe_cpudata*cpudata=buf->cpudata;+structtrbe_drvdata*drvdata=cpudata->drvdata;+intcpu=smp_processor_id();++WARN_ON(buf->trbe_base!=get_trbe_base_pointer());+WARN_ON(buf->trbe_limit!=get_trbe_limit_pointer());++if(cpudata->mode!=CS_MODE_PERF)+returnfalse;++if(cpudata->cpu!=cpu)+returnfalse;++if(!cpumask_test_cpu(cpu,&drvdata->supported_cpus))+returnfalse;++returntrue;+}++staticenumtrbe_fault_actiontrbe_get_fault_act(structperf_output_handle*handle)+{+u64trbsr=read_sysreg_s(SYS_TRBSR_EL1);+intec=get_trbe_ec(trbsr);+intbsc=get_trbe_bsc(trbsr);++WARN_ON(is_trbe_running(trbsr));+if(is_trbe_trg(trbsr)||is_trbe_abort(trbsr))+returnTRBE_FAULT_ACT_FATAL;++if((ec==TRBE_EC_STAGE1_ABORT)||(ec==TRBE_EC_STAGE2_ABORT))+returnTRBE_FAULT_ACT_FATAL;++if(is_trbe_wrap(trbsr)&&(ec==TRBE_EC_OTHERS)&&(bsc==TRBE_BSC_FILLED)){+if(get_trbe_write_pointer()==get_trbe_base_pointer())+returnTRBE_FAULT_ACT_WRAP;+}+returnTRBE_FAULT_ACT_SPURIOUS;+}++staticirqreturn_tarm_trbe_irq_handler(intirq,void*dev)+{+structperf_output_handle**handle_ptr=dev;+structperf_output_handle*handle=*handle_ptr;+enumtrbe_fault_actionact;++WARN_ON(!is_trbe_irq(read_sysreg_s(SYS_TRBSR_EL1)));+clr_trbe_irq();++/*+*EnsurethetraceisvisibletotheCPUsand+*anyexternalabortshavebeenresolved.+*/+trbe_drain_buffer();+isb();++if(!perf_get_aux(handle))+returnIRQ_NONE;++if(!is_perf_trbe(handle))+returnIRQ_NONE;++irq_work_run();
There is a comment in the SPE driver about this. Since this driver closely
follows that implementation it would be nice to have the comments as well.
Otherwise the reader has to constantly go back to the original driver.
I will come back to this function later.
Where was the memory for cpudata allocated? As far as I can tell, at this time
it is just a pointer that was not allocated and as such it should be NULL.
From: Rob Herring <robh@kernel.org> Date: 2021-02-10 00:40:15
On Wed, Jan 27, 2021 at 02:25:30PM +0530, Anshuman Khandual wrote:
quoted hunk
From: Suzuki K Poulose <suzuki.poulose@arm.com>
Document the device tree bindings for Embedded Trace Extensions.
ETE can be connected to legacy coresight components and thus
could optionally contain a connection graph as described by
the CoreSight bindings.
Cc: devicetree@vger.kernel.org
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Rob Herring <robh@kernel.org>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
Changes in V3:
- Fixed all DT yaml semantics problems
Documentation/devicetree/bindings/arm/ete.yaml | 74 ++++++++++++++++++++++++++
1 file changed, 74 insertions(+)
create mode 100644 Documentation/devicetree/bindings/arm/ete.yaml
@@ -0,0 +1,74 @@+# SPDX-License-Identifier: GPL-2.0-only or BSD-2-Clause+# Copyright 2021, Arm Ltd+%YAML1.2+---+$id:"http://devicetree.org/schemas/arm/ete.yaml#"+$schema:"http://devicetree.org/meta-schemas/core.yaml#"++title:ARM Embedded Trace Extensions++maintainers:+-Suzuki K Poulose <suzuki.poulose@arm.com>+-Mathieu Poirier <mathieu.poirier@linaro.org>++description:|+Arm Embedded Trace Extension(ETE) is a per CPU trace component that+allows tracing the CPU execution. It overlaps with the CoreSight ETMv4+architecture and has extended support for future architecture changes.+The trace generated by the ETE could be stored via legacy CoreSight+components (e.g, TMC-ETR) or other means (e.g, using a per CPU buffer+Arm Trace Buffer Extension (TRBE)). Since the ETE can be connected to+legacy CoreSight components, a node must be listed per instance, along+with any optional connection graph as per the coresight bindings.+See bindings/arm/coresight.txt.++properties:+$nodename:+pattern:"^ete([0-9a-f]+)$"+compatible:+items:+-const:arm,embedded-trace-extension++cpu:
We've already established 'cpus' for this purpose.
+ description: |
+ Handle to the cpu this ETE is bound to.
+ $ref: /schemas/types.yaml#/definitions/phandle
+
+ out-ports:
+ type: object
On Fri, Feb 05, 2021 at 10:53:30AM -0700, Mathieu Poirier wrote:
quoted
On Wed, Jan 27, 2021 at 02:25:35PM +0530, Anshuman Khandual wrote:
quoted
Trace Buffer Extension (TRBE) implements a trace buffer per CPU which is
accessible via the system registers. The TRBE supports different addressing
modes including CPU virtual address and buffer modes including the circular
buffer mode. The TRBE buffer is addressed by a base pointer (TRBBASER_EL1),
an write pointer (TRBPTR_EL1) and a limit pointer (TRBLIMITR_EL1). But the
access to the trace buffer could be prohibited by a higher exception level
(EL3 or EL2), indicated by TRBIDR_EL1.P. The TRBE can also generate a CPU
private interrupt (PPI) on address translation errors and when the buffer
is full. Overall implementation here is inspired from the Arm SPE driver.
I got this message when applying the patch:
Applying: coresight: sink: Add TRBE driver
.git/rebase-apply/patch:76: new blank line at EOF.
+
warning: 1 line adds whitespace errors.
quoted
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
Changes in V3:
- Added new DT bindings document TRBE.yaml
- Changed TRBLIMITR_TRIG_MODE_SHIFT from 2 to 3
- Dropped isb() from trbe_reset_local()
- Dropped gap between (void *) and buf->trbe_base
- Changed 'int' to 'unsigned int' in is_trbe_available()
- Dropped unused function set_trbe_running(), set_trbe_virtual_mode(),
set_trbe_enabled() and set_trbe_limit_pointer()
- Changed get_trbe_flag_update(), is_trbe_programmable() and
get_trbe_address_align() to accept TRBIDR value
- Changed is_trbe_running(), is_trbe_abort(), is_trbe_wrap(), is_trbe_trg(),
is_trbe_irq(), get_trbe_bsc() and get_trbe_ec() to accept TRBSR value
- Dropped snapshot mode condition in arm_trbe_alloc_buffer()
- Exit arm_trbe_init() when arm64_kernel_unmapped_at_el0() is enabled
- Compute trbe_limit before trbe_write to get the updated handle
- Added trbe_stop_and_truncate_event()
- Dropped trbe_handle_fatal()
Documentation/trace/coresight/coresight-trbe.rst | 39 +
arch/arm64/include/asm/sysreg.h | 1 +
drivers/hwtracing/coresight/Kconfig | 11 +
drivers/hwtracing/coresight/Makefile | 1 +
drivers/hwtracing/coresight/coresight-trbe.c | 1023 ++++++++++++++++++++++
drivers/hwtracing/coresight/coresight-trbe.h | 160 ++++
6 files changed, 1235 insertions(+)
create mode 100644 Documentation/trace/coresight/coresight-trbe.rst
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.c
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.h
@@ -0,0 +1,39 @@+.. SPDX-License-Identifier: GPL-2.0++==============================+Trace Buffer Extension (TRBE).+==============================++:Author: Anshuman Khandual <anshuman.khandual@arm.com>+:Date: November 2020++Hardware Description+--------------------++Trace Buffer Extension (TRBE) is a percpu hardware which captures in system+memory, CPU traces generated from a corresponding percpu tracing unit. This+gets plugged in as a coresight sink device because the corresponding trace+genarators (ETE), are plugged in as source device.++The TRBE is not compliant to CoreSight architecture specifications, but is+driven via the CoreSight driver framework to support the ETE (which is+CoreSight compliant) integration.++Sysfs files and directories+---------------------------++The TRBE devices appear on the existing coresight bus alongside the other+coresight devices::++ >$ ls /sys/bus/coresight/devices+ trbe0 trbe1 trbe2 trbe3++The ``trbe<N>`` named TRBEs are associated with a CPU.::++ >$ ls /sys/bus/coresight/devices/trbe0/+ align dbm++*Key file items are:-*+*``align``: TRBE write pointer alignment+*``dbm``: TRBE updates memory with access and dirty flags+
I would make it dependent on ETMv4 as well since it can only be used by that
component.
quoted
+ help
+ This driver provides support for percpu Trace Buffer Extension (TRBE).
+ TRBE always needs to be used along with it's corresponding percpu ETE
+ component. ETE generates trace data which is then captured with TRBE.
+ Unlike traditional sink devices, TRBE is a CPU feature accessible via
+ system registers. But it's explicit dependency with trace unit (ETE)
+ requires it to be plugged in as a coresight sink device.
Please add:
"To compile this driver as a module, choose M here: the
module will be called coresight-trbe."
I'm out of time for today, I will continue on Monday.
Mathieu
@@ -0,0 +1,1023 @@+// SPDX-License-Identifier: GPL-2.0+/*+*ThisdriverenablesTraceBufferExtension(TRBE)asaper-cpucoresight+*sinkdevicecouldthenpairwithanappropriateper-cpucoresightsource+*device(ETE)thusgeneratingrequiredtracedata.Tracecanbeenabled+*viatheperfframework.+*+*Copyright(C)2020ARMLtd.+*+*Author:AnshumanKhandual<anshuman.khandual@arm.com>+*/+#define DRVNAME "arm_trbe"++#define pr_fmt(fmt) DRVNAME ": " fmt++#include"coresight-trbe.h"++#define PERF_IDX2OFF(idx, buf) ((idx) % ((buf)->nr_pages << PAGE_SHIFT))++/*+*Apaddingpacketthatwillhelptheuserspacetools+*inskippingrelevantsectionsinthecapturedtrace+*datawhichcouldnotbedecoded.TRBEdoesn'tsupport+*formattingthetracedata,unlikethelegacyCoreSight+*sinksandthusweuseETEtracepacketstopadthe+*sectionsofthebuffer.+*/+#define ETE_IGNORE_PACKET 0x70++/*+*Minimumamountofmeaningfultracewillcontain:+*A-Sync,TraceInfo,TraceOn,Address,Atom.+*Thisisabout44bytesofETEtrace.Tobeon+*thesaferside,weassume64bytesistheminimum+*spacerequiredforameaningfulsession,before+*wehita"WRAP"event.+*/+#define TRBE_TRACE_MIN_BUF_SIZE 64++enumtrbe_fault_action{+TRBE_FAULT_ACT_WRAP,+TRBE_FAULT_ACT_SPURIOUS,+TRBE_FAULT_ACT_FATAL,+};++structtrbe_buf{+unsignedlongtrbe_base;+unsignedlongtrbe_limit;+unsignedlongtrbe_write;+intnr_pages;+void**pages;+boolsnapshot;+structtrbe_cpudata*cpudata;+};++structtrbe_cpudata{+booltrbe_dbm;+u64trbe_align;+intcpu;+enumcs_modemode;+structtrbe_buf*buf;+structtrbe_drvdata*drvdata;+};++structtrbe_drvdata{+structtrbe_cpudata__percpu*cpudata;+structperf_output_handle__percpu**handle;+structhlist_nodehotplug_node;+intirq;+cpumask_tsupported_cpus;+enumcpuhp_statetrbe_online;+structplatform_device*pdev;+};++staticinttrbe_alloc_node(structperf_event*event)+{+if(event->cpu==-1)+returnNUMA_NO_NODE;+returncpu_to_node(event->cpu);+}++staticvoidtrbe_drain_buffer(void)+{+asm(TSB_CSYNC);+dsb(nsh);+}++staticvoidtrbe_drain_and_disable_local(void)+{+trbe_drain_buffer();+write_sysreg_s(0,SYS_TRBLIMITR_EL1);+isb();+}++staticvoidtrbe_reset_local(void)+{+trbe_drain_and_disable_local();+write_sysreg_s(0,SYS_TRBPTR_EL1);+write_sysreg_s(0,SYS_TRBBASER_EL1);+write_sysreg_s(0,SYS_TRBSR_EL1);+}++staticvoidtrbe_stop_and_truncate_event(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);++/*+*Wecannotproceedwiththebuffercollectionandwe+*donothaveanydataforthecurrentsession.The+*etm_perfdriverexpectstocloseouttheaux_buffer+*atevent_stop().SodisabletheTRBEhereandleave+*theupdate_buffer()toreturna0size.+*/+trbe_drain_and_disable_local();+perf_aux_output_flag(handle,PERF_AUX_FLAG_TRUNCATED);+*this_cpu_ptr(buf->cpudata->drvdata->handle)=NULL;+}++/*+*TRBEBufferManagement+*+*TheTRBEbufferspansfromthebasepointertillthelimitpointer.Whenenabled,+*itstartswritingtracedatafromthewritepointeronwardtillthelimitpointer.+*Whenthewritepointerreachestheaddressjustbeforethelimitpointer,itgets+*wrappedaroundagaintothebasepointer.ThisiscalledaTRBEwrapevent,which+*generatesamaintenanceinterruptwhenoperatedinWRAPorFILLmode.Thisdriver+*usesFILLmode,wheretheTRBEstopsthetracecollectionatwrapevent.TheIRQ+*handlerupdatestheAUXbufferandre-enablestheTRBEwithupdatedWRITEand+*LIMITpointers.+*+*WraparoundwithanIRQ+*------<------<-------<-----<-----+*||+*------>------>------->----->-----+*+*+---------------+-----------------------++*|||+*+---------------+-----------------------++*BasePointerWritePointerLimitPointer+*+*ThebaseandlimitpointersalwaysneedstobePAGE_SIZEaligned.Butthewrite+*pointercanbealignedtotheimplementationdefinedTRBEtracebufferalignment+*ascapturedintrbe_cpudata->trbe_align.+*+*+*headtailwakeup+*+---------------------------------------+-----~~------+*|$$$$$$$|################|$$$$$$$$$$$$$$||+*+---------------------------------------+-----~~------+*BasePointerWritePointerLimitPointer+*+*Theperf_output_handleindices(head,tail,wakeup)aremonotonicallyincreasing+*valueswhichtracksallthedriverwritesanduserreadsfromtheperfauxiliary+*buffer.Generally[head..tail]istheareawherethedrivercanwriteintounless+*thewakeupisbehindthetail.EnabledTRBEbufferspanneedstobeadjustedand+*configureddependingontheperf_output_handleindices,sothatthedriverdoes+*notoverrideintoareasintheperfauxiliarybufferwhichisbeingoryettobe+*consumedfromtheuserspace.TheenabledTRBEbufferareaisamovingsubsetof+*theallocatedperfauxiliarybuffer.+*/+staticvoidtrbe_pad_buf(structperf_output_handle*handle,intlen)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);+u64head=PERF_IDX2OFF(handle->head,buf);++memset((void*)buf->trbe_base+head,ETE_IGNORE_PACKET,len);+if(!buf->snapshot)+perf_aux_output_skip(handle,len);+}++staticunsignedlongtrbe_snapshot_offset(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);++/*+*TheETEtracehasalignmentsynchronizationpacketsallowing+*thedecodertoresetincaseofanoverfloworcorruption.+*Sowecanusetheentirebufferforthesnapshotmode.+*/+returnbuf->nr_pages*PAGE_SIZE;+}++/*+*TRBELimitCalculation+*+*ThefollowingmarkersareusedtoillustratevariousTRBEbuffersituations.+*+*$$$$-Dataarea,unconsumedcapturedtracedata,nottobeoverridden+*####-Freearea,enabled,tracewillbewritten+*%%%%-Freearea,disabled,tracewillnotbewritten+*====-Freearea,paddedwithETE_IGNORE_PACKET,tracewillbeskipped+*/+staticunsignedlong__trbe_normal_offset(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);+structtrbe_cpudata*cpudata=buf->cpudata;+constu64bufsize=buf->nr_pages*PAGE_SIZE;+u64limit=bufsize;+u64head,tail,wakeup;++head=PERF_IDX2OFF(handle->head,buf);++/*+*head+*------->|+*|+*headTRBEaligntail+*+----|-------|---------------|-------++*|$$$$|=======|###############|$$$$$$$|+*+----|-------|---------------|-------++*trbe_basetrbe_base+nr_pages+*+*Perfauxbufferoutputheadpositioncanbemisaligneddependingon+*variousfactorsincludinguserspacereads.Incasemisaligned,head+*needstobealignedbeforeTRBEcanbeconfigured.Padthealignment+*gapwithETE_IGNORE_PACKETbytesthatwillbeignoredbyusertools+*andskipthissectionthusadvancingthehead.+*/+if(!IS_ALIGNED(head,cpudata->trbe_align)){+unsignedlongdelta=roundup(head,cpudata->trbe_align)-head;++delta=min(delta,handle->size);+trbe_pad_buf(handle,delta);+head=PERF_IDX2OFF(handle->head,buf);+}++/*+*head=tail(size=0)+*+----|-------------------------------++*|$$$$|$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$|+*+----|-------------------------------++*trbe_basetrbe_base+nr_pages+*+*Perfauxbufferdoesnothaveanyspaceforthedrivertowriteinto.+*Justcommunicatetracetruncationeventtotheuserspacebymarking+*itwithPERF_AUX_FLAG_TRUNCATED.+*/+if(!handle->size){+perf_aux_output_flag(handle,PERF_AUX_FLAG_TRUNCATED);+return0;+}++/* Compute the tail and wakeup indices now that we've aligned head */+tail=PERF_IDX2OFF(handle->head+handle->size,buf);+wakeup=PERF_IDX2OFF(handle->wakeup,buf);++/*+*LetscalculatethebufferareawhichTRBEcouldwriteinto.There+*arethreepossiblescenarioshere.Limitneedstobealignedwith+*PAGE_SIZEpertheTRBErequirement.Alwaysavoidclobberingthe+*unconsumeddata.+*+*1)head<tail+*+*headtail+*+----|-----------------------|-------++*|$$$$|#######################|$$$$$$$|+*+----|-----------------------|-------++*trbe_baselimittrbe_base+nr_pages+*+*TRBEcouldwriteinto[head..tail]area.Unlessthetailisrightat+*theendofthebuffer,neitheranwraparoundnoranIRQisexpected+*whilebeingenabled.+*+*2)head==tail+*+*head=tail(size>0)+*+----|-------------------------------++*|%%%%|###############################|+*+----|-------------------------------++*trbe_baselimit=trbe_base+nr_pages+*+*TRBEshouldjustwriteinto[head..base+nr_pages]areaeventhough+*theentirebufferisempty.Reasonbeing,whenthetracereachesthe+*endofthebuffer,itwilljustwraparoundwithanIRQgivingan+*opportunitytoreconfigurethebuffer.+*+*3)tail<head+*+*tailhead+*+----|-----------------------|-------++*|%%%%|$$$$$$$$$$$$$$$$$$$$$$$|#######|+*+----|-----------------------|-------++*trbe_baselimit=trbe_base+nr_pages+*+*TRBEshouldjustwriteinto[head..base+nr_pages]areaeventhough+*the[trbe_base..tail]isalsoempty.Reasonbeing,whenthetrace+*reachestheendofthebuffer,itwilljustwraparoundwithanIRQ+*givinganopportunitytoreconfigurethebuffer.+*/+if(head<tail)+limit=round_down(tail,PAGE_SIZE);++/*+*Wakeupmaybearbitrarilyfarintothefuture.Ifit'snotinthe+*currentgeneration,eitherwe'llwrapbeforehittingit,orit's+*inthepastandhasbeenhandledalready.+*+*Ifthere'sawakeupbeforewewrap,arrangetobewokenupbythe+*pageboundaryfollowingit.Keepthetailboundaryifthat'slower.+*+*headwakeuptail+*+----|---------------|-------|-------++*|$$$$|###############|%%%%%%%|$$$$$$$|+*+----|---------------|-------|-------++*trbe_baselimittrbe_base+nr_pages+*/+if(handle->wakeup<(handle->head+handle->size)&&head<=wakeup)+limit=min(limit,round_up(wakeup,PAGE_SIZE));++/*+*Therearetwosituationwhenthiscanhappeni.elimitisbefore+*theheadandhenceTRBEcannotbeconfigured.+*+*1)head<tail(aligneddownwithPAGE_SIZE)andalsotheyareboth+*withinthesamePAGEsizerange.+*+*PAGE_SIZE+*|----------------------|+*+*limitheadtail+*+------------|------|--------|-------++*|$$$$$$$$$$$$$$$$$$$|========|$$$$$$$|+*+------------|------|--------|-------++*trbe_basetrbe_base+nr_pages+*+*2)head<wakeup(alignedupwithPAGE_SIZE)<tailandalsoboth+*headandwakeuparewithinsamePAGEsizerange.+*+*PAGE_SIZE+*|----------------------|+*+*limitheadwakeuptail+*+----|------|-------|--------|-------++*|$$$$$$$$$$$|=======|========|$$$$$$$|+*+----|------|-------|--------|-------++*trbe_basetrbe_base+nr_pages+*/+if(limit>head)+returnlimit;++trbe_pad_buf(handle,handle->size);+perf_aux_output_flag(handle,PERF_AUX_FLAG_TRUNCATED);+return0;+}++staticunsignedlongtrbe_normal_offset(structperf_output_handle*handle)+{+structtrbe_buf*buf=perf_get_aux(handle);+u64limit=__trbe_normal_offset(handle);+u64head=PERF_IDX2OFF(handle->head,buf);++/*+*Iftheheadistooclosetothelimitandwedon't+*havespaceforameaningfulrun,weratherpadit+*andstartfresh.+*/+if(limit&&(limit-head<TRBE_TRACE_MIN_BUF_SIZE)){+trbe_pad_buf(handle,limit-head);+limit=__trbe_normal_offset(handle);+}+returnlimit;+}++staticunsignedlongcompute_trbe_buffer_limit(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);+unsignedlongoffset;++if(buf->snapshot)+offset=trbe_snapshot_offset(handle);+else+offset=trbe_normal_offset(handle);+returnbuf->trbe_base+offset;+}++staticvoidclr_trbe_status(void)+{+u64trbsr=read_sysreg_s(SYS_TRBSR_EL1);++WARN_ON(is_trbe_enabled());+trbsr&=~TRBSR_IRQ;+trbsr&=~TRBSR_TRG;+trbsr&=~TRBSR_WRAP;+trbsr&=~(TRBSR_EC_MASK<<TRBSR_EC_SHIFT);+trbsr&=~(TRBSR_BSC_MASK<<TRBSR_BSC_SHIFT);+trbsr&=~TRBSR_STOP;+write_sysreg_s(trbsr,SYS_TRBSR_EL1);+}++staticvoidset_trbe_limit_pointer_enabled(unsignedlongaddr)+{+u64trblimitr=read_sysreg_s(SYS_TRBLIMITR_EL1);++WARN_ON(!IS_ALIGNED(addr,(1UL<<TRBLIMITR_LIMIT_SHIFT)));+WARN_ON(!IS_ALIGNED(addr,PAGE_SIZE));++trblimitr&=~TRBLIMITR_NVM;+trblimitr&=~(TRBLIMITR_FILL_MODE_MASK<<TRBLIMITR_FILL_MODE_SHIFT);+trblimitr&=~(TRBLIMITR_TRIG_MODE_MASK<<TRBLIMITR_TRIG_MODE_SHIFT);+trblimitr&=~(TRBLIMITR_LIMIT_MASK<<TRBLIMITR_LIMIT_SHIFT);++/*+*Filltracebuffermodeisusedherewhileconfiguringthe+*TRBEfortracecapture.Inthisparticularmode,thetrace+*collectionisstoppedandamaintenanceinterruptisraised+*whenthecurrentwritepointerwraps.Thispauseintrace+*collectiongivesthesoftwareanopportunitytocapturethe+*tracedataintheinterrupthandler,beforereconfiguring+*theTRBE.+*/+trblimitr|=(TRBE_FILL_MODE_FILL&TRBLIMITR_FILL_MODE_MASK)<<TRBLIMITR_FILL_MODE_SHIFT;++/*+*TriggermodeisnotusedherewhileconfiguringtheTRBEfor+*thetracecapture.Hencejustkeepthisintheignoremode.+*/+trblimitr|=(TRBE_TRIG_MODE_IGNORE&TRBLIMITR_TRIG_MODE_MASK)<<TRBLIMITR_TRIG_MODE_SHIFT;+trblimitr|=(addr&PAGE_MASK);++trblimitr|=TRBLIMITR_ENABLE;+write_sysreg_s(trblimitr,SYS_TRBLIMITR_EL1);+}++staticvoidtrbe_enable_hw(structtrbe_buf*buf)+{+WARN_ON(buf->trbe_write<buf->trbe_base);+WARN_ON(buf->trbe_write>=buf->trbe_limit);+set_trbe_disabled();+isb();+clr_trbe_status();+set_trbe_base_pointer(buf->trbe_base);+set_trbe_write_pointer(buf->trbe_write);++/*+*Synchronizealltheregisterupdates+*tillnowbeforeenablingtheTRBE.+*/+isb();+set_trbe_limit_pointer_enabled(buf->trbe_limit);++/* Synchronize the TRBE enable event */+isb();+}++staticvoid*arm_trbe_alloc_buffer(structcoresight_device*csdev,+structperf_event*event,void**pages,+intnr_pages,boolsnapshot)+{+structtrbe_buf*buf;+structpage**pglist;+inti;++/*+*TRBELIMITandTRBEWRITEpointersmustbepagealigned.Butwith+*justasinglepage,thereisnotmuchroomleftwhilewritinginto+*apartiallyfilledTRBEbuffer.Hencerestricttheminimumbuffer+*sizeastwopages.+*/+if(nr_pages<2)+returnNULL;++buf=kzalloc_node(sizeof(*buf),GFP_KERNEL,trbe_alloc_node(event));+if(IS_ERR(buf))+returnERR_PTR(-ENOMEM);++pglist=kcalloc(nr_pages,sizeof(*pglist),GFP_KERNEL);+if(IS_ERR(pglist)){+kfree(buf);+returnERR_PTR(-ENOMEM);+}++for(i=0;i<nr_pages;i++)+pglist[i]=virt_to_page(pages[i]);++buf->trbe_base=(unsignedlong)vmap(pglist,nr_pages,VM_MAP,PAGE_KERNEL);+if(IS_ERR((void*)buf->trbe_base)){+kfree(pglist);+kfree(buf);+returnERR_PTR(buf->trbe_base);+}+buf->trbe_limit=buf->trbe_base+nr_pages*PAGE_SIZE;+buf->trbe_write=buf->trbe_base;+buf->snapshot=snapshot;+buf->nr_pages=nr_pages;+buf->pages=pages;+kfree(pglist);+returnbuf;+}++voidarm_trbe_free_buffer(void*config)+{+structtrbe_buf*buf=config;++vunmap((void*)buf->trbe_base);+kfree(buf);+}++staticunsignedlongarm_trbe_update_buffer(structcoresight_device*csdev,+structperf_output_handle*handle,+void*config)+{+structtrbe_drvdata*drvdata=dev_get_drvdata(csdev->dev.parent);+structtrbe_cpudata*cpudata=dev_get_drvdata(&csdev->dev);+structtrbe_buf*buf=config;+unsignedlongsize,offset;++WARN_ON(buf->cpudata!=cpudata);+WARN_ON(cpudata->cpu!=smp_processor_id());+WARN_ON(cpudata->drvdata!=drvdata);+if(cpudata->mode!=CS_MODE_PERF)+return-EINVAL;++/*+*IftheTRBEwasdisabledduetolackofspaceintheAUXbufferora+*spuriousfault,thedriverleavesitdisabled,truncatingthebuffer.+*Sincetheetm_perfdriverexpectstocloseouttheAUXbuffer,the+*driverskipsit.Thus,justpassin0sizeheretoindicatethatthe+*bufferwastruncated.+*/+if(!is_trbe_enabled())+return0;+/*+*perfhandlestructureneedstobesharedwiththeTRBEIRQhandlerfor+*capturingtracedataandrestartingthehandle.Thereisaprobability+*ofanundefinedreferencebasedcrashwhenetmeventisbeingstopped+*whileaTRBEIRQalsogettingprocessed.Thishappensduetherelease+*ofperfhandleviaperf_aux_output_end()inetm_event_stop().Stopping+*theTRBEherewillensurethatnoIRQcouldbegeneratedwhentheperf+*handlegetsfreedinetm_event_stop().+*/+trbe_drain_and_disable_local();+offset=get_trbe_write_pointer()-get_trbe_base_pointer();+size=offset-PERF_IDX2OFF(handle->head,buf);+if(buf->snapshot)+handle->head+=size;+returnsize;+}++staticintarm_trbe_enable(structcoresight_device*csdev,u32mode,void*data)+{+structtrbe_drvdata*drvdata=dev_get_drvdata(csdev->dev.parent);+structtrbe_cpudata*cpudata=dev_get_drvdata(&csdev->dev);+structperf_output_handle*handle=data;+structtrbe_buf*buf=etm_perf_sink_config(handle);++WARN_ON(cpudata->cpu!=smp_processor_id());+WARN_ON(cpudata->drvdata!=drvdata);+if(mode!=CS_MODE_PERF)+return-EINVAL;++*this_cpu_ptr(drvdata->handle)=handle;+cpudata->buf=buf;+cpudata->mode=mode;+buf->cpudata=cpudata;+buf->trbe_limit=compute_trbe_buffer_limit(handle);+buf->trbe_write=buf->trbe_base+PERF_IDX2OFF(handle->head,buf);+if(buf->trbe_limit==buf->trbe_base){+trbe_stop_and_truncate_event(handle);+return0;+}+trbe_enable_hw(buf);+return0;+}++staticintarm_trbe_disable(structcoresight_device*csdev)+{+structtrbe_drvdata*drvdata=dev_get_drvdata(csdev->dev.parent);+structtrbe_cpudata*cpudata=dev_get_drvdata(&csdev->dev);+structtrbe_buf*buf=cpudata->buf;++WARN_ON(buf->cpudata!=cpudata);+WARN_ON(cpudata->cpu!=smp_processor_id());+WARN_ON(cpudata->drvdata!=drvdata);+if(cpudata->mode!=CS_MODE_PERF)+return-EINVAL;++trbe_drain_and_disable_local();+buf->cpudata=NULL;+cpudata->buf=NULL;+cpudata->mode=CS_MODE_DISABLED;+return0;+}++staticvoidtrbe_handle_spurious(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);++buf->trbe_limit=compute_trbe_buffer_limit(handle);+buf->trbe_write=buf->trbe_base+PERF_IDX2OFF(handle->head,buf);+if(buf->trbe_limit==buf->trbe_base){+trbe_drain_and_disable_local();+return;+}+trbe_enable_hw(buf);+}++staticvoidtrbe_handle_overflow(structperf_output_handle*handle)+{+structperf_event*event=handle->event;+structtrbe_buf*buf=etm_perf_sink_config(handle);+unsignedlongoffset,size;+structetm_event_data*event_data;++offset=get_trbe_limit_pointer()-get_trbe_base_pointer();+size=offset-PERF_IDX2OFF(handle->head,buf);+if(buf->snapshot)+handle->head=offset;+perf_aux_output_end(handle,size);++event_data=perf_aux_output_begin(handle,event);+if(!event_data){+trbe_drain_and_disable_local();+*this_cpu_ptr(buf->cpudata->drvdata->handle)=NULL;+return;+}+buf->trbe_limit=compute_trbe_buffer_limit(handle);+buf->trbe_write=buf->trbe_base+PERF_IDX2OFF(handle->head,buf);+if(buf->trbe_limit==buf->trbe_base){+trbe_stop_and_truncate_event(handle);+return;+}+*this_cpu_ptr(buf->cpudata->drvdata->handle)=handle;+trbe_enable_hw(buf);+}++staticboolis_perf_trbe(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);+structtrbe_cpudata*cpudata=buf->cpudata;+structtrbe_drvdata*drvdata=cpudata->drvdata;+intcpu=smp_processor_id();++WARN_ON(buf->trbe_base!=get_trbe_base_pointer());+WARN_ON(buf->trbe_limit!=get_trbe_limit_pointer());++if(cpudata->mode!=CS_MODE_PERF)+returnfalse;++if(cpudata->cpu!=cpu)+returnfalse;++if(!cpumask_test_cpu(cpu,&drvdata->supported_cpus))+returnfalse;++returntrue;+}++staticenumtrbe_fault_actiontrbe_get_fault_act(structperf_output_handle*handle)+{+u64trbsr=read_sysreg_s(SYS_TRBSR_EL1);+intec=get_trbe_ec(trbsr);+intbsc=get_trbe_bsc(trbsr);++WARN_ON(is_trbe_running(trbsr));+if(is_trbe_trg(trbsr)||is_trbe_abort(trbsr))+returnTRBE_FAULT_ACT_FATAL;++if((ec==TRBE_EC_STAGE1_ABORT)||(ec==TRBE_EC_STAGE2_ABORT))+returnTRBE_FAULT_ACT_FATAL;++if(is_trbe_wrap(trbsr)&&(ec==TRBE_EC_OTHERS)&&(bsc==TRBE_BSC_FILLED)){+if(get_trbe_write_pointer()==get_trbe_base_pointer())+returnTRBE_FAULT_ACT_WRAP;+}+returnTRBE_FAULT_ACT_SPURIOUS;+}++staticirqreturn_tarm_trbe_irq_handler(intirq,void*dev)+{+structperf_output_handle**handle_ptr=dev;+structperf_output_handle*handle=*handle_ptr;+enumtrbe_fault_actionact;++WARN_ON(!is_trbe_irq(read_sysreg_s(SYS_TRBSR_EL1)));+clr_trbe_irq();++/*+*EnsurethetraceisvisibletotheCPUsand+*anyexternalabortshavebeenresolved.+*/+trbe_drain_buffer();+isb();++if(!perf_get_aux(handle))+returnIRQ_NONE;++if(!is_perf_trbe(handle))+returnIRQ_NONE;++irq_work_run();
There is a comment in the SPE driver about this. Since this driver closely
follows that implementation it would be nice to have the comments as well.
Otherwise the reader has to constantly go back to the original driver.
Sure, will add the following comment before irq_work_run().
/*
* Ensure perf callbacks have completed, which may disable the
* profiling buffer in response to a TRUNCATION flag.
*/
Where was the memory for cpudata allocated? As far as I can tell, at this time
it is just a pointer that was not allocated and as such it should be NULL.
cpudata gets allocated in arm_trbe_probe_coresight() just before calling
individual CPU based probes i.e arm_trbe_probe_coresight_cpu() directly
and via smp_call_function_many().
arm_trbe_device_probe()
arm_trbe_probe_coresight()
arm_trbe_probe_coresight_cpu()
From: Suzuki K Poulose <suzuki.poulose@arm.com> Date: 2021-02-10 12:37:54
Hi Rob
On 2/9/21 7:00 PM, Rob Herring wrote:
On Wed, Jan 27, 2021 at 02:25:30PM +0530, Anshuman Khandual wrote:
quoted
From: Suzuki K Poulose <suzuki.poulose@arm.com>
Document the device tree bindings for Embedded Trace Extensions.
ETE can be connected to legacy coresight components and thus
could optionally contain a connection graph as described by
the CoreSight bindings.
Cc: devicetree@vger.kernel.org
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Rob Herring <robh@kernel.org>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
Changes in V3:
- Fixed all DT yaml semantics problems
Documentation/devicetree/bindings/arm/ete.yaml | 74 ++++++++++++++++++++++++++
1 file changed, 74 insertions(+)
create mode 100644 Documentation/devicetree/bindings/arm/ete.yaml
@@ -0,0 +1,74 @@+# SPDX-License-Identifier: GPL-2.0-only or BSD-2-Clause+# Copyright 2021, Arm Ltd+%YAML1.2+---+$id:"http://devicetree.org/schemas/arm/ete.yaml#"+$schema:"http://devicetree.org/meta-schemas/core.yaml#"++title:ARM Embedded Trace Extensions++maintainers:+-Suzuki K Poulose <suzuki.poulose@arm.com>+-Mathieu Poirier <mathieu.poirier@linaro.org>++description:|+Arm Embedded Trace Extension(ETE) is a per CPU trace component that+allows tracing the CPU execution. It overlaps with the CoreSight ETMv4+architecture and has extended support for future architecture changes.+The trace generated by the ETE could be stored via legacy CoreSight+components (e.g, TMC-ETR) or other means (e.g, using a per CPU buffer+Arm Trace Buffer Extension (TRBE)). Since the ETE can be connected to+legacy CoreSight components, a node must be listed per instance, along+with any optional connection graph as per the coresight bindings.+See bindings/arm/coresight.txt.++properties:+$nodename:+pattern:"^ete([0-9a-f]+)$"+compatible:+items:+-const:arm,embedded-trace-extension++cpu:
We've already established 'cpus' for this purpose.
So, just to confirm again :
The CoreSight graph bindings expect the input ports and output ports
grouped under in-ports{} and out-ports{} respectively to avoid having
to specify the direction of the ports in the individual "port" nodes.
i.e
in-ports {
property: ports
OR
property: port
required:
OneOf:
ports
port
}
out-ports {
# same as above
}
So thats why I added out-ports as a new object, where the ports/port
could be a child node.
Ideally the definition of out-ports /in-ports should go to a common schema
for CoreSight bindings, when we move to Yaml for the existing bindings,
which will follow in a separate series, later.
quoted
+ description: |
+ Output connections from the ETE to legacy CoreSight trace bus.
+ properties:
+ port:
+ $ref: /schemas/graph.yaml#/properties/port
Actually, if only 1 port ever, you can drop 'out-ports' and just have
'port'. Not sure though if the coresight stuff depends on 'out-ports'.
On Wed, Feb 10, 2021 at 09:42:29AM +0530, Anshuman Khandual wrote:
On 2/9/21 11:09 PM, Mathieu Poirier wrote:
quoted
On Fri, Feb 05, 2021 at 10:53:30AM -0700, Mathieu Poirier wrote:
quoted
On Wed, Jan 27, 2021 at 02:25:35PM +0530, Anshuman Khandual wrote:
quoted
Trace Buffer Extension (TRBE) implements a trace buffer per CPU which is
accessible via the system registers. The TRBE supports different addressing
modes including CPU virtual address and buffer modes including the circular
buffer mode. The TRBE buffer is addressed by a base pointer (TRBBASER_EL1),
an write pointer (TRBPTR_EL1) and a limit pointer (TRBLIMITR_EL1). But the
access to the trace buffer could be prohibited by a higher exception level
(EL3 or EL2), indicated by TRBIDR_EL1.P. The TRBE can also generate a CPU
private interrupt (PPI) on address translation errors and when the buffer
is full. Overall implementation here is inspired from the Arm SPE driver.
I got this message when applying the patch:
Applying: coresight: sink: Add TRBE driver
.git/rebase-apply/patch:76: new blank line at EOF.
+
warning: 1 line adds whitespace errors.
quoted
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
Changes in V3:
- Added new DT bindings document TRBE.yaml
- Changed TRBLIMITR_TRIG_MODE_SHIFT from 2 to 3
- Dropped isb() from trbe_reset_local()
- Dropped gap between (void *) and buf->trbe_base
- Changed 'int' to 'unsigned int' in is_trbe_available()
- Dropped unused function set_trbe_running(), set_trbe_virtual_mode(),
set_trbe_enabled() and set_trbe_limit_pointer()
- Changed get_trbe_flag_update(), is_trbe_programmable() and
get_trbe_address_align() to accept TRBIDR value
- Changed is_trbe_running(), is_trbe_abort(), is_trbe_wrap(), is_trbe_trg(),
is_trbe_irq(), get_trbe_bsc() and get_trbe_ec() to accept TRBSR value
- Dropped snapshot mode condition in arm_trbe_alloc_buffer()
- Exit arm_trbe_init() when arm64_kernel_unmapped_at_el0() is enabled
- Compute trbe_limit before trbe_write to get the updated handle
- Added trbe_stop_and_truncate_event()
- Dropped trbe_handle_fatal()
Documentation/trace/coresight/coresight-trbe.rst | 39 +
arch/arm64/include/asm/sysreg.h | 1 +
drivers/hwtracing/coresight/Kconfig | 11 +
drivers/hwtracing/coresight/Makefile | 1 +
drivers/hwtracing/coresight/coresight-trbe.c | 1023 ++++++++++++++++++++++
drivers/hwtracing/coresight/coresight-trbe.h | 160 ++++
6 files changed, 1235 insertions(+)
create mode 100644 Documentation/trace/coresight/coresight-trbe.rst
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.c
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.h
[...]
quoted
quoted
quoted
+
+static irqreturn_t arm_trbe_irq_handler(int irq, void *dev)
+{
+ struct perf_output_handle **handle_ptr = dev;
+ struct perf_output_handle *handle = *handle_ptr;
+ enum trbe_fault_action act;
+
+ WARN_ON(!is_trbe_irq(read_sysreg_s(SYS_TRBSR_EL1)));
+ clr_trbe_irq();
+
+ /*
+ * Ensure the trace is visible to the CPUs and
+ * any external aborts have been resolved.
+ */
+ trbe_drain_buffer();
+ isb();
+
+ if (!perf_get_aux(handle))
+ return IRQ_NONE;
+
+ if (!is_perf_trbe(handle))
+ return IRQ_NONE;
+
+ irq_work_run();
There is a comment in the SPE driver about this. Since this driver closely
follows that implementation it would be nice to have the comments as well.
Otherwise the reader has to constantly go back to the original driver.
Sure, will add the following comment before irq_work_run().
/*
* Ensure perf callbacks have completed, which may disable the
* profiling buffer in response to a TRUNCATION flag.
*/
Where was the memory for cpudata allocated? As far as I can tell, at this time
it is just a pointer that was not allocated and as such it should be NULL.
cpudata gets allocated in arm_trbe_probe_coresight() just before calling
individual CPU based probes i.e arm_trbe_probe_coresight_cpu() directly
and via smp_call_function_many().
arm_trbe_device_probe()
arm_trbe_probe_coresight()
arm_trbe_probe_coresight_cpu()
Ah yes, my apologies here. Looking at the code I realised I skipped
arm_trbe_probe_coresight() and went straight to arm_trbe_probe_coresight_cpu().
No wonder things didn't make sense. I will take another look at this function.
No need to do this since devm_kzalloc() was used above.
Suzuki had pointed out these issues, have already incorporated them i.e
dropped kfree() here.
To avoid getting tunel vision I don't look at other comments before reviewing a
patchset. As such it is possible to get redundant comments.
More to come shortly.
quoted
quoted
quoted
+ return -ENOMEM;
Why not using the error from coresight_get_platform_data() instead of
masking it?
On Wed, Jan 27, 2021 at 02:25:35PM +0530, Anshuman Khandual wrote:
Trace Buffer Extension (TRBE) implements a trace buffer per CPU which is
accessible via the system registers. The TRBE supports different addressing
modes including CPU virtual address and buffer modes including the circular
buffer mode. The TRBE buffer is addressed by a base pointer (TRBBASER_EL1),
an write pointer (TRBPTR_EL1) and a limit pointer (TRBLIMITR_EL1). But the
access to the trace buffer could be prohibited by a higher exception level
(EL3 or EL2), indicated by TRBIDR_EL1.P. The TRBE can also generate a CPU
private interrupt (PPI) on address translation errors and when the buffer
is full. Overall implementation here is inspired from the Arm SPE driver.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
Changes in V3:
- Added new DT bindings document TRBE.yaml
- Changed TRBLIMITR_TRIG_MODE_SHIFT from 2 to 3
- Dropped isb() from trbe_reset_local()
- Dropped gap between (void *) and buf->trbe_base
- Changed 'int' to 'unsigned int' in is_trbe_available()
- Dropped unused function set_trbe_running(), set_trbe_virtual_mode(),
set_trbe_enabled() and set_trbe_limit_pointer()
- Changed get_trbe_flag_update(), is_trbe_programmable() and
get_trbe_address_align() to accept TRBIDR value
- Changed is_trbe_running(), is_trbe_abort(), is_trbe_wrap(), is_trbe_trg(),
is_trbe_irq(), get_trbe_bsc() and get_trbe_ec() to accept TRBSR value
- Dropped snapshot mode condition in arm_trbe_alloc_buffer()
- Exit arm_trbe_init() when arm64_kernel_unmapped_at_el0() is enabled
- Compute trbe_limit before trbe_write to get the updated handle
- Added trbe_stop_and_truncate_event()
- Dropped trbe_handle_fatal()
Documentation/trace/coresight/coresight-trbe.rst | 39 +
arch/arm64/include/asm/sysreg.h | 1 +
drivers/hwtracing/coresight/Kconfig | 11 +
drivers/hwtracing/coresight/Makefile | 1 +
drivers/hwtracing/coresight/coresight-trbe.c | 1023 ++++++++++++++++++++++
drivers/hwtracing/coresight/coresight-trbe.h | 160 ++++
6 files changed, 1235 insertions(+)
create mode 100644 Documentation/trace/coresight/coresight-trbe.rst
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.c
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.h
+
There is already a check for this in arm_trbe_probe_coresight(), we couldn't be
here if there was a problem with the allocation.
+
+ if (trbe_csdev)
+ return;
Now that's a reason to have a WARN_ON(). If we are probing and a sink is
already present in this cpu's slot, something went seriously wrong and we should
be clear about it.
+
+ cpudata->cpu = smp_processor_id();
+ cpudata->drvdata = drvdata;
+ dev = &cpudata->drvdata->pdev->dev;
+
+ if (!is_trbe_available()) {
+ pr_err("TRBE is not implemented on cpu %d\n", cpudata->cpu);
+ goto cpu_clear;
+ }
+
+ if (!is_trbe_programmable(trbidr)) {
+ pr_err("TRBE is owned in higher exception level on cpu %d\n", cpudata->cpu);
+ goto cpu_clear;
+ }
+ desc.name = devm_kasprintf(dev, GFP_KERNEL, "%s%d", DRVNAME, smp_processor_id());
We will end up with "arm_trbe0", "arm_trbe1" and so on in sysfs... Is the
"arm_" part absolutely needed? I think this should be like what we do for etmv3
and etmv4 where only "etmX" shows up in sysfs.
Here coresight_unregister() should be called. The other option is to call
coresight_register() when everything else is known to be fine, which is the
favoured approach.
In what scenario do you see not having a trbe_csdev and still needing to disable
IRQs for the HW? If there is a such a case then a few lines of comment is
needed.
Theoretically this code shouldn't run when the TRBE is enabled, because the CS
core will prevent that from happening. As sush disabling interrupts after
coresight_unregister() has been called and setting cpudata->drvdata to NULL
should be fine. But from an outsider's point of view it will look very bizarre.
Either write a comment to explain all that or call the above two before doing
the cleanup.
+}
+
+static int arm_trbe_probe_coresight(struct trbe_drvdata *drvdata)
+{
+ drvdata->cpudata = alloc_percpu(typeof(*drvdata->cpudata));
+ if (IS_ERR(drvdata->cpudata))
+ return PTR_ERR(drvdata->cpudata);
As far as I can tell alloc_percpu() returns NULL on failure and nothing else.
The above two calls look racy to me. The executing process could be moved to
another CPU between the call to arm_trbe_probe_coresight_cpu() and
smp_call_function_many(), which would prevent the initialisation of the TRBE on
the new CPU to be done. I suggest using a for_each_cpu() loop where
smp_call_function_single() would be used. That way we are guaranteed all the
TRBEs will be initialised.
On Wed, Jan 27, 2021 at 02:25:35PM +0530, Anshuman Khandual wrote:
quoted hunk
Trace Buffer Extension (TRBE) implements a trace buffer per CPU which is
accessible via the system registers. The TRBE supports different addressing
modes including CPU virtual address and buffer modes including the circular
buffer mode. The TRBE buffer is addressed by a base pointer (TRBBASER_EL1),
an write pointer (TRBPTR_EL1) and a limit pointer (TRBLIMITR_EL1). But the
access to the trace buffer could be prohibited by a higher exception level
(EL3 or EL2), indicated by TRBIDR_EL1.P. The TRBE can also generate a CPU
private interrupt (PPI) on address translation errors and when the buffer
is full. Overall implementation here is inspired from the Arm SPE driver.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
Changes in V3:
- Added new DT bindings document TRBE.yaml
- Changed TRBLIMITR_TRIG_MODE_SHIFT from 2 to 3
- Dropped isb() from trbe_reset_local()
- Dropped gap between (void *) and buf->trbe_base
- Changed 'int' to 'unsigned int' in is_trbe_available()
- Dropped unused function set_trbe_running(), set_trbe_virtual_mode(),
set_trbe_enabled() and set_trbe_limit_pointer()
- Changed get_trbe_flag_update(), is_trbe_programmable() and
get_trbe_address_align() to accept TRBIDR value
- Changed is_trbe_running(), is_trbe_abort(), is_trbe_wrap(), is_trbe_trg(),
is_trbe_irq(), get_trbe_bsc() and get_trbe_ec() to accept TRBSR value
- Dropped snapshot mode condition in arm_trbe_alloc_buffer()
- Exit arm_trbe_init() when arm64_kernel_unmapped_at_el0() is enabled
- Compute trbe_limit before trbe_write to get the updated handle
- Added trbe_stop_and_truncate_event()
- Dropped trbe_handle_fatal()
Documentation/trace/coresight/coresight-trbe.rst | 39 +
arch/arm64/include/asm/sysreg.h | 1 +
drivers/hwtracing/coresight/Kconfig | 11 +
drivers/hwtracing/coresight/Makefile | 1 +
drivers/hwtracing/coresight/coresight-trbe.c | 1023 ++++++++++++++++++++++
drivers/hwtracing/coresight/coresight-trbe.h | 160 ++++
6 files changed, 1235 insertions(+)
create mode 100644 Documentation/trace/coresight/coresight-trbe.rst
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.c
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.h
@@ -0,0 +1,39 @@+.. SPDX-License-Identifier: GPL-2.0++==============================+Trace Buffer Extension (TRBE).+==============================++:Author: Anshuman Khandual <anshuman.khandual@arm.com>+:Date: November 2020++Hardware Description+--------------------++Trace Buffer Extension (TRBE) is a percpu hardware which captures in system+memory, CPU traces generated from a corresponding percpu tracing unit. This+gets plugged in as a coresight sink device because the corresponding trace+genarators (ETE), are plugged in as source device.++The TRBE is not compliant to CoreSight architecture specifications, but is+driven via the CoreSight driver framework to support the ETE (which is+CoreSight compliant) integration.++Sysfs files and directories+---------------------------++The TRBE devices appear on the existing coresight bus alongside the other+coresight devices::++ >$ ls /sys/bus/coresight/devices+ trbe0 trbe1 trbe2 trbe3++The ``trbe<N>`` named TRBEs are associated with a CPU.::++ >$ ls /sys/bus/coresight/devices/trbe0/+ align dbm++*Key file items are:-*+*``align``: TRBE write pointer alignment+*``dbm``: TRBE updates memory with access and dirty flags+
+
+ if (cpuhp_state_add_instance(trbe_online, &drvdata->hotplug_node))
+ return -EINVAL;
Wrong error code. Even if the callbacks aren't called it might be a good idea to
properly cleanup the with cpuhp_remove_multi_state().
More to come tomorrow.
Thanks,
Mathieu
On Wed, Jan 27, 2021 at 02:25:35PM +0530, Anshuman Khandual wrote:
quoted
Trace Buffer Extension (TRBE) implements a trace buffer per CPU which is
accessible via the system registers. The TRBE supports different addressing
modes including CPU virtual address and buffer modes including the circular
buffer mode. The TRBE buffer is addressed by a base pointer (TRBBASER_EL1),
an write pointer (TRBPTR_EL1) and a limit pointer (TRBLIMITR_EL1). But the
access to the trace buffer could be prohibited by a higher exception level
(EL3 or EL2), indicated by TRBIDR_EL1.P. The TRBE can also generate a CPU
private interrupt (PPI) on address translation errors and when the buffer
is full. Overall implementation here is inspired from the Arm SPE driver.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
Changes in V3:
- Added new DT bindings document TRBE.yaml
- Changed TRBLIMITR_TRIG_MODE_SHIFT from 2 to 3
- Dropped isb() from trbe_reset_local()
- Dropped gap between (void *) and buf->trbe_base
- Changed 'int' to 'unsigned int' in is_trbe_available()
- Dropped unused function set_trbe_running(), set_trbe_virtual_mode(),
set_trbe_enabled() and set_trbe_limit_pointer()
- Changed get_trbe_flag_update(), is_trbe_programmable() and
get_trbe_address_align() to accept TRBIDR value
- Changed is_trbe_running(), is_trbe_abort(), is_trbe_wrap(), is_trbe_trg(),
is_trbe_irq(), get_trbe_bsc() and get_trbe_ec() to accept TRBSR value
- Dropped snapshot mode condition in arm_trbe_alloc_buffer()
- Exit arm_trbe_init() when arm64_kernel_unmapped_at_el0() is enabled
- Compute trbe_limit before trbe_write to get the updated handle
- Added trbe_stop_and_truncate_event()
- Dropped trbe_handle_fatal()
Documentation/trace/coresight/coresight-trbe.rst | 39 +
arch/arm64/include/asm/sysreg.h | 1 +
drivers/hwtracing/coresight/Kconfig | 11 +
drivers/hwtracing/coresight/Makefile | 1 +
drivers/hwtracing/coresight/coresight-trbe.c | 1023 ++++++++++++++++++++++
drivers/hwtracing/coresight/coresight-trbe.h | 160 ++++
6 files changed, 1235 insertions(+)
create mode 100644 Documentation/trace/coresight/coresight-trbe.rst
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.c
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.h
@@ -0,0 +1,39 @@+.. SPDX-License-Identifier: GPL-2.0++==============================+Trace Buffer Extension (TRBE).+==============================++:Author: Anshuman Khandual <anshuman.khandual@arm.com>+:Date: November 2020++Hardware Description+--------------------++Trace Buffer Extension (TRBE) is a percpu hardware which captures in system+memory, CPU traces generated from a corresponding percpu tracing unit. This+gets plugged in as a coresight sink device because the corresponding trace+genarators (ETE), are plugged in as source device.++The TRBE is not compliant to CoreSight architecture specifications, but is+driven via the CoreSight driver framework to support the ETE (which is+CoreSight compliant) integration.++Sysfs files and directories+---------------------------++The TRBE devices appear on the existing coresight bus alongside the other+coresight devices::++ >$ ls /sys/bus/coresight/devices+ trbe0 trbe1 trbe2 trbe3++The ``trbe<N>`` named TRBEs are associated with a CPU.::++ >$ ls /sys/bus/coresight/devices/trbe0/+ align dbm++*Key file items are:-*+*``align``: TRBE write pointer alignment+*``dbm``: TRBE updates memory with access and dirty flags+
@@ -0,0 +1,1023 @@+// SPDX-License-Identifier: GPL-2.0+/*+*ThisdriverenablesTraceBufferExtension(TRBE)asaper-cpucoresight+*sinkdevicecouldthenpairwithanappropriateper-cpucoresightsource+*device(ETE)thusgeneratingrequiredtracedata.Tracecanbeenabled+*viatheperfframework.+*+*Copyright(C)2020ARMLtd.+*+*Author:AnshumanKhandual<anshuman.khandual@arm.com>+*/+#define DRVNAME "arm_trbe"++#define pr_fmt(fmt) DRVNAME ": " fmt++#include"coresight-trbe.h"++#define PERF_IDX2OFF(idx, buf) ((idx) % ((buf)->nr_pages << PAGE_SHIFT))++/*+*Apaddingpacketthatwillhelptheuserspacetools+*inskippingrelevantsectionsinthecapturedtrace+*datawhichcouldnotbedecoded.TRBEdoesn'tsupport+*formattingthetracedata,unlikethelegacyCoreSight+*sinksandthusweuseETEtracepacketstopadthe+*sectionsofthebuffer.+*/+#define ETE_IGNORE_PACKET 0x70++/*+*Minimumamountofmeaningfultracewillcontain:+*A-Sync,TraceInfo,TraceOn,Address,Atom.+*Thisisabout44bytesofETEtrace.Tobeon+*thesaferside,weassume64bytesistheminimum+*spacerequiredforameaningfulsession,before+*wehita"WRAP"event.+*/+#define TRBE_TRACE_MIN_BUF_SIZE 64++enumtrbe_fault_action{+TRBE_FAULT_ACT_WRAP,+TRBE_FAULT_ACT_SPURIOUS,+TRBE_FAULT_ACT_FATAL,+};++structtrbe_buf{+unsignedlongtrbe_base;+unsignedlongtrbe_limit;+unsignedlongtrbe_write;+intnr_pages;+void**pages;+boolsnapshot;+structtrbe_cpudata*cpudata;+};++structtrbe_cpudata{+booltrbe_dbm;+u64trbe_align;+intcpu;+enumcs_modemode;+structtrbe_buf*buf;+structtrbe_drvdata*drvdata;+};++structtrbe_drvdata{+structtrbe_cpudata__percpu*cpudata;+structperf_output_handle__percpu**handle;+structhlist_nodehotplug_node;+intirq;+cpumask_tsupported_cpus;+enumcpuhp_statetrbe_online;+structplatform_device*pdev;+};++staticinttrbe_alloc_node(structperf_event*event)+{+if(event->cpu==-1)+returnNUMA_NO_NODE;+returncpu_to_node(event->cpu);+}++staticvoidtrbe_drain_buffer(void)+{+asm(TSB_CSYNC);+dsb(nsh);+}++staticvoidtrbe_drain_and_disable_local(void)+{+trbe_drain_buffer();+write_sysreg_s(0,SYS_TRBLIMITR_EL1);+isb();+}++staticvoidtrbe_reset_local(void)+{+trbe_drain_and_disable_local();+write_sysreg_s(0,SYS_TRBPTR_EL1);+write_sysreg_s(0,SYS_TRBBASER_EL1);+write_sysreg_s(0,SYS_TRBSR_EL1);+}++staticvoidtrbe_stop_and_truncate_event(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);++/*+*Wecannotproceedwiththebuffercollectionandwe+*donothaveanydataforthecurrentsession.The+*etm_perfdriverexpectstocloseouttheaux_buffer+*atevent_stop().SodisabletheTRBEhereandleave+*theupdate_buffer()toreturna0size.+*/+trbe_drain_and_disable_local();+perf_aux_output_flag(handle,PERF_AUX_FLAG_TRUNCATED);+*this_cpu_ptr(buf->cpudata->drvdata->handle)=NULL;+}++/*+*TRBEBufferManagement+*+*TheTRBEbufferspansfromthebasepointertillthelimitpointer.Whenenabled,+*itstartswritingtracedatafromthewritepointeronwardtillthelimitpointer.+*Whenthewritepointerreachestheaddressjustbeforethelimitpointer,itgets+*wrappedaroundagaintothebasepointer.ThisiscalledaTRBEwrapevent,which+*generatesamaintenanceinterruptwhenoperatedinWRAPorFILLmode.Thisdriver+*usesFILLmode,wheretheTRBEstopsthetracecollectionatwrapevent.TheIRQ+*handlerupdatestheAUXbufferandre-enablestheTRBEwithupdatedWRITEand+*LIMITpointers.+*+*WraparoundwithanIRQ+*------<------<-------<-----<-----+*||+*------>------>------->----->-----+*+*+---------------+-----------------------++*|||+*+---------------+-----------------------++*BasePointerWritePointerLimitPointer+*+*ThebaseandlimitpointersalwaysneedstobePAGE_SIZEaligned.Butthewrite+*pointercanbealignedtotheimplementationdefinedTRBEtracebufferalignment+*ascapturedintrbe_cpudata->trbe_align.+*+*+*headtailwakeup+*+---------------------------------------+-----~~------+*|$$$$$$$|################|$$$$$$$$$$$$$$||+*+---------------------------------------+-----~~------+*BasePointerWritePointerLimitPointer+*+*Theperf_output_handleindices(head,tail,wakeup)aremonotonicallyincreasing+*valueswhichtracksallthedriverwritesanduserreadsfromtheperfauxiliary+*buffer.Generally[head..tail]istheareawherethedrivercanwriteintounless+*thewakeupisbehindthetail.EnabledTRBEbufferspanneedstobeadjustedand+*configureddependingontheperf_output_handleindices,sothatthedriverdoes+*notoverrideintoareasintheperfauxiliarybufferwhichisbeingoryettobe+*consumedfromtheuserspace.TheenabledTRBEbufferareaisamovingsubsetof+*theallocatedperfauxiliarybuffer.+*/+staticvoidtrbe_pad_buf(structperf_output_handle*handle,intlen)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);+u64head=PERF_IDX2OFF(handle->head,buf);++memset((void*)buf->trbe_base+head,ETE_IGNORE_PACKET,len);+if(!buf->snapshot)+perf_aux_output_skip(handle,len);+}++staticunsignedlongtrbe_snapshot_offset(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);++/*+*TheETEtracehasalignmentsynchronizationpacketsallowing+*thedecodertoresetincaseofanoverfloworcorruption.+*Sowecanusetheentirebufferforthesnapshotmode.+*/+returnbuf->nr_pages*PAGE_SIZE;+}++/*+*TRBELimitCalculation+*+*ThefollowingmarkersareusedtoillustratevariousTRBEbuffersituations.+*+*$$$$-Dataarea,unconsumedcapturedtracedata,nottobeoverridden+*####-Freearea,enabled,tracewillbewritten+*%%%%-Freearea,disabled,tracewillnotbewritten+*====-Freearea,paddedwithETE_IGNORE_PACKET,tracewillbeskipped+*/+staticunsignedlong__trbe_normal_offset(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);+structtrbe_cpudata*cpudata=buf->cpudata;+constu64bufsize=buf->nr_pages*PAGE_SIZE;+u64limit=bufsize;+u64head,tail,wakeup;++head=PERF_IDX2OFF(handle->head,buf);++/*+*head+*------->|+*|+*headTRBEaligntail+*+----|-------|---------------|-------++*|$$$$|=======|###############|$$$$$$$|+*+----|-------|---------------|-------++*trbe_basetrbe_base+nr_pages+*+*Perfauxbufferoutputheadpositioncanbemisaligneddependingon+*variousfactorsincludinguserspacereads.Incasemisaligned,head+*needstobealignedbeforeTRBEcanbeconfigured.Padthealignment+*gapwithETE_IGNORE_PACKETbytesthatwillbeignoredbyusertools+*andskipthissectionthusadvancingthehead.+*/+if(!IS_ALIGNED(head,cpudata->trbe_align)){+unsignedlongdelta=roundup(head,cpudata->trbe_align)-head;++delta=min(delta,handle->size);+trbe_pad_buf(handle,delta);+head=PERF_IDX2OFF(handle->head,buf);+}++/*+*head=tail(size=0)+*+----|-------------------------------++*|$$$$|$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$|+*+----|-------------------------------++*trbe_basetrbe_base+nr_pages+*+*Perfauxbufferdoesnothaveanyspaceforthedrivertowriteinto.+*Justcommunicatetracetruncationeventtotheuserspacebymarking+*itwithPERF_AUX_FLAG_TRUNCATED.+*/+if(!handle->size){+perf_aux_output_flag(handle,PERF_AUX_FLAG_TRUNCATED);+return0;+}++/* Compute the tail and wakeup indices now that we've aligned head */+tail=PERF_IDX2OFF(handle->head+handle->size,buf);+wakeup=PERF_IDX2OFF(handle->wakeup,buf);++/*+*LetscalculatethebufferareawhichTRBEcouldwriteinto.There+*arethreepossiblescenarioshere.Limitneedstobealignedwith+*PAGE_SIZEpertheTRBErequirement.Alwaysavoidclobberingthe+*unconsumeddata.+*+*1)head<tail+*+*headtail+*+----|-----------------------|-------++*|$$$$|#######################|$$$$$$$|+*+----|-----------------------|-------++*trbe_baselimittrbe_base+nr_pages+*+*TRBEcouldwriteinto[head..tail]area.Unlessthetailisrightat+*theendofthebuffer,neitheranwraparoundnoranIRQisexpected+*whilebeingenabled.+*+*2)head==tail+*+*head=tail(size>0)+*+----|-------------------------------++*|%%%%|###############################|+*+----|-------------------------------++*trbe_baselimit=trbe_base+nr_pages+*+*TRBEshouldjustwriteinto[head..base+nr_pages]areaeventhough+*theentirebufferisempty.Reasonbeing,whenthetracereachesthe+*endofthebuffer,itwilljustwraparoundwithanIRQgivingan+*opportunitytoreconfigurethebuffer.+*+*3)tail<head+*+*tailhead+*+----|-----------------------|-------++*|%%%%|$$$$$$$$$$$$$$$$$$$$$$$|#######|+*+----|-----------------------|-------++*trbe_baselimit=trbe_base+nr_pages+*+*TRBEshouldjustwriteinto[head..base+nr_pages]areaeventhough+*the[trbe_base..tail]isalsoempty.Reasonbeing,whenthetrace+*reachestheendofthebuffer,itwilljustwraparoundwithanIRQ+*givinganopportunitytoreconfigurethebuffer.+*/+if(head<tail)+limit=round_down(tail,PAGE_SIZE);++/*+*Wakeupmaybearbitrarilyfarintothefuture.Ifit'snotinthe+*currentgeneration,eitherwe'llwrapbeforehittingit,orit's+*inthepastandhasbeenhandledalready.+*+*Ifthere'sawakeupbeforewewrap,arrangetobewokenupbythe+*pageboundaryfollowingit.Keepthetailboundaryifthat'slower.+*+*headwakeuptail+*+----|---------------|-------|-------++*|$$$$|###############|%%%%%%%|$$$$$$$|+*+----|---------------|-------|-------++*trbe_baselimittrbe_base+nr_pages+*/+if(handle->wakeup<(handle->head+handle->size)&&head<=wakeup)+limit=min(limit,round_up(wakeup,PAGE_SIZE));++/*+*Therearetwosituationwhenthiscanhappeni.elimitisbefore+*theheadandhenceTRBEcannotbeconfigured.+*+*1)head<tail(aligneddownwithPAGE_SIZE)andalsotheyareboth+*withinthesamePAGEsizerange.+*+*PAGE_SIZE+*|----------------------|+*+*limitheadtail+*+------------|------|--------|-------++*|$$$$$$$$$$$$$$$$$$$|========|$$$$$$$|+*+------------|------|--------|-------++*trbe_basetrbe_base+nr_pages+*+*2)head<wakeup(alignedupwithPAGE_SIZE)<tailandalsoboth+*headandwakeuparewithinsamePAGEsizerange.+*+*PAGE_SIZE+*|----------------------|+*+*limitheadwakeuptail+*+----|------|-------|--------|-------++*|$$$$$$$$$$$|=======|========|$$$$$$$|+*+----|------|-------|--------|-------++*trbe_basetrbe_base+nr_pages+*/+if(limit>head)+returnlimit;++trbe_pad_buf(handle,handle->size);+perf_aux_output_flag(handle,PERF_AUX_FLAG_TRUNCATED);+return0;+}++staticunsignedlongtrbe_normal_offset(structperf_output_handle*handle)+{+structtrbe_buf*buf=perf_get_aux(handle);+u64limit=__trbe_normal_offset(handle);+u64head=PERF_IDX2OFF(handle->head,buf);++/*+*Iftheheadistooclosetothelimitandwedon't+*havespaceforameaningfulrun,weratherpadit+*andstartfresh.+*/+if(limit&&(limit-head<TRBE_TRACE_MIN_BUF_SIZE)){+trbe_pad_buf(handle,limit-head);+limit=__trbe_normal_offset(handle);+}+returnlimit;+}++staticunsignedlongcompute_trbe_buffer_limit(structperf_output_handle*handle)+{+structtrbe_buf*buf=etm_perf_sink_config(handle);+unsignedlongoffset;++if(buf->snapshot)+offset=trbe_snapshot_offset(handle);+else+offset=trbe_normal_offset(handle);+returnbuf->trbe_base+offset;+}++staticvoidclr_trbe_status(void)+{+u64trbsr=read_sysreg_s(SYS_TRBSR_EL1);++WARN_ON(is_trbe_enabled());+trbsr&=~TRBSR_IRQ;+trbsr&=~TRBSR_TRG;+trbsr&=~TRBSR_WRAP;+trbsr&=~(TRBSR_EC_MASK<<TRBSR_EC_SHIFT);+trbsr&=~(TRBSR_BSC_MASK<<TRBSR_BSC_SHIFT);+trbsr&=~TRBSR_STOP;+write_sysreg_s(trbsr,SYS_TRBSR_EL1);+}++staticvoidset_trbe_limit_pointer_enabled(unsignedlongaddr)+{+u64trblimitr=read_sysreg_s(SYS_TRBLIMITR_EL1);++WARN_ON(!IS_ALIGNED(addr,(1UL<<TRBLIMITR_LIMIT_SHIFT)));+WARN_ON(!IS_ALIGNED(addr,PAGE_SIZE));++trblimitr&=~TRBLIMITR_NVM;+trblimitr&=~(TRBLIMITR_FILL_MODE_MASK<<TRBLIMITR_FILL_MODE_SHIFT);+trblimitr&=~(TRBLIMITR_TRIG_MODE_MASK<<TRBLIMITR_TRIG_MODE_SHIFT);+trblimitr&=~(TRBLIMITR_LIMIT_MASK<<TRBLIMITR_LIMIT_SHIFT);++/*+*Filltracebuffermodeisusedherewhileconfiguringthe+*TRBEfortracecapture.Inthisparticularmode,thetrace+*collectionisstoppedandamaintenanceinterruptisraised+*whenthecurrentwritepointerwraps.Thispauseintrace+*collectiongivesthesoftwareanopportunitytocapturethe+*tracedataintheinterrupthandler,beforereconfiguring+*theTRBE.+*/+trblimitr|=(TRBE_FILL_MODE_FILL&TRBLIMITR_FILL_MODE_MASK)<<TRBLIMITR_FILL_MODE_SHIFT;++/*+*TriggermodeisnotusedherewhileconfiguringtheTRBEfor+*thetracecapture.Hencejustkeepthisintheignoremode.+*/+trblimitr|=(TRBE_TRIG_MODE_IGNORE&TRBLIMITR_TRIG_MODE_MASK)<<TRBLIMITR_TRIG_MODE_SHIFT;+trblimitr|=(addr&PAGE_MASK);++trblimitr|=TRBLIMITR_ENABLE;+write_sysreg_s(trblimitr,SYS_TRBLIMITR_EL1);+}++staticvoidtrbe_enable_hw(structtrbe_buf*buf)+{+WARN_ON(buf->trbe_write<buf->trbe_base);+WARN_ON(buf->trbe_write>=buf->trbe_limit);+set_trbe_disabled();+isb();+clr_trbe_status();+set_trbe_base_pointer(buf->trbe_base);+set_trbe_write_pointer(buf->trbe_write);++/*+*Synchronizealltheregisterupdates+*tillnowbeforeenablingtheTRBE.+*/+isb();+set_trbe_limit_pointer_enabled(buf->trbe_limit);++/* Synchronize the TRBE enable event */+isb();+}++staticvoid*arm_trbe_alloc_buffer(structcoresight_device*csdev,+structperf_event*event,void**pages,+intnr_pages,boolsnapshot)+{+structtrbe_buf*buf;+structpage**pglist;+inti;++/*+*TRBELIMITandTRBEWRITEpointersmustbepagealigned.Butwith+*justasinglepage,thereisnotmuchroomleftwhilewritinginto+*apartiallyfilledTRBEbuffer.Hencerestricttheminimumbuffer+*sizeastwopages.+*/
I read this three times and event then I'm not sure to fully get it. I
suggest re-working this paragraph.
I guess it should be "no room left" instead of "not much room left". If there
is only a single page and the buffer is partially filled, there wont be space
left to write the traces after the WRITE/LIMIT pointer's PAGE_SIZE alignment.
/*
* TRBE LIMIT and TRBE WRITE pointers must be page aligned. But with
* just a single page, there would not be any room left while writing
* into a partially filled TRBE buffer after the page size alignment.
* Hence restrict the minimum buffer size as two pages.
*/
quoted
+ if (nr_pages < 2)
+ return NULL;
+
+ buf = kzalloc_node(sizeof(*buf), GFP_KERNEL, trbe_alloc_node(event));
+ if (IS_ERR(buf))
+ return ERR_PTR(-ENOMEM);
+
+ pglist = kcalloc(nr_pages, sizeof(*pglist), GFP_KERNEL);
+ if (IS_ERR(pglist)) {
+ kfree(buf);
+ return ERR_PTR(-ENOMEM);
+ }
+
+ for (i = 0; i < nr_pages; i++)
+ pglist[i] = virt_to_page(pages[i]);
+
+ buf->trbe_base = (unsigned long) vmap(pglist, nr_pages, VM_MAP, PAGE_KERNEL);
+ if (IS_ERR((void *)buf->trbe_base)) {
Why not simply make buf->trbe_base a void * instead of having to do all this
There are many arithmetic and comparison operations involving trbe_base
element. Hence it might be better to keep it as unsigned long, also to
keeps it consistent with other pointers i.e trbe_write, trbe_limit.
Snippet from $cat drivers/hwtracing/coresight/coresight-trbe.c | grep "trbe_base"
There are just two places type casting trbe_base back to (void *).
memset((void *)buf->trbe_base + head, ETE_IGNORE_PACKET, len);
return buf->trbe_base + offset;
WARN_ON(buf->trbe_write < buf->trbe_base);
set_trbe_base_pointer(buf->trbe_base);
buf->trbe_base = (unsigned long)vmap(pglist, nr_pages, VM_MAP, PAGE_KERNEL);
if (IS_ERR((void *)buf->trbe_base)) {
return ERR_PTR(buf->trbe_base);
buf->trbe_limit = buf->trbe_base + nr_pages * PAGE_SIZE;
buf->trbe_write = buf->trbe_base;
vunmap((void *)buf->trbe_base);
base = get_trbe_base_pointer();
buf->trbe_write = buf->trbe_base + PERF_IDX2OFF(handle->head, buf);
if (buf->trbe_limit == buf->trbe_base) {
buf->trbe_write = buf->trbe_base + PERF_IDX2OFF(handle->head, buf);
if (buf->trbe_limit == buf->trbe_base) {
offset = get_trbe_limit_pointer() - get_trbe_base_pointer();
buf->trbe_write = buf->trbe_base + PERF_IDX2OFF(handle->head, buf);
if (buf->trbe_limit == buf->trbe_base) {
WARN_ON(buf->trbe_base != get_trbe_base_pointer());
if (get_trbe_write_pointer() == get_trbe_base_pointer())
casting? And IS_ERR() doesn't work with vmap().
Sure, will drop IS_ERR() here.
quoted
+ kfree(pglist);
+ kfree(buf);
+ return ERR_PTR(buf->trbe_base);
+ }
+ buf->trbe_limit = buf->trbe_base + nr_pages * PAGE_SIZE;
+ buf->trbe_write = buf->trbe_base;
+ buf->snapshot = snapshot;
+ buf->nr_pages = nr_pages;
+ buf->pages = pages;
+ kfree(pglist);
+ return buf;
+}
+
+void arm_trbe_free_buffer(void *config)
+{
+ struct trbe_buf *buf = config;
+
+ vunmap((void *)buf->trbe_base);
+ kfree(buf);
+}
+
+static unsigned long arm_trbe_update_buffer(struct coresight_device *csdev,
+ struct perf_output_handle *handle,
+ void *config)
+{
+ struct trbe_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
+ struct trbe_cpudata *cpudata = dev_get_drvdata(&csdev->dev);
+ struct trbe_buf *buf = config;
+ unsigned long size, offset;
+
+ WARN_ON(buf->cpudata != cpudata);
+ WARN_ON(cpudata->cpu != smp_processor_id());
+ WARN_ON(cpudata->drvdata != drvdata);
+ if (cpudata->mode != CS_MODE_PERF)
+ return -EINVAL;
+
+ /*
+ * If the TRBE was disabled due to lack of space in the AUX buffer or a
+ * spurious fault, the driver leaves it disabled, truncating the buffer.
+ * Since the etm_perf driver expects to close out the AUX buffer, the
+ * driver skips it. Thus, just pass in 0 size here to indicate that the
+ * buffer was truncated.
+ */
+ if (!is_trbe_enabled())
+ return 0;
+ /*
+ * perf handle structure needs to be shared with the TRBE IRQ handler for
+ * capturing trace data and restarting the handle. There is a probability
+ * of an undefined reference based crash when etm event is being stopped
+ * while a TRBE IRQ also getting processed. This happens due the release
+ * of perf handle via perf_aux_output_end() in etm_event_stop(). Stopping
+ * the TRBE here will ensure that no IRQ could be generated when the perf
+ * handle gets freed in etm_event_stop().
+ */
+ trbe_drain_and_disable_local();
+ offset = get_trbe_write_pointer() - get_trbe_base_pointer();
+ size = offset - PERF_IDX2OFF(handle->head, buf);
+ if (buf->snapshot)
+ handle->head += size;
+ return size;
+}
+
+static int arm_trbe_enable(struct coresight_device *csdev, u32 mode, void *data)
+{
+ struct trbe_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
+ struct trbe_cpudata *cpudata = dev_get_drvdata(&csdev->dev);
+ struct perf_output_handle *handle = data;
+ struct trbe_buf *buf = etm_perf_sink_config(handle);
+
+ WARN_ON(cpudata->cpu != smp_processor_id());
+ WARN_ON(cpudata->drvdata != drvdata);
+ if (mode != CS_MODE_PERF)
+ return -EINVAL;
+
+ *this_cpu_ptr(drvdata->handle) = handle;
+ cpudata->buf = buf;
+ cpudata->mode = mode;
+ buf->cpudata = cpudata;
+ buf->trbe_limit = compute_trbe_buffer_limit(handle);
+ buf->trbe_write = buf->trbe_base + PERF_IDX2OFF(handle->head, buf);
+ if (buf->trbe_limit == buf->trbe_base) {
+ trbe_stop_and_truncate_event(handle);
+ return 0;
+ }
+ trbe_enable_hw(buf);
+ return 0;
+}
+
+static int arm_trbe_disable(struct coresight_device *csdev)
+{
+ struct trbe_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
+ struct trbe_cpudata *cpudata = dev_get_drvdata(&csdev->dev);
+ struct trbe_buf *buf = cpudata->buf;
+
+ WARN_ON(buf->cpudata != cpudata);
+ WARN_ON(cpudata->cpu != smp_processor_id());
+ WARN_ON(cpudata->drvdata != drvdata);
+ if (cpudata->mode != CS_MODE_PERF)
+ return -EINVAL;
+
+ trbe_drain_and_disable_local();
+ buf->cpudata = NULL;
+ cpudata->buf = NULL;
+ cpudata->mode = CS_MODE_DISABLED;
+ return 0;
+}
+
+static void trbe_handle_spurious(struct perf_output_handle *handle)
+{
+ struct trbe_buf *buf = etm_perf_sink_config(handle);
+
+ buf->trbe_limit = compute_trbe_buffer_limit(handle);
+ buf->trbe_write = buf->trbe_base + PERF_IDX2OFF(handle->head, buf);
+ if (buf->trbe_limit == buf->trbe_base) {
+ trbe_drain_and_disable_local();
+ return;
+ }
+ trbe_enable_hw(buf);
+}
+
+static void trbe_handle_overflow(struct perf_output_handle *handle)
+{
+ struct perf_event *event = handle->event;
+ struct trbe_buf *buf = etm_perf_sink_config(handle);
+ unsigned long offset, size;
+ struct etm_event_data *event_data;
+
+ offset = get_trbe_limit_pointer() - get_trbe_base_pointer();
+ size = offset - PERF_IDX2OFF(handle->head, buf);
+ if (buf->snapshot)
+ handle->head = offset;
+ perf_aux_output_end(handle, size);
+
+ event_data = perf_aux_output_begin(handle, event);
+ if (!event_data) {
+ trbe_drain_and_disable_local();
+ *this_cpu_ptr(buf->cpudata->drvdata->handle) = NULL;
+ return;
+ }
+ buf->trbe_limit = compute_trbe_buffer_limit(handle);
+ buf->trbe_write = buf->trbe_base + PERF_IDX2OFF(handle->head, buf);
+ if (buf->trbe_limit == buf->trbe_base) {
+ trbe_stop_and_truncate_event(handle);
+ return;
+ }
+ *this_cpu_ptr(buf->cpudata->drvdata->handle) = handle;
+ trbe_enable_hw(buf);
+}
+
+static bool is_perf_trbe(struct perf_output_handle *handle)
+{
+ struct trbe_buf *buf = etm_perf_sink_config(handle);
+ struct trbe_cpudata *cpudata = buf->cpudata;
+ struct trbe_drvdata *drvdata = cpudata->drvdata;
+ int cpu = smp_processor_id();
+
+ WARN_ON(buf->trbe_base != get_trbe_base_pointer());
+ WARN_ON(buf->trbe_limit != get_trbe_limit_pointer());
+
+ if (cpudata->mode != CS_MODE_PERF)
+ return false;
+
+ if (cpudata->cpu != cpu)
+ return false;
+
+ if (!cpumask_test_cpu(cpu, &drvdata->supported_cpus))
+ return false;
+
+ return true;
+}
+
+static enum trbe_fault_action trbe_get_fault_act(struct perf_output_handle *handle)
+{
+ u64 trbsr = read_sysreg_s(SYS_TRBSR_EL1);
+ int ec = get_trbe_ec(trbsr);
+ int bsc = get_trbe_bsc(trbsr);
+
+ WARN_ON(is_trbe_running(trbsr));
+ if (is_trbe_trg(trbsr) || is_trbe_abort(trbsr))
+ return TRBE_FAULT_ACT_FATAL;
+
+ if ((ec == TRBE_EC_STAGE1_ABORT) || (ec == TRBE_EC_STAGE2_ABORT))
+ return TRBE_FAULT_ACT_FATAL;
+
+ if (is_trbe_wrap(trbsr) && (ec == TRBE_EC_OTHERS) && (bsc == TRBE_BSC_FILLED)) {
+ if (get_trbe_write_pointer() == get_trbe_base_pointer())
+ return TRBE_FAULT_ACT_WRAP;
+ }
+ return TRBE_FAULT_ACT_SPURIOUS;
+}
+
+static irqreturn_t arm_trbe_irq_handler(int irq, void *dev)
+{
+ struct perf_output_handle **handle_ptr = dev;
+ struct perf_output_handle *handle = *handle_ptr;
+ enum trbe_fault_action act;
+
+ WARN_ON(!is_trbe_irq(read_sysreg_s(SYS_TRBSR_EL1)));
+ clr_trbe_irq();
+
+ /*
+ * Ensure the trace is visible to the CPUs and
+ * any external aborts have been resolved.
+ */
+ trbe_drain_buffer();
+ isb();
+
+ if (!perf_get_aux(handle))
+ return IRQ_NONE;
+
+ if (!is_perf_trbe(handle))
+ return IRQ_NONE;
+
+ irq_work_run();
+
+ act = trbe_get_fault_act(handle);
+ switch (act) {
+ case TRBE_FAULT_ACT_WRAP:
+ trbe_handle_overflow(handle);
+ break;
+ case TRBE_FAULT_ACT_SPURIOUS:
+ trbe_handle_spurious(handle);
+ break;
+ case TRBE_FAULT_ACT_FATAL:
+ trbe_stop_and_truncate_event(handle);
+ break;
+ }
+ return IRQ_HANDLED;
+}
+
+static const struct coresight_ops_sink arm_trbe_sink_ops = {
+ .enable = arm_trbe_enable,
+ .disable = arm_trbe_disable,
+ .alloc_buffer = arm_trbe_alloc_buffer,
+ .free_buffer = arm_trbe_free_buffer,
+ .update_buffer = arm_trbe_update_buffer,
+};
+
+static const struct coresight_ops arm_trbe_cs_ops = {
+ .sink_ops = &arm_trbe_sink_ops,
+};
+
+static ssize_t align_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+ struct trbe_cpudata *cpudata = dev_get_drvdata(dev);
+
+ return sprintf(buf, "%llx\n", cpudata->trbe_align);
+}
+static DEVICE_ATTR_RO(align);
+
+static ssize_t dbm_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+ struct trbe_cpudata *cpudata = dev_get_drvdata(dev);
+
+ return sprintf(buf, "%d\n", cpudata->trbe_dbm);
+}
+static DEVICE_ATTR_RO(dbm);
What does "dbm" stand for? Looking at the documentation for TRBIDR_EL1.F, I
don't see what "dbm" relates to.
I made it up to refer TRBIDR_EL1.F as "Dirty (and Access Flag) Bit Management".
Could change it as "afdbm" to be more specific or if it is preferred.
On Wed, Jan 27, 2021 at 02:25:35PM +0530, Anshuman Khandual wrote:
quoted
Trace Buffer Extension (TRBE) implements a trace buffer per CPU which is
accessible via the system registers. The TRBE supports different addressing
modes including CPU virtual address and buffer modes including the circular
buffer mode. The TRBE buffer is addressed by a base pointer (TRBBASER_EL1),
an write pointer (TRBPTR_EL1) and a limit pointer (TRBLIMITR_EL1). But the
access to the trace buffer could be prohibited by a higher exception level
(EL3 or EL2), indicated by TRBIDR_EL1.P. The TRBE can also generate a CPU
private interrupt (PPI) on address translation errors and when the buffer
is full. Overall implementation here is inspired from the Arm SPE driver.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
Changes in V3:
- Added new DT bindings document TRBE.yaml
- Changed TRBLIMITR_TRIG_MODE_SHIFT from 2 to 3
- Dropped isb() from trbe_reset_local()
- Dropped gap between (void *) and buf->trbe_base
- Changed 'int' to 'unsigned int' in is_trbe_available()
- Dropped unused function set_trbe_running(), set_trbe_virtual_mode(),
set_trbe_enabled() and set_trbe_limit_pointer()
- Changed get_trbe_flag_update(), is_trbe_programmable() and
get_trbe_address_align() to accept TRBIDR value
- Changed is_trbe_running(), is_trbe_abort(), is_trbe_wrap(), is_trbe_trg(),
is_trbe_irq(), get_trbe_bsc() and get_trbe_ec() to accept TRBSR value
- Dropped snapshot mode condition in arm_trbe_alloc_buffer()
- Exit arm_trbe_init() when arm64_kernel_unmapped_at_el0() is enabled
- Compute trbe_limit before trbe_write to get the updated handle
- Added trbe_stop_and_truncate_event()
- Dropped trbe_handle_fatal()
Documentation/trace/coresight/coresight-trbe.rst | 39 +
arch/arm64/include/asm/sysreg.h | 1 +
drivers/hwtracing/coresight/Kconfig | 11 +
drivers/hwtracing/coresight/Makefile | 1 +
drivers/hwtracing/coresight/coresight-trbe.c | 1023 ++++++++++++++++++++++
drivers/hwtracing/coresight/coresight-trbe.h | 160 ++++
6 files changed, 1235 insertions(+)
create mode 100644 Documentation/trace/coresight/coresight-trbe.rst
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.c
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.h
+
There is already a check for this in arm_trbe_probe_coresight(), we couldn't be
here if there was a problem with the allocation.
Right but just to be extra cautious. Do you really want this to be dropped ?
quoted
+
+ if (trbe_csdev)
+ return;
Now that's a reason to have a WARN_ON(). If we are probing and a sink is
already present in this cpu's slot, something went seriously wrong and we should
be clear about it.
Right, will add an WARN_ON().
quoted
+
+ cpudata->cpu = smp_processor_id();
+ cpudata->drvdata = drvdata;
+ dev = &cpudata->drvdata->pdev->dev;
+
+ if (!is_trbe_available()) {
+ pr_err("TRBE is not implemented on cpu %d\n", cpudata->cpu);
+ goto cpu_clear;
+ }
+
+ if (!is_trbe_programmable(trbidr)) {
+ pr_err("TRBE is owned in higher exception level on cpu %d\n", cpudata->cpu);
+ goto cpu_clear;
+ }
+ desc.name = devm_kasprintf(dev, GFP_KERNEL, "%s%d", DRVNAME, smp_processor_id());
We will end up with "arm_trbe0", "arm_trbe1" and so on in sysfs... Is the
"arm_" part absolutely needed? I think this should be like what we do for etmv3
and etmv4 where only "etmX" shows up in sysfs.
Okay, will drop arm_ here. IIRC this was originally trbeX where X is the cpu number
but then ended up using DRVNAME as prefix.
Here coresight_unregister() should be called. The other option is to call
coresight_register() when everything else is known to be fine, which is the
favoured approach.
In what scenario do you see not having a trbe_csdev and still needing to disable
IRQs for the HW? If there is a such a case then a few lines of comment is
needed.
Theoretically this code shouldn't run when the TRBE is enabled, because the CS
core will prevent that from happening. As sush disabling interrupts after
coresight_unregister() has been called and setting cpudata->drvdata to NULL
should be fine. But from an outsider's point of view it will look very bizarre.
Either write a comment to explain all that or call the above two before doing
the cleanup.
Okay, will move them before the cleanup.
quoted
+}
+
+static int arm_trbe_probe_coresight(struct trbe_drvdata *drvdata)
+{
+ drvdata->cpudata = alloc_percpu(typeof(*drvdata->cpudata));
+ if (IS_ERR(drvdata->cpudata))
+ return PTR_ERR(drvdata->cpudata);
As far as I can tell alloc_percpu() returns NULL on failure and nothing else.
Sure, will change the return code as -ENOMEM when alloc_percpu() returns NULL.
The above two calls look racy to me. The executing process could be moved to
another CPU between the call to arm_trbe_probe_coresight_cpu() and
smp_call_function_many(), which would prevent the initialisation of the TRBE on
the new CPU to be done. I suggest using a for_each_cpu() loop where
smp_call_function_single() would be used. That way we are guaranteed all the
TRBEs will be initialised.
+ if (nr_pages < 2)
+ return NULL;
+
+ buf = kzalloc_node(sizeof(*buf), GFP_KERNEL, trbe_alloc_node(event));
+ if (IS_ERR(buf))
+ return ERR_PTR(-ENOMEM);
+
+ pglist = kcalloc(nr_pages, sizeof(*pglist), GFP_KERNEL);
+ if (IS_ERR(pglist)) {
+ kfree(buf);
+ return ERR_PTR(-ENOMEM);
+ }
+
+ for (i = 0; i < nr_pages; i++)
+ pglist[i] = virt_to_page(pages[i]);
+
+ buf->trbe_base = (unsigned long) vmap(pglist, nr_pages, VM_MAP, PAGE_KERNEL);
+ if (IS_ERR((void *)buf->trbe_base)) {
Why not simply make buf->trbe_base a void * instead of having to do all this
There are many arithmetic and comparison operations involving trbe_base
element. Hence it might be better to keep it as unsigned long, also to
keeps it consistent with other pointers i.e trbe_write, trbe_limit.
That is a fair point. Please add a comment to explain your design choice and
make sure the sparse checker is happy with all of it.
Snippet from $cat drivers/hwtracing/coresight/coresight-trbe.c | grep "trbe_base"
There are just two places type casting trbe_base back to (void *).
memset((void *)buf->trbe_base + head, ETE_IGNORE_PACKET, len);
return buf->trbe_base + offset;
WARN_ON(buf->trbe_write < buf->trbe_base);
set_trbe_base_pointer(buf->trbe_base);
buf->trbe_base = (unsigned long)vmap(pglist, nr_pages, VM_MAP, PAGE_KERNEL);
if (IS_ERR((void *)buf->trbe_base)) {
return ERR_PTR(buf->trbe_base);
buf->trbe_limit = buf->trbe_base + nr_pages * PAGE_SIZE;
buf->trbe_write = buf->trbe_base;
vunmap((void *)buf->trbe_base);
base = get_trbe_base_pointer();
buf->trbe_write = buf->trbe_base + PERF_IDX2OFF(handle->head, buf);
if (buf->trbe_limit == buf->trbe_base) {
buf->trbe_write = buf->trbe_base + PERF_IDX2OFF(handle->head, buf);
if (buf->trbe_limit == buf->trbe_base) {
offset = get_trbe_limit_pointer() - get_trbe_base_pointer();
buf->trbe_write = buf->trbe_base + PERF_IDX2OFF(handle->head, buf);
if (buf->trbe_limit == buf->trbe_base) {
WARN_ON(buf->trbe_base != get_trbe_base_pointer());
if (get_trbe_write_pointer() == get_trbe_base_pointer())
What does "dbm" stand for? Looking at the documentation for TRBIDR_EL1.F, I
don't see what "dbm" relates to.
I made it up to refer TRBIDR_EL1.F as "Dirty (and Access Flag) Bit Management".
Could change it as "afdbm" to be more specific or if it is preferred.
I don't see "afdbm" being a better solution - why not simply "flag"?
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
On Fri, Feb 12, 2021 at 11:13:01AM +0530, Anshuman Khandual wrote:
On 2/11/21 12:30 AM, Mathieu Poirier wrote:
quoted
On Wed, Jan 27, 2021 at 02:25:35PM +0530, Anshuman Khandual wrote:
quoted
Trace Buffer Extension (TRBE) implements a trace buffer per CPU which is
accessible via the system registers. The TRBE supports different addressing
modes including CPU virtual address and buffer modes including the circular
buffer mode. The TRBE buffer is addressed by a base pointer (TRBBASER_EL1),
an write pointer (TRBPTR_EL1) and a limit pointer (TRBLIMITR_EL1). But the
access to the trace buffer could be prohibited by a higher exception level
(EL3 or EL2), indicated by TRBIDR_EL1.P. The TRBE can also generate a CPU
private interrupt (PPI) on address translation errors and when the buffer
is full. Overall implementation here is inspired from the Arm SPE driver.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
Changes in V3:
- Added new DT bindings document TRBE.yaml
- Changed TRBLIMITR_TRIG_MODE_SHIFT from 2 to 3
- Dropped isb() from trbe_reset_local()
- Dropped gap between (void *) and buf->trbe_base
- Changed 'int' to 'unsigned int' in is_trbe_available()
- Dropped unused function set_trbe_running(), set_trbe_virtual_mode(),
set_trbe_enabled() and set_trbe_limit_pointer()
- Changed get_trbe_flag_update(), is_trbe_programmable() and
get_trbe_address_align() to accept TRBIDR value
- Changed is_trbe_running(), is_trbe_abort(), is_trbe_wrap(), is_trbe_trg(),
is_trbe_irq(), get_trbe_bsc() and get_trbe_ec() to accept TRBSR value
- Dropped snapshot mode condition in arm_trbe_alloc_buffer()
- Exit arm_trbe_init() when arm64_kernel_unmapped_at_el0() is enabled
- Compute trbe_limit before trbe_write to get the updated handle
- Added trbe_stop_and_truncate_event()
- Dropped trbe_handle_fatal()
Documentation/trace/coresight/coresight-trbe.rst | 39 +
arch/arm64/include/asm/sysreg.h | 1 +
drivers/hwtracing/coresight/Kconfig | 11 +
drivers/hwtracing/coresight/Makefile | 1 +
drivers/hwtracing/coresight/coresight-trbe.c | 1023 ++++++++++++++++++++++
drivers/hwtracing/coresight/coresight-trbe.h | 160 ++++
6 files changed, 1235 insertions(+)
create mode 100644 Documentation/trace/coresight/coresight-trbe.rst
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.c
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.h
+
There is already a check for this in arm_trbe_probe_coresight(), we couldn't be
here if there was a problem with the allocation.
Right but just to be extra cautious. Do you really want this to be dropped ?
I don't think it is necessary but there is no harm in keeping it if you are keen
on it.
quoted
quoted
+
+ if (trbe_csdev)
+ return;
Now that's a reason to have a WARN_ON(). If we are probing and a sink is
already present in this cpu's slot, something went seriously wrong and we should
be clear about it.
Right, will add an WARN_ON().
quoted
quoted
+
+ cpudata->cpu = smp_processor_id();
+ cpudata->drvdata = drvdata;
+ dev = &cpudata->drvdata->pdev->dev;
+
+ if (!is_trbe_available()) {
+ pr_err("TRBE is not implemented on cpu %d\n", cpudata->cpu);
+ goto cpu_clear;
+ }
+
+ if (!is_trbe_programmable(trbidr)) {
+ pr_err("TRBE is owned in higher exception level on cpu %d\n", cpudata->cpu);
+ goto cpu_clear;
+ }
+ desc.name = devm_kasprintf(dev, GFP_KERNEL, "%s%d", DRVNAME, smp_processor_id());
We will end up with "arm_trbe0", "arm_trbe1" and so on in sysfs... Is the
"arm_" part absolutely needed? I think this should be like what we do for etmv3
and etmv4 where only "etmX" shows up in sysfs.
Okay, will drop arm_ here. IIRC this was originally trbeX where X is the cpu number
but then ended up using DRVNAME as prefix.
Here coresight_unregister() should be called. The other option is to call
coresight_register() when everything else is known to be fine, which is the
favoured approach.
In what scenario do you see not having a trbe_csdev and still needing to disable
IRQs for the HW? If there is a such a case then a few lines of comment is
needed.
Theoretically this code shouldn't run when the TRBE is enabled, because the CS
core will prevent that from happening. As sush disabling interrupts after
coresight_unregister() has been called and setting cpudata->drvdata to NULL
should be fine. But from an outsider's point of view it will look very bizarre.
Either write a comment to explain all that or call the above two before doing
the cleanup.
Okay, will move them before the cleanup.
quoted
quoted
+}
+
+static int arm_trbe_probe_coresight(struct trbe_drvdata *drvdata)
+{
+ drvdata->cpudata = alloc_percpu(typeof(*drvdata->cpudata));
+ if (IS_ERR(drvdata->cpudata))
+ return PTR_ERR(drvdata->cpudata);
As far as I can tell alloc_percpu() returns NULL on failure and nothing else.
Sure, will change the return code as -ENOMEM when alloc_percpu() returns NULL.
The above two calls look racy to me. The executing process could be moved to
another CPU between the call to arm_trbe_probe_coresight_cpu() and
smp_call_function_many(), which would prevent the initialisation of the TRBE on
the new CPU to be done. I suggest using a for_each_cpu() loop where
smp_call_function_single() would be used. That way we are guaranteed all the
TRBEs will be initialised.
On Wed, Jan 27, 2021 at 02:25:35PM +0530, Anshuman Khandual wrote:
quoted hunk
Trace Buffer Extension (TRBE) implements a trace buffer per CPU which is
accessible via the system registers. The TRBE supports different addressing
modes including CPU virtual address and buffer modes including the circular
buffer mode. The TRBE buffer is addressed by a base pointer (TRBBASER_EL1),
an write pointer (TRBPTR_EL1) and a limit pointer (TRBLIMITR_EL1). But the
access to the trace buffer could be prohibited by a higher exception level
(EL3 or EL2), indicated by TRBIDR_EL1.P. The TRBE can also generate a CPU
private interrupt (PPI) on address translation errors and when the buffer
is full. Overall implementation here is inspired from the Arm SPE driver.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
Changes in V3:
- Added new DT bindings document TRBE.yaml
- Changed TRBLIMITR_TRIG_MODE_SHIFT from 2 to 3
- Dropped isb() from trbe_reset_local()
- Dropped gap between (void *) and buf->trbe_base
- Changed 'int' to 'unsigned int' in is_trbe_available()
- Dropped unused function set_trbe_running(), set_trbe_virtual_mode(),
set_trbe_enabled() and set_trbe_limit_pointer()
- Changed get_trbe_flag_update(), is_trbe_programmable() and
get_trbe_address_align() to accept TRBIDR value
- Changed is_trbe_running(), is_trbe_abort(), is_trbe_wrap(), is_trbe_trg(),
is_trbe_irq(), get_trbe_bsc() and get_trbe_ec() to accept TRBSR value
- Dropped snapshot mode condition in arm_trbe_alloc_buffer()
- Exit arm_trbe_init() when arm64_kernel_unmapped_at_el0() is enabled
- Compute trbe_limit before trbe_write to get the updated handle
- Added trbe_stop_and_truncate_event()
- Dropped trbe_handle_fatal()
Documentation/trace/coresight/coresight-trbe.rst | 39 +
arch/arm64/include/asm/sysreg.h | 1 +
drivers/hwtracing/coresight/Kconfig | 11 +
drivers/hwtracing/coresight/Makefile | 1 +
drivers/hwtracing/coresight/coresight-trbe.c | 1023 ++++++++++++++++++++++
drivers/hwtracing/coresight/coresight-trbe.h | 160 ++++
6 files changed, 1235 insertions(+)
create mode 100644 Documentation/trace/coresight/coresight-trbe.rst
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.c
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.h
@@ -0,0 +1,39 @@+.. SPDX-License-Identifier: GPL-2.0++==============================+Trace Buffer Extension (TRBE).+==============================++:Author: Anshuman Khandual <anshuman.khandual@arm.com>+:Date: November 2020++Hardware Description+--------------------++Trace Buffer Extension (TRBE) is a percpu hardware which captures in system+memory, CPU traces generated from a corresponding percpu tracing unit. This+gets plugged in as a coresight sink device because the corresponding trace+genarators (ETE), are plugged in as source device.++The TRBE is not compliant to CoreSight architecture specifications, but is+driven via the CoreSight driver framework to support the ETE (which is+CoreSight compliant) integration.++Sysfs files and directories+---------------------------++The TRBE devices appear on the existing coresight bus alongside the other+coresight devices::++ >$ ls /sys/bus/coresight/devices+ trbe0 trbe1 trbe2 trbe3++The ``trbe<N>`` named TRBEs are associated with a CPU.::++ >$ ls /sys/bus/coresight/devices/trbe0/+ align dbm++*Key file items are:-*+*``align``: TRBE write pointer alignment+*``dbm``: TRBE updates memory with access and dirty flags+
I haven't read the TRBE progammer's manual but looking a the documentation the above
looks good.
+
+static void set_trbe_limit_pointer_enabled(unsigned long addr)
+{
+ u64 trblimitr = read_sysreg_s(SYS_TRBLIMITR_EL1);
+
+ WARN_ON(!IS_ALIGNED(addr, (1UL << TRBLIMITR_LIMIT_SHIFT)));
+ WARN_ON(!IS_ALIGNED(addr, PAGE_SIZE));
+
+ trblimitr &= ~TRBLIMITR_NVM;
+ trblimitr &= ~(TRBLIMITR_FILL_MODE_MASK << TRBLIMITR_FILL_MODE_SHIFT);
+ trblimitr &= ~(TRBLIMITR_TRIG_MODE_MASK << TRBLIMITR_TRIG_MODE_SHIFT);
+ trblimitr &= ~(TRBLIMITR_LIMIT_MASK << TRBLIMITR_LIMIT_SHIFT);
+
+ /*
+ * Fill trace buffer mode is used here while configuring the
+ * TRBE for trace capture. In this particular mode, the trace
+ * collection is stopped and a maintenance interrupt is raised
+ * when the current write pointer wraps. This pause in trace
+ * collection gives the software an opportunity to capture the
+ * trace data in the interrupt handler, before reconfiguring
+ * the TRBE.
+ */
+ trblimitr |= (TRBE_FILL_MODE_FILL & TRBLIMITR_FILL_MODE_MASK) << TRBLIMITR_FILL_MODE_SHIFT;
+
+ /*
+ * Trigger mode is not used here while configuring the TRBE for
+ * the trace capture. Hence just keep this in the ignore mode.
+ */
+ trblimitr |= (TRBE_TRIG_MODE_IGNORE & TRBLIMITR_TRIG_MODE_MASK) << TRBLIMITR_TRIG_MODE_SHIFT;
+ trblimitr |= (addr & PAGE_MASK);
+
+ trblimitr |= TRBLIMITR_ENABLE;
+ write_sysreg_s(trblimitr, SYS_TRBLIMITR_EL1);
+}
Same here
+
+static void trbe_enable_hw(struct trbe_buf *buf)
+{
+ WARN_ON(buf->trbe_write < buf->trbe_base);
+ WARN_ON(buf->trbe_write >= buf->trbe_limit);
+ set_trbe_disabled();
+ isb();
+ clr_trbe_status();
+ set_trbe_base_pointer(buf->trbe_base);
+ set_trbe_write_pointer(buf->trbe_write);
+
+ /*
+ * Synchronize all the register updates
+ * till now before enabling the TRBE.
+ */
+ isb();
+ set_trbe_limit_pointer_enabled(buf->trbe_limit);
+
+ /* Synchronize the TRBE enable event */
+ isb();
+}
Ok
+
+static void *arm_trbe_alloc_buffer(struct coresight_device *csdev,
+ struct perf_event *event, void **pages,
+ int nr_pages, bool snapshot)
+{
+ struct trbe_buf *buf;
+ struct page **pglist;
+ int i;
+
+ /*
+ * TRBE LIMIT and TRBE WRITE pointers must be page aligned. But with
+ * just a single page, there is not much room left while writing into
+ * a partially filled TRBE buffer. Hence restrict the minimum buffer
+ * size as two pages.
+ */
+ if (nr_pages < 2)
+ return NULL;
+
+ buf = kzalloc_node(sizeof(*buf), GFP_KERNEL, trbe_alloc_node(event));
+ if (IS_ERR(buf))
+ return ERR_PTR(-ENOMEM);
+
+static unsigned long arm_trbe_update_buffer(struct coresight_device *csdev,
+ struct perf_output_handle *handle,
+ void *config)
+{
+ struct trbe_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
+ struct trbe_cpudata *cpudata = dev_get_drvdata(&csdev->dev);
+ struct trbe_buf *buf = config;
+ unsigned long size, offset;
+
+ WARN_ON(buf->cpudata != cpudata);
+ WARN_ON(cpudata->cpu != smp_processor_id());
+ WARN_ON(cpudata->drvdata != drvdata);
+ if (cpudata->mode != CS_MODE_PERF)
+ return -EINVAL;
+
+ /*
+ * If the TRBE was disabled due to lack of space in the AUX buffer or a
+ * spurious fault, the driver leaves it disabled, truncating the buffer.
+ * Since the etm_perf driver expects to close out the AUX buffer, the
+ * driver skips it. Thus, just pass in 0 size here to indicate that the
+ * buffer was truncated.
+ */
+ if (!is_trbe_enabled())
+ return 0;
+ /*
+ * perf handle structure needs to be shared with the TRBE IRQ handler for
+ * capturing trace data and restarting the handle. There is a probability
+ * of an undefined reference based crash when etm event is being stopped
+ * while a TRBE IRQ also getting processed. This happens due the release
+ * of perf handle via perf_aux_output_end() in etm_event_stop(). Stopping
+ * the TRBE here will ensure that no IRQ could be generated when the perf
+ * handle gets freed in etm_event_stop().
+ */
+ trbe_drain_and_disable_local();
+ offset = get_trbe_write_pointer() - get_trbe_base_pointer();
+ size = offset - PERF_IDX2OFF(handle->head, buf);
+ if (buf->snapshot)
+ handle->head += size;
+ return size;
+}
+{
+ u64 trbsr = read_sysreg_s(SYS_TRBSR_EL1);
+ int ec = get_trbe_ec(trbsr);
+ int bsc = get_trbe_bsc(trbsr);
+
+ WARN_ON(is_trbe_running(trbsr));
+ if (is_trbe_trg(trbsr) || is_trbe_abort(trbsr))
+ return TRBE_FAULT_ACT_FATAL;
+
+ if ((ec == TRBE_EC_STAGE1_ABORT) || (ec == TRBE_EC_STAGE2_ABORT))
+ return TRBE_FAULT_ACT_FATAL;
+
+ if (is_trbe_wrap(trbsr) && (ec == TRBE_EC_OTHERS) && (bsc == TRBE_BSC_FILLED)) {
+ if (get_trbe_write_pointer() == get_trbe_base_pointer())
+ return TRBE_FAULT_ACT_WRAP;
+ }
+ return TRBE_FAULT_ACT_SPURIOUS;
+}
+
+static irqreturn_t arm_trbe_irq_handler(int irq, void *dev)
+{
+ struct perf_output_handle **handle_ptr = dev;
+ struct perf_output_handle *handle = *handle_ptr;
+ enum trbe_fault_action act;
+
+ WARN_ON(!is_trbe_irq(read_sysreg_s(SYS_TRBSR_EL1)));
+ clr_trbe_irq();
+
+ /*
+ * Ensure the trace is visible to the CPUs and
+ * any external aborts have been resolved.
+ */
+ trbe_drain_buffer();
+ isb();
+
+ if (!perf_get_aux(handle))
+ return IRQ_NONE;
+
+ if (!is_perf_trbe(handle))
+ return IRQ_NONE;
+
+ irq_work_run();
I trust Will that this is the right thing to do.
I will stop here for this revision. I will dive more in the mechanic of the
TRBE on the next revision.
Thanks,
Mathieu
+ if (nr_pages < 2)
+ return NULL;
+
+ buf = kzalloc_node(sizeof(*buf), GFP_KERNEL, trbe_alloc_node(event));
+ if (IS_ERR(buf))
+ return ERR_PTR(-ENOMEM);
+
+ pglist = kcalloc(nr_pages, sizeof(*pglist), GFP_KERNEL);
+ if (IS_ERR(pglist)) {
+ kfree(buf);
+ return ERR_PTR(-ENOMEM);
+ }
+
+ for (i = 0; i < nr_pages; i++)
+ pglist[i] = virt_to_page(pages[i]);
+
+ buf->trbe_base = (unsigned long) vmap(pglist, nr_pages, VM_MAP, PAGE_KERNEL);
+ if (IS_ERR((void *)buf->trbe_base)) {
Why not simply make buf->trbe_base a void * instead of having to do all this
There are many arithmetic and comparison operations involving trbe_base
element. Hence it might be better to keep it as unsigned long, also to
keeps it consistent with other pointers i.e trbe_write, trbe_limit.
That is a fair point. Please add a comment to explain your design choice and
make sure the sparse checker is happy with all of it.
Added a comment.
quoted
Snippet from $cat drivers/hwtracing/coresight/coresight-trbe.c | grep "trbe_base"
There are just two places type casting trbe_base back to (void *).
memset((void *)buf->trbe_base + head, ETE_IGNORE_PACKET, len);
return buf->trbe_base + offset;
WARN_ON(buf->trbe_write < buf->trbe_base);
set_trbe_base_pointer(buf->trbe_base);
buf->trbe_base = (unsigned long)vmap(pglist, nr_pages, VM_MAP, PAGE_KERNEL);
if (IS_ERR((void *)buf->trbe_base)) {
return ERR_PTR(buf->trbe_base);
buf->trbe_limit = buf->trbe_base + nr_pages * PAGE_SIZE;
buf->trbe_write = buf->trbe_base;
vunmap((void *)buf->trbe_base);
base = get_trbe_base_pointer();
buf->trbe_write = buf->trbe_base + PERF_IDX2OFF(handle->head, buf);
if (buf->trbe_limit == buf->trbe_base) {
buf->trbe_write = buf->trbe_base + PERF_IDX2OFF(handle->head, buf);
if (buf->trbe_limit == buf->trbe_base) {
offset = get_trbe_limit_pointer() - get_trbe_base_pointer();
buf->trbe_write = buf->trbe_base + PERF_IDX2OFF(handle->head, buf);
if (buf->trbe_limit == buf->trbe_base) {
WARN_ON(buf->trbe_base != get_trbe_base_pointer());
if (get_trbe_write_pointer() == get_trbe_base_pointer())
On Wed, Jan 27, 2021 at 02:25:35PM +0530, Anshuman Khandual wrote:
quoted
Trace Buffer Extension (TRBE) implements a trace buffer per CPU which is
accessible via the system registers. The TRBE supports different addressing
modes including CPU virtual address and buffer modes including the circular
buffer mode. The TRBE buffer is addressed by a base pointer (TRBBASER_EL1),
an write pointer (TRBPTR_EL1) and a limit pointer (TRBLIMITR_EL1). But the
access to the trace buffer could be prohibited by a higher exception level
(EL3 or EL2), indicated by TRBIDR_EL1.P. The TRBE can also generate a CPU
private interrupt (PPI) on address translation errors and when the buffer
is full. Overall implementation here is inspired from the Arm SPE driver.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
Changes in V3:
- Added new DT bindings document TRBE.yaml
- Changed TRBLIMITR_TRIG_MODE_SHIFT from 2 to 3
- Dropped isb() from trbe_reset_local()
- Dropped gap between (void *) and buf->trbe_base
- Changed 'int' to 'unsigned int' in is_trbe_available()
- Dropped unused function set_trbe_running(), set_trbe_virtual_mode(),
set_trbe_enabled() and set_trbe_limit_pointer()
- Changed get_trbe_flag_update(), is_trbe_programmable() and
get_trbe_address_align() to accept TRBIDR value
- Changed is_trbe_running(), is_trbe_abort(), is_trbe_wrap(), is_trbe_trg(),
is_trbe_irq(), get_trbe_bsc() and get_trbe_ec() to accept TRBSR value
- Dropped snapshot mode condition in arm_trbe_alloc_buffer()
- Exit arm_trbe_init() when arm64_kernel_unmapped_at_el0() is enabled
- Compute trbe_limit before trbe_write to get the updated handle
- Added trbe_stop_and_truncate_event()
- Dropped trbe_handle_fatal()
Documentation/trace/coresight/coresight-trbe.rst | 39 +
arch/arm64/include/asm/sysreg.h | 1 +
drivers/hwtracing/coresight/Kconfig | 11 +
drivers/hwtracing/coresight/Makefile | 1 +
drivers/hwtracing/coresight/coresight-trbe.c | 1023 ++++++++++++++++++++++
drivers/hwtracing/coresight/coresight-trbe.h | 160 ++++
6 files changed, 1235 insertions(+)
create mode 100644 Documentation/trace/coresight/coresight-trbe.rst
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.c
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.h
@@ -0,0 +1,39 @@+.. SPDX-License-Identifier: GPL-2.0++==============================+Trace Buffer Extension (TRBE).+==============================++:Author: Anshuman Khandual <anshuman.khandual@arm.com>+:Date: November 2020++Hardware Description+--------------------++Trace Buffer Extension (TRBE) is a percpu hardware which captures in system+memory, CPU traces generated from a corresponding percpu tracing unit. This+gets plugged in as a coresight sink device because the corresponding trace+genarators (ETE), are plugged in as source device.++The TRBE is not compliant to CoreSight architecture specifications, but is+driven via the CoreSight driver framework to support the ETE (which is+CoreSight compliant) integration.++Sysfs files and directories+---------------------------++The TRBE devices appear on the existing coresight bus alongside the other+coresight devices::++ >$ ls /sys/bus/coresight/devices+ trbe0 trbe1 trbe2 trbe3++The ``trbe<N>`` named TRBEs are associated with a CPU.::++ >$ ls /sys/bus/coresight/devices/trbe0/+ align dbm++*Key file items are:-*+*``align``: TRBE write pointer alignment+*``dbm``: TRBE updates memory with access and dirty flags+
I haven't read the TRBE progammer's manual but looking a the documentation the above
looks good.
quoted
+
+static void set_trbe_limit_pointer_enabled(unsigned long addr)
+{
+ u64 trblimitr = read_sysreg_s(SYS_TRBLIMITR_EL1);
+
+ WARN_ON(!IS_ALIGNED(addr, (1UL << TRBLIMITR_LIMIT_SHIFT)));
+ WARN_ON(!IS_ALIGNED(addr, PAGE_SIZE));
+
+ trblimitr &= ~TRBLIMITR_NVM;
+ trblimitr &= ~(TRBLIMITR_FILL_MODE_MASK << TRBLIMITR_FILL_MODE_SHIFT);
+ trblimitr &= ~(TRBLIMITR_TRIG_MODE_MASK << TRBLIMITR_TRIG_MODE_SHIFT);
+ trblimitr &= ~(TRBLIMITR_LIMIT_MASK << TRBLIMITR_LIMIT_SHIFT);
+
+ /*
+ * Fill trace buffer mode is used here while configuring the
+ * TRBE for trace capture. In this particular mode, the trace
+ * collection is stopped and a maintenance interrupt is raised
+ * when the current write pointer wraps. This pause in trace
+ * collection gives the software an opportunity to capture the
+ * trace data in the interrupt handler, before reconfiguring
+ * the TRBE.
+ */
+ trblimitr |= (TRBE_FILL_MODE_FILL & TRBLIMITR_FILL_MODE_MASK) << TRBLIMITR_FILL_MODE_SHIFT;
+
+ /*
+ * Trigger mode is not used here while configuring the TRBE for
+ * the trace capture. Hence just keep this in the ignore mode.
+ */
+ trblimitr |= (TRBE_TRIG_MODE_IGNORE & TRBLIMITR_TRIG_MODE_MASK) << TRBLIMITR_TRIG_MODE_SHIFT;
+ trblimitr |= (addr & PAGE_MASK);
+
+ trblimitr |= TRBLIMITR_ENABLE;
+ write_sysreg_s(trblimitr, SYS_TRBLIMITR_EL1);
+}
Same here
quoted
+
+static void trbe_enable_hw(struct trbe_buf *buf)
+{
+ WARN_ON(buf->trbe_write < buf->trbe_base);
+ WARN_ON(buf->trbe_write >= buf->trbe_limit);
+ set_trbe_disabled();
+ isb();
+ clr_trbe_status();
+ set_trbe_base_pointer(buf->trbe_base);
+ set_trbe_write_pointer(buf->trbe_write);
+
+ /*
+ * Synchronize all the register updates
+ * till now before enabling the TRBE.
+ */
+ isb();
+ set_trbe_limit_pointer_enabled(buf->trbe_limit);
+
+ /* Synchronize the TRBE enable event */
+ isb();
+}
Ok
quoted
+
+static void *arm_trbe_alloc_buffer(struct coresight_device *csdev,
+ struct perf_event *event, void **pages,
+ int nr_pages, bool snapshot)
+{
+ struct trbe_buf *buf;
+ struct page **pglist;
+ int i;
+
+ /*
+ * TRBE LIMIT and TRBE WRITE pointers must be page aligned. But with
+ * just a single page, there is not much room left while writing into
+ * a partially filled TRBE buffer. Hence restrict the minimum buffer
+ * size as two pages.
+ */
+ if (nr_pages < 2)
+ return NULL;
+
+ buf = kzalloc_node(sizeof(*buf), GFP_KERNEL, trbe_alloc_node(event));
+ if (IS_ERR(buf))
+ return ERR_PTR(-ENOMEM);
You know what do to.
Right, will check for NULL instead and return ERR_PTR(-ENOMEM) as
the function return type is (void *).
+
+static unsigned long arm_trbe_update_buffer(struct coresight_device *csdev,
+ struct perf_output_handle *handle,
+ void *config)
+{
+ struct trbe_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
+ struct trbe_cpudata *cpudata = dev_get_drvdata(&csdev->dev);
+ struct trbe_buf *buf = config;
+ unsigned long size, offset;
+
+ WARN_ON(buf->cpudata != cpudata);
+ WARN_ON(cpudata->cpu != smp_processor_id());
+ WARN_ON(cpudata->drvdata != drvdata);
+ if (cpudata->mode != CS_MODE_PERF)
+ return -EINVAL;
+
+ /*
+ * If the TRBE was disabled due to lack of space in the AUX buffer or a
+ * spurious fault, the driver leaves it disabled, truncating the buffer.
+ * Since the etm_perf driver expects to close out the AUX buffer, the
+ * driver skips it. Thus, just pass in 0 size here to indicate that the
+ * buffer was truncated.
+ */
+ if (!is_trbe_enabled())
+ return 0;
+ /*
+ * perf handle structure needs to be shared with the TRBE IRQ handler for
+ * capturing trace data and restarting the handle. There is a probability
+ * of an undefined reference based crash when etm event is being stopped
+ * while a TRBE IRQ also getting processed. This happens due the release
+ * of perf handle via perf_aux_output_end() in etm_event_stop(). Stopping
+ * the TRBE here will ensure that no IRQ could be generated when the perf
+ * handle gets freed in etm_event_stop().
+ */
+ trbe_drain_and_disable_local();
+ offset = get_trbe_write_pointer() - get_trbe_base_pointer();
+ size = offset - PERF_IDX2OFF(handle->head, buf);
+ if (buf->snapshot)
+ handle->head += size;
+ return size;
+}
+{
+ u64 trbsr = read_sysreg_s(SYS_TRBSR_EL1);
+ int ec = get_trbe_ec(trbsr);
+ int bsc = get_trbe_bsc(trbsr);
+
+ WARN_ON(is_trbe_running(trbsr));
+ if (is_trbe_trg(trbsr) || is_trbe_abort(trbsr))
+ return TRBE_FAULT_ACT_FATAL;
+
+ if ((ec == TRBE_EC_STAGE1_ABORT) || (ec == TRBE_EC_STAGE2_ABORT))
+ return TRBE_FAULT_ACT_FATAL;
+
+ if (is_trbe_wrap(trbsr) && (ec == TRBE_EC_OTHERS) && (bsc == TRBE_BSC_FILLED)) {
+ if (get_trbe_write_pointer() == get_trbe_base_pointer())
+ return TRBE_FAULT_ACT_WRAP;
+ }
+ return TRBE_FAULT_ACT_SPURIOUS;
+}
+
+static irqreturn_t arm_trbe_irq_handler(int irq, void *dev)
+{
+ struct perf_output_handle **handle_ptr = dev;
+ struct perf_output_handle *handle = *handle_ptr;
+ enum trbe_fault_action act;
+
+ WARN_ON(!is_trbe_irq(read_sysreg_s(SYS_TRBSR_EL1)));
+ clr_trbe_irq();
+
+ /*
+ * Ensure the trace is visible to the CPUs and
+ * any external aborts have been resolved.
+ */
+ trbe_drain_buffer();
+ isb();
+
+ if (!perf_get_aux(handle))
+ return IRQ_NONE;
+
+ if (!is_perf_trbe(handle))
+ return IRQ_NONE;
+
+ irq_work_run();
I trust Will that this is the right thing to do.
I will stop here for this revision. I will dive more in the mechanic of the
TRBE on the next revision.
Okay, will collate all the changes till now and respin sooner.
- Anshuman
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: Mike Leach <hidden> Date: 2021-02-15 13:22:15
HI,
On Tue, 2 Feb 2021 at 18:56, Mathieu Poirier [off-list ref] wrote:
On Wed, Jan 27, 2021 at 02:25:29PM +0530, Anshuman Khandual wrote:
quoted
From: Suzuki K Poulose <suzuki.poulose@arm.com>
Add ETE as one of the supported device types we support
with ETM4x driver. The devices are named following the
existing convention as ete<N>.
ETE mandates that the trace resource status register is programmed
before the tracing is turned on. For the moment simply write to
it indicating TraceActive.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
drivers/hwtracing/coresight/Kconfig | 10 ++--
drivers/hwtracing/coresight/coresight-etm4x-core.c | 56 +++++++++++++++++-----
.../hwtracing/coresight/coresight-etm4x-sysfs.c | 19 ++++++--
drivers/hwtracing/coresight/coresight-etm4x.h | 16 ++++++-
4 files changed, 79 insertions(+), 22 deletions(-)
@@ -431,6 +431,13 @@ static int etm4_enable_hw(struct etmv4_drvdata *drvdata)etm4x_relaxed_write32(csa,trcpdcr|TRCPDCR_PU,TRCPDCR);}+/*+*ETEmandatesthattheTRCRSRiswrittentobefore+*enablingit.+*/+if(etm4x_is_ete(drvdata))+etm4x_relaxed_write32(csa,TRCRSR_TA,TRCRSR);+/* Enable the trace unit */etm4x_relaxed_write32(csa,1,TRCPRGCTLR);
+ major = ETM_ARCH_MAJOR_VERSION(drvdata->arch);
+ minor = ETM_ARCH_MINOR_VERSION(drvdata->arch);
And here too. Othersiwe it makes a big blob in the middle of the function.
quoted
+ if (etm4x_is_ete(drvdata)) {
+ type_name = "ete";
+ /* ETE v1 has major version == 5. Adjust this for logging.*/
+ major -= 4;
I don't have the documentation for the ETE but I would not adjust @major. I
would simply leave it to what the HW gives us since regardless of the name, the
major revision of the IP block is 5.
For consistency this adjustment should be retained. All the prior
drivers reported the protocol version related to this.
For ETM3.x this value was 0x2 - logging printed "ETMv3.x", for PTM
this value was 0x3 - logging printed "PTM 1.x".
Only for ETMv4 is this value the same as the protocol version.
Mike
--
Mike Leach
Principal Engineer, ARM Ltd.
Manchester Design Centre. UK
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: Mike Leach <hidden> Date: 2021-02-15 14:09:49
Reviewed-by: mike.leach <redacted>
On Wed, 27 Jan 2021 at 08:55, Anshuman Khandual
[off-list ref] wrote:
quoted hunk
From: Suzuki K Poulose <suzuki.poulose@arm.com>
ETE may not implement the OS lock and instead could rely on
the PE OS Lock for the trace unit access. This is indicated
by the TRCOLSR.OSM == 0b100. Add support for handling the
PE OS lock
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
drivers/hwtracing/coresight/coresight-etm4x-core.c | 50 ++++++++++++++++++----
drivers/hwtracing/coresight/coresight-etm4x.h | 15 +++++++
2 files changed, 56 insertions(+), 9 deletions(-)
@@ -114,30 +114,59 @@ void etm4x_sysreg_write(u64 val, u32 offset, bool _relaxed, bool _64bit)}}-staticvoidetm4_os_unlock_csa(structetmv4_drvdata*drvdata,structcsdev_access*csa)+staticvoidetm_detect_os_lock(structetmv4_drvdata*drvdata,+structcsdev_access*csa){-/* Writing 0 to TRCOSLAR unlocks the trace registers */-etm4x_relaxed_write32(csa,0x0,TRCOSLAR);-drvdata->os_unlock=true;+u32oslsr=etm4x_relaxed_read32(csa,TRCOSLSR);++drvdata->os_lock_model=ETM_OSLSR_OSLM(oslsr);+}++staticvoidetm_write_os_lock(structetmv4_drvdata*drvdata,+structcsdev_access*csa,u32val)+{+val=!!val;++switch(drvdata->os_lock_model){+caseETM_OSLOCK_PRESENT:+etm4x_relaxed_write32(csa,val,TRCOSLAR);+break;+caseETM_OSLOCK_PE:+write_sysreg_s(val,SYS_OSLAR_EL1);+break;+default:+pr_warn_once("CPU%d: Unsupported Trace OSLock model: %x\n",+smp_processor_id(),drvdata->os_lock_model);+fallthrough;+caseETM_OSLOCK_NI:+return;+}isb();}+staticinlinevoidetm4_os_unlock_csa(structetmv4_drvdata*drvdata,+structcsdev_access*csa)+{+WARN_ON(drvdata->cpu!=smp_processor_id());++/* Writing 0 to OS Lock unlocks the trace unit registers */+etm_write_os_lock(drvdata,csa,0x0);+drvdata->os_unlock=true;+}+staticvoidetm4_os_unlock(structetmv4_drvdata*drvdata){if(!WARN_ON(!drvdata->csdev))etm4_os_unlock_csa(drvdata,&drvdata->csdev->access);-}staticvoidetm4_os_lock(structetmv4_drvdata*drvdata){if(WARN_ON(!drvdata->csdev))return;--/* Writing 0x1 to TRCOSLAR locks the trace registers */-etm4x_relaxed_write32(&drvdata->csdev->access,0x1,TRCOSLAR);+/* Writing 0x1 to OS Lock locks the trace registers */+etm_write_os_lock(drvdata,&drvdata->csdev->access,0x1);drvdata->os_unlock=false;-isb();}staticvoidetm4_cs_lock(structetmv4_drvdata*drvdata,
@@ -906,6 +935,9 @@ static void etm4_init_arch_data(void *info)if(!etm4_init_csdev_access(drvdata,csa))return;+/* Detect the support for OS Lock before we actuall use it */+etm_detect_os_lock(drvdata,csa);+/* Make sure all registers are accessible */etm4_os_unlock_csa(drvdata,csa);etm4_cs_unlock(drvdata,csa);
--
Mike Leach
Principal Engineer, ARM Ltd.
Manchester Design Centre. UK
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: Mike Leach <hidden> Date: 2021-02-15 14:09:49
Reviewed-by: Mike Leach <redacted>
On Mon, 15 Feb 2021 at 13:21, Mike Leach [off-list ref] wrote:
HI,
On Tue, 2 Feb 2021 at 18:56, Mathieu Poirier [off-list ref] wrote:
quoted
On Wed, Jan 27, 2021 at 02:25:29PM +0530, Anshuman Khandual wrote:
quoted
From: Suzuki K Poulose <suzuki.poulose@arm.com>
Add ETE as one of the supported device types we support
with ETM4x driver. The devices are named following the
existing convention as ete<N>.
ETE mandates that the trace resource status register is programmed
before the tracing is turned on. For the moment simply write to
it indicating TraceActive.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
drivers/hwtracing/coresight/Kconfig | 10 ++--
drivers/hwtracing/coresight/coresight-etm4x-core.c | 56 +++++++++++++++++-----
.../hwtracing/coresight/coresight-etm4x-sysfs.c | 19 ++++++--
drivers/hwtracing/coresight/coresight-etm4x.h | 16 ++++++-
4 files changed, 79 insertions(+), 22 deletions(-)
@@ -431,6 +431,13 @@ static int etm4_enable_hw(struct etmv4_drvdata *drvdata)etm4x_relaxed_write32(csa,trcpdcr|TRCPDCR_PU,TRCPDCR);}+/*+*ETEmandatesthattheTRCRSRiswrittentobefore+*enablingit.+*/+if(etm4x_is_ete(drvdata))+etm4x_relaxed_write32(csa,TRCRSR_TA,TRCRSR);+/* Enable the trace unit */etm4x_relaxed_write32(csa,1,TRCPRGCTLR);
+ major = ETM_ARCH_MAJOR_VERSION(drvdata->arch);
+ minor = ETM_ARCH_MINOR_VERSION(drvdata->arch);
And here too. Othersiwe it makes a big blob in the middle of the function.
quoted
+ if (etm4x_is_ete(drvdata)) {
+ type_name = "ete";
+ /* ETE v1 has major version == 5. Adjust this for logging.*/
+ major -= 4;
I don't have the documentation for the ETE but I would not adjust @major. I
would simply leave it to what the HW gives us since regardless of the name, the
major revision of the IP block is 5.
For consistency this adjustment should be retained. All the prior
drivers reported the protocol version related to this.
For ETM3.x this value was 0x2 - logging printed "ETMv3.x", for PTM
this value was 0x3 - logging printed "PTM 1.x".
Only for ETMv4 is this value the same as the protocol version.
Mike
--
Mike Leach
Principal Engineer, ARM Ltd.
Manchester Design Centre. UK
--
Mike Leach
Principal Engineer, ARM Ltd.
Manchester Design Centre. UK
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: Mike Leach <hidden> Date: 2021-02-15 14:10:08
Reviewed-by: Mike Leach <redacted>
On Wed, 27 Jan 2021 at 08:55, Anshuman Khandual
[off-list ref] wrote:
quoted hunk
From: Suzuki K Poulose <suzuki.poulose@arm.com>
Add support for handling the system registers for Embedded Trace
Extensions (ETE). ETE shares most of the registers with ETMv4 except
for some and also adds some new registers. Re-arrange the ETMv4x list
to share the common definitions and add the ETE sysreg support.
Cc: Mike Leach <redacted>
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
drivers/hwtracing/coresight/coresight-etm4x-core.c | 32 +++++++++++++
drivers/hwtracing/coresight/coresight-etm4x.h | 52 ++++++++++++++++++----
2 files changed, 75 insertions(+), 9 deletions(-)
@@ -160,10 +162,22 @@#define CASE_NOP(__unused, x) \case(x):/* fall through */+#define ETE_ONLY_SYSREG_LIST(op, val) \+CASE_##op((val),TRCRSR)\+CASE_##op((val),TRCEXTINSELRn(1))\+CASE_##op((val),TRCEXTINSELRn(2))\+CASE_##op((val),TRCEXTINSELRn(3))+/* List of registers accessible via System instructions */-#define ETM_SYSREG_LIST(op, val) \-CASE_##op((val),TRCPRGCTLR)\+#define ETM4x_ONLY_SYSREG_LIST(op, val) \CASE_##op((val),TRCPROCSELR)\+CASE_##op((val),TRCVDCTLR)\+CASE_##op((val),TRCVDSACCTLR)\+CASE_##op((val),TRCVDARCCTLR)\+CASE_##op((val),TRCOSLAR)++#define ETM_COMMON_SYSREG_LIST(op, val) \+CASE_##op((val),TRCPRGCTLR)\CASE_##op((val),TRCSTATR)\CASE_##op((val),TRCCONFIGR)\CASE_##op((val),TRCAUXCTLR)\
--
Mike Leach
Principal Engineer, ARM Ltd.
Manchester Design Centre. UK
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: Mike Leach <hidden> Date: 2021-02-15 17:28:49
HI Anshuman
On Wed, 27 Jan 2021 at 08:55, Anshuman Khandual
[off-list ref] wrote:
Add support for dedicated sinks that are bound to individual CPUs. (e.g,
TRBE). To allow quicker access to the sink for a given CPU bound source,
keep a percpu array of the sink devices. Also, add support for building
a path to the CPU local sink from the ETM.
Really need to tighten up the terminology here - I think what you mean
is a PE architecturally defined sink - i.e. one that can be determined
by reading the feature registers on the PE, rather than an ETR which
cannot.
However, the Coresight Base System Architecture specification does
recommend a per cpu design using an ETR per CPU - now I assume that
this case is not catered for in this patch?
This adds a new percpu sink type CORESIGHT_DEV_SUBTYPE_SINK_PERCPU_SYSMEM.
This new sink type is exclusively available and can only work with percpu
source type device CORESIGHT_DEV_SUBTYPE_SOURCE_PERCPU_PROC.
CORESIGHT_DEV_SUBTYPE_SOURCE_PERCPU_PROC - this does not exist.
quoted hunk
This defines a percpu structure that accommodates a single coresight_device
which can be used to store an initialized instance from a sink driver. As
these sinks are exclusively linked and dependent on corresponding percpu
sources devices, they should also be the default sink device during a perf
session.
Outwards device connections are scanned while establishing paths between a
source and a sink device. But such connections are not present for certain
percpu source and sink devices which are exclusively linked and dependent.
Build the path directly and skip connection scanning for such devices.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
Changes in V3:
- Updated coresight_find_default_sink()
drivers/hwtracing/coresight/coresight-core.c | 16 ++++++++++++++--
include/linux/coresight.h | 12 ++++++++++++
2 files changed, 26 insertions(+), 2 deletions(-)
If you do indeed mean the architecturally defined sinks then this
could be 'csdev_pe_arch_sink' - or something similar to indicate the
reliance on the PE architecture, unless per-cpu ETR topologies are
also handled here.
quoted hunk
/**
* struct coresight_node - elements of a path, from source to sink
@@ -784,6 +785,13 @@ static int _coresight_build_path(struct coresight_device *csdev, if (csdev == sink) goto out;+ if (coresight_is_percpu_source(csdev) && coresight_is_percpu_sink(sink) &&+ sink == per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev))) {+ _coresight_build_path(sink, sink, path);+ found = true;+ goto out;+ }+ /* Not a sink - recursively explore each port found on this element */ for (i = 0; i < csdev->pdata->nr_outport; i++) { struct coresight_device *child_dev;
@@ -999,8 +1007,12 @@ coresight_find_default_sink(struct coresight_device *csdev) int depth = 0; /* look for a default sink if we have not found for this device */- if (!csdev->def_sink)- csdev->def_sink = coresight_find_sink(csdev, &depth);+ if (!csdev->def_sink) {+ if (coresight_is_percpu_source(csdev))+ csdev->def_sink = per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev));+ if (!csdev->def_sink)+ csdev->def_sink = coresight_find_sink(csdev, &depth);+ } return csdev->def_sink; }
All cpu sources are per cpu - that is ETMv3, ETMv4, PTM, ETE - this
might be better as simply coresight_is_cpu_source() as all the
aforementioned types will return true.
Regards
Mike
--
Mike Leach
Principal Engineer, ARM Ltd.
Manchester Design Centre. UK
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: Mike Leach <hidden> Date: 2021-02-15 17:28:52
On Wed, 27 Jan 2021 at 08:55, Anshuman Khandual
[off-list ref] wrote:
quoted hunk
From: Suzuki K Poulose <suzuki.poulose@arm.com>
The context associated with an ETM for a given perf event
includes :
- handle -> the perf output handle for the AUX buffer.
- the path for the trace components
- the buffer config for the sink.
The path and the buffer config are part of the "aux_priv" data
(etm_event_data) setup by the setup_aux() callback, and made available
via perf_get_aux(handle).
Now with a sink supporting IRQ, the sink could "end" an output
handle when the buffer reaches the programmed limit and would try
to restart a handle. This could fail if there is not enough
space left the AUX buffer (e.g, the userspace has not consumed
the data). This leaves the "handle" disconnected from the "event"
and also the "perf_get_aux()" cleared. This all happens within
the sink driver, without the etm_perf driver being aware.
Now when the event is actually stopped, etm_event_stop()
will need to access the "event_data". But since the handle
is not valid anymore, we loose the information to stop the
"trace" path. So, we need a reliable way to access the etm_event_data
even when the handle may not be active.
This patch replaces the per_cpu handle array with a per_cpu context
for the ETM, which tracks the "handle" as well as the "etm_event_data".
The context notes the etm_event_data at etm_event_start() and clears
it at etm_event_stop(). This makes sure that we don't access a
stale "etm_event_data" as we are guaranteed that it is not
freed by free_aux() as long as the event is active and tracing,
also provides us with access to the critical information
needed to wind up a session even in the absence of an active
output_handle.
This is not an issue for the legacy sinks as none of them supports
an IRQ and is centrally handled by the etm-perf.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Anshuman Khandual <redacted>
Cc: Leo Yan <redacted>
Cc: Mike Leach <redacted>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
drivers/hwtracing/coresight/coresight-etm-perf.c | 45 +++++++++++++++++++++---
1 file changed, 40 insertions(+), 5 deletions(-)
@@ -374,6 +394,8 @@ static void etm_event_start(struct perf_event *event, int flags)if(source_ops(csdev)->enable(csdev,event,CS_MODE_PERF))gotofail_disable_path;+/* Save the event_data for this ETM */+ctxt->event_data=event_data;out:return;
@@ -392,13 +414,20 @@ static void etm_event_stop(struct perf_event *event, int mode)intcpu=smp_processor_id();unsignedlongsize;structcoresight_device*sink,*csdev=per_cpu(csdev_src,cpu);-structperf_output_handle*handle=this_cpu_ptr(&ctx_handle);-structetm_event_data*event_data=perf_get_aux(handle);+structetm_ctxt*ctxt=this_cpu_ptr(&etm_ctxt);+structperf_output_handle*handle=&ctxt->handle;+structetm_event_data*event_data=ctxt->event_data;structlist_head*path;+/* Clear the event_data as this ETM is stopping the trace. */+ctxt->event_data=NULL;if(event->hw.state==PERF_HES_STOPPED)return;+/* We must have a valid event_data for a running event */+if(WARN_ON(!event_data))+return;+if(!csdev)return;
@@ -416,7 +445,13 @@ static void etm_event_stop(struct perf_event *event, int mode)/* tell the core */event->hw.state=PERF_HES_STOPPED;-if(mode&PERF_EF_UPDATE){+/*+*Ifthehandleisnotboundtoaneventanymore+*(e.g,thesinkdriverwasunabletorestartthe+*handleduetolackofbufferspace),wedon't+*havetodoanythinghere.+*/+if(handle->event&&(mode&PERF_EF_UPDATE)){if(WARN_ON_ONCE(handle->event!=event))return;--
2.7.4
Reviewed-by: Mike Leach <redacted>
--
Mike Leach
Principal Engineer, ARM Ltd.
Manchester Design Centre. UK
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
On Mon, Feb 15, 2021 at 04:27:26PM +0000, Mike Leach wrote:
HI Anshuman
On Wed, 27 Jan 2021 at 08:55, Anshuman Khandual
[off-list ref] wrote:
quoted
Add support for dedicated sinks that are bound to individual CPUs. (e.g,
TRBE). To allow quicker access to the sink for a given CPU bound source,
keep a percpu array of the sink devices. Also, add support for building
a path to the CPU local sink from the ETM.
Really need to tighten up the terminology here - I think what you mean
is a PE architecturally defined sink - i.e. one that can be determined
by reading the feature registers on the PE, rather than an ETR which
cannot.
However, the Coresight Base System Architecture specification does
recommend a per cpu design using an ETR per CPU - now I assume that
this case is not catered for in this patch?
quoted
This adds a new percpu sink type CORESIGHT_DEV_SUBTYPE_SINK_PERCPU_SYSMEM.
This new sink type is exclusively available and can only work with percpu
source type device CORESIGHT_DEV_SUBTYPE_SOURCE_PERCPU_PROC.
CORESIGHT_DEV_SUBTYPE_SOURCE_PERCPU_PROC - this does not exist.
quoted
This defines a percpu structure that accommodates a single coresight_device
which can be used to store an initialized instance from a sink driver. As
these sinks are exclusively linked and dependent on corresponding percpu
sources devices, they should also be the default sink device during a perf
session.
Outwards device connections are scanned while establishing paths between a
source and a sink device. But such connections are not present for certain
percpu source and sink devices which are exclusively linked and dependent.
Build the path directly and skip connection scanning for such devices.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
Changes in V3:
- Updated coresight_find_default_sink()
drivers/hwtracing/coresight/coresight-core.c | 16 ++++++++++++++--
include/linux/coresight.h | 12 ++++++++++++
2 files changed, 26 insertions(+), 2 deletions(-)
If you do indeed mean the architecturally defined sinks then this
could be 'csdev_pe_arch_sink' - or something similar to indicate the
reliance on the PE architecture, unless per-cpu ETR topologies are
also handled here.
I would like to treat systems with one ETR per CPU the same way we do for TRBEs.
That way we have two distinct way of working, i.e topologies where the sink is
shared and 1:1 topologies. As such moving forward with "csdev_pe_arch_sink"
could become misleading when 1:1 ETR topologies are supported.
Mathieu
quoted
/**
* struct coresight_node - elements of a path, from source to sink
@@ -784,6 +785,13 @@ static int _coresight_build_path(struct coresight_device *csdev, if (csdev == sink) goto out;+ if (coresight_is_percpu_source(csdev) && coresight_is_percpu_sink(sink) &&+ sink == per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev))) {+ _coresight_build_path(sink, sink, path);+ found = true;+ goto out;+ }+ /* Not a sink - recursively explore each port found on this element */ for (i = 0; i < csdev->pdata->nr_outport; i++) { struct coresight_device *child_dev;
@@ -999,8 +1007,12 @@ coresight_find_default_sink(struct coresight_device *csdev) int depth = 0; /* look for a default sink if we have not found for this device */- if (!csdev->def_sink)- csdev->def_sink = coresight_find_sink(csdev, &depth);+ if (!csdev->def_sink) {+ if (coresight_is_percpu_source(csdev))+ csdev->def_sink = per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev));+ if (!csdev->def_sink)+ csdev->def_sink = coresight_find_sink(csdev, &depth);+ } return csdev->def_sink; }
All cpu sources are per cpu - that is ETMv3, ETMv4, PTM, ETE - this
might be better as simply coresight_is_cpu_source() as all the
aforementioned types will return true.
From: Mike Leach <hidden> Date: 2021-02-15 17:59:34
Hi Mathieu,
On Mon, 15 Feb 2021 at 16:56, Mathieu Poirier
[off-list ref] wrote:
On Mon, Feb 15, 2021 at 04:27:26PM +0000, Mike Leach wrote:
quoted
HI Anshuman
On Wed, 27 Jan 2021 at 08:55, Anshuman Khandual
[off-list ref] wrote:
quoted
Add support for dedicated sinks that are bound to individual CPUs. (e.g,
TRBE). To allow quicker access to the sink for a given CPU bound source,
keep a percpu array of the sink devices. Also, add support for building
a path to the CPU local sink from the ETM.
Really need to tighten up the terminology here - I think what you mean
is a PE architecturally defined sink - i.e. one that can be determined
by reading the feature registers on the PE, rather than an ETR which
cannot.
However, the Coresight Base System Architecture specification does
recommend a per cpu design using an ETR per CPU - now I assume that
this case is not catered for in this patch?
quoted
This adds a new percpu sink type CORESIGHT_DEV_SUBTYPE_SINK_PERCPU_SYSMEM.
This new sink type is exclusively available and can only work with percpu
source type device CORESIGHT_DEV_SUBTYPE_SOURCE_PERCPU_PROC.
CORESIGHT_DEV_SUBTYPE_SOURCE_PERCPU_PROC - this does not exist.
quoted
This defines a percpu structure that accommodates a single coresight_device
which can be used to store an initialized instance from a sink driver. As
these sinks are exclusively linked and dependent on corresponding percpu
sources devices, they should also be the default sink device during a perf
session.
Outwards device connections are scanned while establishing paths between a
source and a sink device. But such connections are not present for certain
percpu source and sink devices which are exclusively linked and dependent.
Build the path directly and skip connection scanning for such devices.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
Changes in V3:
- Updated coresight_find_default_sink()
drivers/hwtracing/coresight/coresight-core.c | 16 ++++++++++++++--
include/linux/coresight.h | 12 ++++++++++++
2 files changed, 26 insertions(+), 2 deletions(-)
If you do indeed mean the architecturally defined sinks then this
could be 'csdev_pe_arch_sink' - or something similar to indicate the
reliance on the PE architecture, unless per-cpu ETR topologies are
also handled here.
I would like to treat systems with one ETR per CPU the same way we do for TRBEs.
That way we have two distinct way of working, i.e topologies where the sink is
shared and 1:1 topologies. As such moving forward with "csdev_pe_arch_sink"
could become misleading when 1:1 ETR topologies are supported.
Mathieu
I believe that In terms of connecting source -> sink for 1:1 ETM:ETR,
then the existing code will already work via the normal build path and
ports declarations. Suzukis changes in coresight-etm-perf to allow
multiple sinks of the same type to be active for ETE:TRBE will also
work for ETx:ETR. (at least in terms of path building - there may
still be other issues that come into play about buffers etc).
The TRBE .dts doesn''t have any ports and is as such outside this
framework. This patch appears to be making it detectable when
connecting source -> sink where we have ETE:TRBE on a given CPU - as
in the subsequent patches, the TRBE driver registers in the per cpu
sink array.
So these changes are not really related to 1:1 specifically, but the
detectability of PE architected sinks. There is a need for the per cpu
array for TRBE as there is no other way of finding them - but not for
ETR - which should work just fine without changes I think.
Regards
Mike
quoted
quoted
/**
* struct coresight_node - elements of a path, from source to sink
@@ -784,6 +785,13 @@ static int _coresight_build_path(struct coresight_device *csdev, if (csdev == sink) goto out;+ if (coresight_is_percpu_source(csdev) && coresight_is_percpu_sink(sink) &&+ sink == per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev))) {+ _coresight_build_path(sink, sink, path);+ found = true;+ goto out;+ }+ /* Not a sink - recursively explore each port found on this element */ for (i = 0; i < csdev->pdata->nr_outport; i++) { struct coresight_device *child_dev;
@@ -999,8 +1007,12 @@ coresight_find_default_sink(struct coresight_device *csdev) int depth = 0; /* look for a default sink if we have not found for this device */- if (!csdev->def_sink)- csdev->def_sink = coresight_find_sink(csdev, &depth);+ if (!csdev->def_sink) {+ if (coresight_is_percpu_source(csdev))+ csdev->def_sink = per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev));+ if (!csdev->def_sink)+ csdev->def_sink = coresight_find_sink(csdev, &depth);+ } return csdev->def_sink; }
All cpu sources are per cpu - that is ETMv3, ETMv4, PTM, ETE - this
might be better as simply coresight_is_cpu_source() as all the
aforementioned types will return true.
Regards
Mike
--
Mike Leach
Principal Engineer, ARM Ltd.
Manchester Design Centre. UK
--
Mike Leach
Principal Engineer, ARM Ltd.
Manchester Design Centre. UK
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Reviewed-by: Mike Leach <redacted>
--
Mike Leach
Principal Engineer, ARM Ltd.
Manchester Design Centre. UK
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: Mike Leach <hidden> Date: 2021-02-16 09:01:58
Hi Anshuman,
There have been plenty of detailed comments so I will restrict mine to
a few general issues:-
1) Currently there appears to be no sysfs support (I cannot see the
MODE_SYSFS constants running alongside the MODE_PERF ones present in
the other sink drivers). This is present on all other coresight
devices, and must be provided for this device. It is useful for
testing, and there are users out there who will have scripts to use
it. It is not essential it makes it into this set, but should be a
follow up set.
2) Using FILL mode for TRBE means that the trace will by definition be
lossy. Fill mode will halt collection without cleanly stopping and
flushing the source. This will result in the sink missing the last of
the data from the source as it stops. Even if taking the exception
moves into a prohibited region there is still the possibility the last
trace operations will not be seen. Further it is possible that the
last few bytes of trace will be an incomplete packet, and indeed the
start of the next buffer could contain incomplete packets too.
This operation differs from the other sinks which will only halt after
the sources have stopped and the path has been flushed. This ensures
that the latest trace is complete. The weakness with the older sinks
is the lack of interrupt meaning buffers were frequently wrapped so
that only the latest trace is available.
By using TRBE WRAP mode, with a watermark as described in the TRBE
spec, using the interrupts it is possible to approach lossless trace
in a way that is not possible with earlier ETR/ETB. This is something
that has been requested by partners since trace became available in
linux systems. (There is still a possibility of loss due to filling
the buffer completely and overflowing the watermark, but that can be
flagged).
While FILL mode trace is a good start, and suitable for some scenarios
- WRAP mode needs implementing as well.
3) Padding: To be clear, it is not safe for the decoder to run off the
end of one buffer, into the padding area and continue decoding, or
continue through the padding into the next buffer. However I believe
the buffer start / stop points are demarked by the aux_output_start /
aux_output_end calls?
With upcoming perf decode updates this should enable the decoder to
correctly be started and stopped on the buffer boundaries. The padding
is there primarily to ensure that the decoder does not synchronize
with the data stream until a genuine sync point is found.
4) TRBE needs to be a loadable module like the rest of coresight.
Regards
Mike
On Mon, 15 Feb 2021 at 09:46, Anshuman Khandual
[off-list ref] wrote:
On 2/13/21 1:56 AM, Mathieu Poirier wrote:
quoted
On Wed, Jan 27, 2021 at 02:25:35PM +0530, Anshuman Khandual wrote:
quoted
Trace Buffer Extension (TRBE) implements a trace buffer per CPU which is
accessible via the system registers. The TRBE supports different addressing
modes including CPU virtual address and buffer modes including the circular
buffer mode. The TRBE buffer is addressed by a base pointer (TRBBASER_EL1),
an write pointer (TRBPTR_EL1) and a limit pointer (TRBLIMITR_EL1). But the
access to the trace buffer could be prohibited by a higher exception level
(EL3 or EL2), indicated by TRBIDR_EL1.P. The TRBE can also generate a CPU
private interrupt (PPI) on address translation errors and when the buffer
is full. Overall implementation here is inspired from the Arm SPE driver.
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <redacted>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Anshuman Khandual <redacted>
---
Changes in V3:
- Added new DT bindings document TRBE.yaml
- Changed TRBLIMITR_TRIG_MODE_SHIFT from 2 to 3
- Dropped isb() from trbe_reset_local()
- Dropped gap between (void *) and buf->trbe_base
- Changed 'int' to 'unsigned int' in is_trbe_available()
- Dropped unused function set_trbe_running(), set_trbe_virtual_mode(),
set_trbe_enabled() and set_trbe_limit_pointer()
- Changed get_trbe_flag_update(), is_trbe_programmable() and
get_trbe_address_align() to accept TRBIDR value
- Changed is_trbe_running(), is_trbe_abort(), is_trbe_wrap(), is_trbe_trg(),
is_trbe_irq(), get_trbe_bsc() and get_trbe_ec() to accept TRBSR value
- Dropped snapshot mode condition in arm_trbe_alloc_buffer()
- Exit arm_trbe_init() when arm64_kernel_unmapped_at_el0() is enabled
- Compute trbe_limit before trbe_write to get the updated handle
- Added trbe_stop_and_truncate_event()
- Dropped trbe_handle_fatal()
Documentation/trace/coresight/coresight-trbe.rst | 39 +
arch/arm64/include/asm/sysreg.h | 1 +
drivers/hwtracing/coresight/Kconfig | 11 +
drivers/hwtracing/coresight/Makefile | 1 +
drivers/hwtracing/coresight/coresight-trbe.c | 1023 ++++++++++++++++++++++
drivers/hwtracing/coresight/coresight-trbe.h | 160 ++++
6 files changed, 1235 insertions(+)
create mode 100644 Documentation/trace/coresight/coresight-trbe.rst
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.c
create mode 100644 drivers/hwtracing/coresight/coresight-trbe.h
@@ -0,0 +1,39 @@+.. SPDX-License-Identifier: GPL-2.0++==============================+Trace Buffer Extension (TRBE).+==============================++:Author: Anshuman Khandual <anshuman.khandual@arm.com>+:Date: November 2020++Hardware Description+--------------------++Trace Buffer Extension (TRBE) is a percpu hardware which captures in system+memory, CPU traces generated from a corresponding percpu tracing unit. This+gets plugged in as a coresight sink device because the corresponding trace+genarators (ETE), are plugged in as source device.++The TRBE is not compliant to CoreSight architecture specifications, but is+driven via the CoreSight driver framework to support the ETE (which is+CoreSight compliant) integration.++Sysfs files and directories+---------------------------++The TRBE devices appear on the existing coresight bus alongside the other+coresight devices::++ >$ ls /sys/bus/coresight/devices+ trbe0 trbe1 trbe2 trbe3++The ``trbe<N>`` named TRBEs are associated with a CPU.::++ >$ ls /sys/bus/coresight/devices/trbe0/+ align dbm++*Key file items are:-*+*``align``: TRBE write pointer alignment+*``dbm``: TRBE updates memory with access and dirty flags+
I haven't read the TRBE progammer's manual but looking a the documentation the above
looks good.
quoted
+
+static void set_trbe_limit_pointer_enabled(unsigned long addr)
+{
+ u64 trblimitr = read_sysreg_s(SYS_TRBLIMITR_EL1);
+
+ WARN_ON(!IS_ALIGNED(addr, (1UL << TRBLIMITR_LIMIT_SHIFT)));
+ WARN_ON(!IS_ALIGNED(addr, PAGE_SIZE));
+
+ trblimitr &= ~TRBLIMITR_NVM;
+ trblimitr &= ~(TRBLIMITR_FILL_MODE_MASK << TRBLIMITR_FILL_MODE_SHIFT);
+ trblimitr &= ~(TRBLIMITR_TRIG_MODE_MASK << TRBLIMITR_TRIG_MODE_SHIFT);
+ trblimitr &= ~(TRBLIMITR_LIMIT_MASK << TRBLIMITR_LIMIT_SHIFT);
+
+ /*
+ * Fill trace buffer mode is used here while configuring the
+ * TRBE for trace capture. In this particular mode, the trace
+ * collection is stopped and a maintenance interrupt is raised
+ * when the current write pointer wraps. This pause in trace
+ * collection gives the software an opportunity to capture the
+ * trace data in the interrupt handler, before reconfiguring
+ * the TRBE.
+ */
+ trblimitr |= (TRBE_FILL_MODE_FILL & TRBLIMITR_FILL_MODE_MASK) << TRBLIMITR_FILL_MODE_SHIFT;
+
+ /*
+ * Trigger mode is not used here while configuring the TRBE for
+ * the trace capture. Hence just keep this in the ignore mode.
+ */
+ trblimitr |= (TRBE_TRIG_MODE_IGNORE & TRBLIMITR_TRIG_MODE_MASK) << TRBLIMITR_TRIG_MODE_SHIFT;
+ trblimitr |= (addr & PAGE_MASK);
+
+ trblimitr |= TRBLIMITR_ENABLE;
+ write_sysreg_s(trblimitr, SYS_TRBLIMITR_EL1);
+}
Same here
quoted
+
+static void trbe_enable_hw(struct trbe_buf *buf)
+{
+ WARN_ON(buf->trbe_write < buf->trbe_base);
+ WARN_ON(buf->trbe_write >= buf->trbe_limit);
+ set_trbe_disabled();
+ isb();
+ clr_trbe_status();
+ set_trbe_base_pointer(buf->trbe_base);
+ set_trbe_write_pointer(buf->trbe_write);
+
+ /*
+ * Synchronize all the register updates
+ * till now before enabling the TRBE.
+ */
+ isb();
+ set_trbe_limit_pointer_enabled(buf->trbe_limit);
+
+ /* Synchronize the TRBE enable event */
+ isb();
+}
Ok
quoted
+
+static void *arm_trbe_alloc_buffer(struct coresight_device *csdev,
+ struct perf_event *event, void **pages,
+ int nr_pages, bool snapshot)
+{
+ struct trbe_buf *buf;
+ struct page **pglist;
+ int i;
+
+ /*
+ * TRBE LIMIT and TRBE WRITE pointers must be page aligned. But with
+ * just a single page, there is not much room left while writing into
+ * a partially filled TRBE buffer. Hence restrict the minimum buffer
+ * size as two pages.
+ */
+ if (nr_pages < 2)
+ return NULL;
+
+ buf = kzalloc_node(sizeof(*buf), GFP_KERNEL, trbe_alloc_node(event));
+ if (IS_ERR(buf))
+ return ERR_PTR(-ENOMEM);
You know what do to.
Right, will check for NULL instead and return ERR_PTR(-ENOMEM) as
the function return type is (void *).
+
+static unsigned long arm_trbe_update_buffer(struct coresight_device *csdev,
+ struct perf_output_handle *handle,
+ void *config)
+{
+ struct trbe_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
+ struct trbe_cpudata *cpudata = dev_get_drvdata(&csdev->dev);
+ struct trbe_buf *buf = config;
+ unsigned long size, offset;
+
+ WARN_ON(buf->cpudata != cpudata);
+ WARN_ON(cpudata->cpu != smp_processor_id());
+ WARN_ON(cpudata->drvdata != drvdata);
+ if (cpudata->mode != CS_MODE_PERF)
+ return -EINVAL;
+
+ /*
+ * If the TRBE was disabled due to lack of space in the AUX buffer or a
+ * spurious fault, the driver leaves it disabled, truncating the buffer.
+ * Since the etm_perf driver expects to close out the AUX buffer, the
+ * driver skips it. Thus, just pass in 0 size here to indicate that the
+ * buffer was truncated.
+ */
+ if (!is_trbe_enabled())
+ return 0;
+ /*
+ * perf handle structure needs to be shared with the TRBE IRQ handler for
+ * capturing trace data and restarting the handle. There is a probability
+ * of an undefined reference based crash when etm event is being stopped
+ * while a TRBE IRQ also getting processed. This happens due the release
+ * of perf handle via perf_aux_output_end() in etm_event_stop(). Stopping
+ * the TRBE here will ensure that no IRQ could be generated when the perf
+ * handle gets freed in etm_event_stop().
+ */
+ trbe_drain_and_disable_local();
+ offset = get_trbe_write_pointer() - get_trbe_base_pointer();
+ size = offset - PERF_IDX2OFF(handle->head, buf);
+ if (buf->snapshot)
+ handle->head += size;
+ return size;
+}
+{
+ u64 trbsr = read_sysreg_s(SYS_TRBSR_EL1);
+ int ec = get_trbe_ec(trbsr);
+ int bsc = get_trbe_bsc(trbsr);
+
+ WARN_ON(is_trbe_running(trbsr));
+ if (is_trbe_trg(trbsr) || is_trbe_abort(trbsr))
+ return TRBE_FAULT_ACT_FATAL;
+
+ if ((ec == TRBE_EC_STAGE1_ABORT) || (ec == TRBE_EC_STAGE2_ABORT))
+ return TRBE_FAULT_ACT_FATAL;
+
+ if (is_trbe_wrap(trbsr) && (ec == TRBE_EC_OTHERS) && (bsc == TRBE_BSC_FILLED)) {
+ if (get_trbe_write_pointer() == get_trbe_base_pointer())
+ return TRBE_FAULT_ACT_WRAP;
+ }
+ return TRBE_FAULT_ACT_SPURIOUS;
+}
+
+static irqreturn_t arm_trbe_irq_handler(int irq, void *dev)
+{
+ struct perf_output_handle **handle_ptr = dev;
+ struct perf_output_handle *handle = *handle_ptr;
+ enum trbe_fault_action act;
+
+ WARN_ON(!is_trbe_irq(read_sysreg_s(SYS_TRBSR_EL1)));
+ clr_trbe_irq();
+
+ /*
+ * Ensure the trace is visible to the CPUs and
+ * any external aborts have been resolved.
+ */
+ trbe_drain_buffer();
+ isb();
+
+ if (!perf_get_aux(handle))
+ return IRQ_NONE;
+
+ if (!is_perf_trbe(handle))
+ return IRQ_NONE;
+
+ irq_work_run();
I trust Will that this is the right thing to do.
I will stop here for this revision. I will dive more in the mechanic of the
TRBE on the next revision.
Okay, will collate all the changes till now and respin sooner.
- Anshuman
--
Mike Leach
Principal Engineer, ARM Ltd.
Manchester Design Centre. UK
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel