18c2ecf20Sopenharmony_ci========================
28c2ecf20Sopenharmony_ciPMU Event Based Branches
38c2ecf20Sopenharmony_ci========================
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ciEvent Based Branches (EBBs) are a feature which allows the hardware to
68c2ecf20Sopenharmony_cibranch directly to a specified user space address when certain events occur.
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ciThe full specification is available in Power ISA v2.07:
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci  https://www.power.org/documentation/power-isa-version-2-07/
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ciOne type of event for which EBBs can be configured is PMU exceptions. This
138c2ecf20Sopenharmony_cidocument describes the API for configuring the Power PMU to generate EBBs,
148c2ecf20Sopenharmony_ciusing the Linux perf_events API.
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ciTerminology
188c2ecf20Sopenharmony_ci-----------
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ciThroughout this document we will refer to an "EBB event" or "EBB events". This
218c2ecf20Sopenharmony_cijust refers to a struct perf_event which has set the "EBB" flag in its
228c2ecf20Sopenharmony_ciattr.config. All events which can be configured on the hardware PMU are
238c2ecf20Sopenharmony_cipossible "EBB events".
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ciBackground
278c2ecf20Sopenharmony_ci----------
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ciWhen a PMU EBB occurs it is delivered to the currently running process. As such
308c2ecf20Sopenharmony_ciEBBs can only sensibly be used by programs for self-monitoring.
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ciIt is a feature of the perf_events API that events can be created on other
338c2ecf20Sopenharmony_ciprocesses, subject to standard permission checks. This is also true of EBB
348c2ecf20Sopenharmony_cievents, however unless the target process enables EBBs (via mtspr(BESCR)) no
358c2ecf20Sopenharmony_ciEBBs will ever be delivered.
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ciThis makes it possible for a process to enable EBBs for itself, but not
388c2ecf20Sopenharmony_ciactually configure any events. At a later time another process can come along
398c2ecf20Sopenharmony_ciand attach an EBB event to the process, which will then cause EBBs to be
408c2ecf20Sopenharmony_cidelivered to the first process. It's not clear if this is actually useful.
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ciWhen the PMU is configured for EBBs, all PMU interrupts are delivered to the
448c2ecf20Sopenharmony_ciuser process. This means once an EBB event is scheduled on the PMU, no non-EBB
458c2ecf20Sopenharmony_cievents can be configured. This means that EBB events can not be run
468c2ecf20Sopenharmony_ciconcurrently with regular 'perf' commands, or any other perf events.
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ciIt is however safe to run 'perf' commands on a process which is using EBBs. The
498c2ecf20Sopenharmony_cikernel will in general schedule the EBB event, and perf will be notified that
508c2ecf20Sopenharmony_ciits events could not run.
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ciThe exclusion between EBB events and regular events is implemented using the
538c2ecf20Sopenharmony_ciexisting "pinned" and "exclusive" attributes of perf_events. This means EBB
548c2ecf20Sopenharmony_cievents will be given priority over other events, unless they are also pinned.
558c2ecf20Sopenharmony_ciIf an EBB event and a regular event are both pinned, then whichever is enabled
568c2ecf20Sopenharmony_cifirst will be scheduled and the other will be put in error state. See the
578c2ecf20Sopenharmony_cisection below titled "Enabling an EBB event" for more information.
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ciCreating an EBB event
618c2ecf20Sopenharmony_ci---------------------
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ciTo request that an event is counted using EBB, the event code should have bit
648c2ecf20Sopenharmony_ci63 set.
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ciEBB events must be created with a particular, and restrictive, set of
678c2ecf20Sopenharmony_ciattributes - this is so that they interoperate correctly with the rest of the
688c2ecf20Sopenharmony_ciperf_events subsystem.
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ciAn EBB event must be created with the "pinned" and "exclusive" attributes set.
718c2ecf20Sopenharmony_ciNote that if you are creating a group of EBB events, only the leader can have
728c2ecf20Sopenharmony_cithese attributes set.
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ciAn EBB event must NOT set any of the "inherit", "sample_period", "freq" or
758c2ecf20Sopenharmony_ci"enable_on_exec" attributes.
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ciAn EBB event must be attached to a task. This is specified to perf_event_open()
788c2ecf20Sopenharmony_ciby passing a pid value, typically 0 indicating the current task.
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ciAll events in a group must agree on whether they want EBB. That is all events
818c2ecf20Sopenharmony_cimust request EBB, or none may request EBB.
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ciEBB events must specify the PMC they are to be counted on. This ensures
848c2ecf20Sopenharmony_ciuserspace is able to reliably determine which PMC the event is scheduled on.
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ciEnabling an EBB event
888c2ecf20Sopenharmony_ci---------------------
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ciOnce an EBB event has been successfully opened, it must be enabled with the
918c2ecf20Sopenharmony_ciperf_events API. This can be achieved either via the ioctl() interface, or the
928c2ecf20Sopenharmony_ciprctl() interface.
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ciHowever, due to the design of the perf_events API, enabling an event does not
958c2ecf20Sopenharmony_ciguarantee that it has been scheduled on the PMU. To ensure that the EBB event
968c2ecf20Sopenharmony_cihas been scheduled on the PMU, you must perform a read() on the event. If the
978c2ecf20Sopenharmony_ciread() returns EOF, then the event has not been scheduled and EBBs are not
988c2ecf20Sopenharmony_cienabled.
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ciThis behaviour occurs because the EBB event is pinned and exclusive. When the
1018c2ecf20Sopenharmony_ciEBB event is enabled it will force all other non-pinned events off the PMU. In
1028c2ecf20Sopenharmony_cithis case the enable will be successful. However if there is already an event
1038c2ecf20Sopenharmony_cipinned on the PMU then the enable will not be successful.
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ciReading an EBB event
1078c2ecf20Sopenharmony_ci--------------------
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ciIt is possible to read() from an EBB event. However the results are
1108c2ecf20Sopenharmony_cimeaningless. Because interrupts are being delivered to the user process the
1118c2ecf20Sopenharmony_cikernel is not able to count the event, and so will return a junk value.
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ciClosing an EBB event
1158c2ecf20Sopenharmony_ci--------------------
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ciWhen an EBB event is finished with, you can close it using close() as for any
1188c2ecf20Sopenharmony_ciregular event. If this is the last EBB event the PMU will be deconfigured and
1198c2ecf20Sopenharmony_cino further PMU EBBs will be delivered.
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ciEBB Handler
1238c2ecf20Sopenharmony_ci-----------
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ciThe EBB handler is just regular userspace code, however it must be written in
1268c2ecf20Sopenharmony_cithe style of an interrupt handler. When the handler is entered all registers
1278c2ecf20Sopenharmony_ciare live (possibly) and so must be saved somehow before the handler can invoke
1288c2ecf20Sopenharmony_ciother code.
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ciIt's up to the program how to handle this. For C programs a relatively simple
1318c2ecf20Sopenharmony_cioption is to create an interrupt frame on the stack and save registers there.
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ciFork
1348c2ecf20Sopenharmony_ci----
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ciEBB events are not inherited across fork. If the child process wishes to use
1378c2ecf20Sopenharmony_ciEBBs it should open a new event for itself. Similarly the EBB state in
1388c2ecf20Sopenharmony_ciBESCR/EBBHR/EBBRR is cleared across fork().
139