1*4d2e26a3SMauro Carvalho Chehab======================== 2*4d2e26a3SMauro Carvalho ChehabPMU Event Based Branches 3*4d2e26a3SMauro Carvalho Chehab======================== 4*4d2e26a3SMauro Carvalho Chehab 5*4d2e26a3SMauro Carvalho ChehabEvent Based Branches (EBBs) are a feature which allows the hardware to 6*4d2e26a3SMauro Carvalho Chehabbranch directly to a specified user space address when certain events occur. 7*4d2e26a3SMauro Carvalho Chehab 8*4d2e26a3SMauro Carvalho ChehabThe full specification is available in Power ISA v2.07: 9*4d2e26a3SMauro Carvalho Chehab 10*4d2e26a3SMauro Carvalho Chehab https://www.power.org/documentation/power-isa-version-2-07/ 11*4d2e26a3SMauro Carvalho Chehab 12*4d2e26a3SMauro Carvalho ChehabOne type of event for which EBBs can be configured is PMU exceptions. This 13*4d2e26a3SMauro Carvalho Chehabdocument describes the API for configuring the Power PMU to generate EBBs, 14*4d2e26a3SMauro Carvalho Chehabusing the Linux perf_events API. 15*4d2e26a3SMauro Carvalho Chehab 16*4d2e26a3SMauro Carvalho Chehab 17*4d2e26a3SMauro Carvalho ChehabTerminology 18*4d2e26a3SMauro Carvalho Chehab----------- 19*4d2e26a3SMauro Carvalho Chehab 20*4d2e26a3SMauro Carvalho ChehabThroughout this document we will refer to an "EBB event" or "EBB events". This 21*4d2e26a3SMauro Carvalho Chehabjust refers to a struct perf_event which has set the "EBB" flag in its 22*4d2e26a3SMauro Carvalho Chehabattr.config. All events which can be configured on the hardware PMU are 23*4d2e26a3SMauro Carvalho Chehabpossible "EBB events". 24*4d2e26a3SMauro Carvalho Chehab 25*4d2e26a3SMauro Carvalho Chehab 26*4d2e26a3SMauro Carvalho ChehabBackground 27*4d2e26a3SMauro Carvalho Chehab---------- 28*4d2e26a3SMauro Carvalho Chehab 29*4d2e26a3SMauro Carvalho ChehabWhen a PMU EBB occurs it is delivered to the currently running process. As such 30*4d2e26a3SMauro Carvalho ChehabEBBs can only sensibly be used by programs for self-monitoring. 31*4d2e26a3SMauro Carvalho Chehab 32*4d2e26a3SMauro Carvalho ChehabIt is a feature of the perf_events API that events can be created on other 33*4d2e26a3SMauro Carvalho Chehabprocesses, subject to standard permission checks. This is also true of EBB 34*4d2e26a3SMauro Carvalho Chehabevents, however unless the target process enables EBBs (via mtspr(BESCR)) no 35*4d2e26a3SMauro Carvalho ChehabEBBs will ever be delivered. 36*4d2e26a3SMauro Carvalho Chehab 37*4d2e26a3SMauro Carvalho ChehabThis makes it possible for a process to enable EBBs for itself, but not 38*4d2e26a3SMauro Carvalho Chehabactually configure any events. At a later time another process can come along 39*4d2e26a3SMauro Carvalho Chehaband attach an EBB event to the process, which will then cause EBBs to be 40*4d2e26a3SMauro Carvalho Chehabdelivered to the first process. It's not clear if this is actually useful. 41*4d2e26a3SMauro Carvalho Chehab 42*4d2e26a3SMauro Carvalho Chehab 43*4d2e26a3SMauro Carvalho ChehabWhen the PMU is configured for EBBs, all PMU interrupts are delivered to the 44*4d2e26a3SMauro Carvalho Chehabuser process. This means once an EBB event is scheduled on the PMU, no non-EBB 45*4d2e26a3SMauro Carvalho Chehabevents can be configured. This means that EBB events can not be run 46*4d2e26a3SMauro Carvalho Chehabconcurrently with regular 'perf' commands, or any other perf events. 47*4d2e26a3SMauro Carvalho Chehab 48*4d2e26a3SMauro Carvalho ChehabIt is however safe to run 'perf' commands on a process which is using EBBs. The 49*4d2e26a3SMauro Carvalho Chehabkernel will in general schedule the EBB event, and perf will be notified that 50*4d2e26a3SMauro Carvalho Chehabits events could not run. 51*4d2e26a3SMauro Carvalho Chehab 52*4d2e26a3SMauro Carvalho ChehabThe exclusion between EBB events and regular events is implemented using the 53*4d2e26a3SMauro Carvalho Chehabexisting "pinned" and "exclusive" attributes of perf_events. This means EBB 54*4d2e26a3SMauro Carvalho Chehabevents will be given priority over other events, unless they are also pinned. 55*4d2e26a3SMauro Carvalho ChehabIf an EBB event and a regular event are both pinned, then whichever is enabled 56*4d2e26a3SMauro Carvalho Chehabfirst will be scheduled and the other will be put in error state. See the 57*4d2e26a3SMauro Carvalho Chehabsection below titled "Enabling an EBB event" for more information. 58*4d2e26a3SMauro Carvalho Chehab 59*4d2e26a3SMauro Carvalho Chehab 60*4d2e26a3SMauro Carvalho ChehabCreating an EBB event 61*4d2e26a3SMauro Carvalho Chehab--------------------- 62*4d2e26a3SMauro Carvalho Chehab 63*4d2e26a3SMauro Carvalho ChehabTo request that an event is counted using EBB, the event code should have bit 64*4d2e26a3SMauro Carvalho Chehab63 set. 65*4d2e26a3SMauro Carvalho Chehab 66*4d2e26a3SMauro Carvalho ChehabEBB events must be created with a particular, and restrictive, set of 67*4d2e26a3SMauro Carvalho Chehabattributes - this is so that they interoperate correctly with the rest of the 68*4d2e26a3SMauro Carvalho Chehabperf_events subsystem. 69*4d2e26a3SMauro Carvalho Chehab 70*4d2e26a3SMauro Carvalho ChehabAn EBB event must be created with the "pinned" and "exclusive" attributes set. 71*4d2e26a3SMauro Carvalho ChehabNote that if you are creating a group of EBB events, only the leader can have 72*4d2e26a3SMauro Carvalho Chehabthese attributes set. 73*4d2e26a3SMauro Carvalho Chehab 74*4d2e26a3SMauro Carvalho ChehabAn EBB event must NOT set any of the "inherit", "sample_period", "freq" or 75*4d2e26a3SMauro Carvalho Chehab"enable_on_exec" attributes. 76*4d2e26a3SMauro Carvalho Chehab 77*4d2e26a3SMauro Carvalho ChehabAn EBB event must be attached to a task. This is specified to perf_event_open() 78*4d2e26a3SMauro Carvalho Chehabby passing a pid value, typically 0 indicating the current task. 79*4d2e26a3SMauro Carvalho Chehab 80*4d2e26a3SMauro Carvalho ChehabAll events in a group must agree on whether they want EBB. That is all events 81*4d2e26a3SMauro Carvalho Chehabmust request EBB, or none may request EBB. 82*4d2e26a3SMauro Carvalho Chehab 83*4d2e26a3SMauro Carvalho ChehabEBB events must specify the PMC they are to be counted on. This ensures 84*4d2e26a3SMauro Carvalho Chehabuserspace is able to reliably determine which PMC the event is scheduled on. 85*4d2e26a3SMauro Carvalho Chehab 86*4d2e26a3SMauro Carvalho Chehab 87*4d2e26a3SMauro Carvalho ChehabEnabling an EBB event 88*4d2e26a3SMauro Carvalho Chehab--------------------- 89*4d2e26a3SMauro Carvalho Chehab 90*4d2e26a3SMauro Carvalho ChehabOnce an EBB event has been successfully opened, it must be enabled with the 91*4d2e26a3SMauro Carvalho Chehabperf_events API. This can be achieved either via the ioctl() interface, or the 92*4d2e26a3SMauro Carvalho Chehabprctl() interface. 93*4d2e26a3SMauro Carvalho Chehab 94*4d2e26a3SMauro Carvalho ChehabHowever, due to the design of the perf_events API, enabling an event does not 95*4d2e26a3SMauro Carvalho Chehabguarantee that it has been scheduled on the PMU. To ensure that the EBB event 96*4d2e26a3SMauro Carvalho Chehabhas been scheduled on the PMU, you must perform a read() on the event. If the 97*4d2e26a3SMauro Carvalho Chehabread() returns EOF, then the event has not been scheduled and EBBs are not 98*4d2e26a3SMauro Carvalho Chehabenabled. 99*4d2e26a3SMauro Carvalho Chehab 100*4d2e26a3SMauro Carvalho ChehabThis behaviour occurs because the EBB event is pinned and exclusive. When the 101*4d2e26a3SMauro Carvalho ChehabEBB event is enabled it will force all other non-pinned events off the PMU. In 102*4d2e26a3SMauro Carvalho Chehabthis case the enable will be successful. However if there is already an event 103*4d2e26a3SMauro Carvalho Chehabpinned on the PMU then the enable will not be successful. 104*4d2e26a3SMauro Carvalho Chehab 105*4d2e26a3SMauro Carvalho Chehab 106*4d2e26a3SMauro Carvalho ChehabReading an EBB event 107*4d2e26a3SMauro Carvalho Chehab-------------------- 108*4d2e26a3SMauro Carvalho Chehab 109*4d2e26a3SMauro Carvalho ChehabIt is possible to read() from an EBB event. However the results are 110*4d2e26a3SMauro Carvalho Chehabmeaningless. Because interrupts are being delivered to the user process the 111*4d2e26a3SMauro Carvalho Chehabkernel is not able to count the event, and so will return a junk value. 112*4d2e26a3SMauro Carvalho Chehab 113*4d2e26a3SMauro Carvalho Chehab 114*4d2e26a3SMauro Carvalho ChehabClosing an EBB event 115*4d2e26a3SMauro Carvalho Chehab-------------------- 116*4d2e26a3SMauro Carvalho Chehab 117*4d2e26a3SMauro Carvalho ChehabWhen an EBB event is finished with, you can close it using close() as for any 118*4d2e26a3SMauro Carvalho Chehabregular event. If this is the last EBB event the PMU will be deconfigured and 119*4d2e26a3SMauro Carvalho Chehabno further PMU EBBs will be delivered. 120*4d2e26a3SMauro Carvalho Chehab 121*4d2e26a3SMauro Carvalho Chehab 122*4d2e26a3SMauro Carvalho ChehabEBB Handler 123*4d2e26a3SMauro Carvalho Chehab----------- 124*4d2e26a3SMauro Carvalho Chehab 125*4d2e26a3SMauro Carvalho ChehabThe EBB handler is just regular userspace code, however it must be written in 126*4d2e26a3SMauro Carvalho Chehabthe style of an interrupt handler. When the handler is entered all registers 127*4d2e26a3SMauro Carvalho Chehabare live (possibly) and so must be saved somehow before the handler can invoke 128*4d2e26a3SMauro Carvalho Chehabother code. 129*4d2e26a3SMauro Carvalho Chehab 130*4d2e26a3SMauro Carvalho ChehabIt's up to the program how to handle this. For C programs a relatively simple 131*4d2e26a3SMauro Carvalho Chehaboption is to create an interrupt frame on the stack and save registers there. 132*4d2e26a3SMauro Carvalho Chehab 133*4d2e26a3SMauro Carvalho ChehabFork 134*4d2e26a3SMauro Carvalho Chehab---- 135*4d2e26a3SMauro Carvalho Chehab 136*4d2e26a3SMauro Carvalho ChehabEBB events are not inherited across fork. If the child process wishes to use 137*4d2e26a3SMauro Carvalho ChehabEBBs it should open a new event for itself. Similarly the EBB state in 138*4d2e26a3SMauro Carvalho ChehabBESCR/EBBHR/EBBRR is cleared across fork(). 139