1f50a7f3dSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
23752e453SMichael Ellerman /*
33752e453SMichael Ellerman  * Copyright 2014, Michael Ellerman, IBM Corp.
43752e453SMichael Ellerman  */
53752e453SMichael Ellerman 
63752e453SMichael Ellerman #include <signal.h>
73752e453SMichael Ellerman #include <stdio.h>
83752e453SMichael Ellerman #include <stdlib.h>
93752e453SMichael Ellerman #include <stdbool.h>
103752e453SMichael Ellerman #include <sys/types.h>
113752e453SMichael Ellerman #include <sys/wait.h>
123752e453SMichael Ellerman #include <unistd.h>
133752e453SMichael Ellerman 
143752e453SMichael Ellerman #include "ebb.h"
153752e453SMichael Ellerman 
163752e453SMichael Ellerman 
173752e453SMichael Ellerman /*
183752e453SMichael Ellerman  * Tests a per-task event vs an EBB - in that order. The EBB should push the
193752e453SMichael Ellerman  * per-task event off the PMU.
203752e453SMichael Ellerman  */
213752e453SMichael Ellerman 
setup_child_event(struct event * event,pid_t child_pid)223752e453SMichael Ellerman static int setup_child_event(struct event *event, pid_t child_pid)
233752e453SMichael Ellerman {
243752e453SMichael Ellerman 	event_init_named(event, 0x400FA, "PM_RUN_INST_CMPL");
253752e453SMichael Ellerman 
263752e453SMichael Ellerman 	event->attr.exclude_kernel = 1;
273752e453SMichael Ellerman 	event->attr.exclude_hv = 1;
283752e453SMichael Ellerman 	event->attr.exclude_idle = 1;
293752e453SMichael Ellerman 
303752e453SMichael Ellerman 	FAIL_IF(event_open_with_pid(event, child_pid));
313752e453SMichael Ellerman 	FAIL_IF(event_enable(event));
323752e453SMichael Ellerman 
333752e453SMichael Ellerman 	return 0;
343752e453SMichael Ellerman }
353752e453SMichael Ellerman 
task_event_vs_ebb(void)363752e453SMichael Ellerman int task_event_vs_ebb(void)
373752e453SMichael Ellerman {
383752e453SMichael Ellerman 	union pipe read_pipe, write_pipe;
393752e453SMichael Ellerman 	struct event event;
403752e453SMichael Ellerman 	pid_t pid;
413752e453SMichael Ellerman 	int rc;
423752e453SMichael Ellerman 
4339fcfb91SDenis Kirjanov 	SKIP_IF(!ebb_is_supported());
4439fcfb91SDenis Kirjanov 
453752e453SMichael Ellerman 	FAIL_IF(pipe(read_pipe.fds) == -1);
463752e453SMichael Ellerman 	FAIL_IF(pipe(write_pipe.fds) == -1);
473752e453SMichael Ellerman 
483752e453SMichael Ellerman 	pid = fork();
493752e453SMichael Ellerman 	if (pid == 0) {
503752e453SMichael Ellerman 		/* NB order of pipes looks reversed */
513752e453SMichael Ellerman 		exit(ebb_child(write_pipe, read_pipe));
523752e453SMichael Ellerman 	}
533752e453SMichael Ellerman 
543752e453SMichael Ellerman 	/* We setup the task event first */
553752e453SMichael Ellerman 	rc = setup_child_event(&event, pid);
563752e453SMichael Ellerman 	if (rc) {
573752e453SMichael Ellerman 		kill_child_and_wait(pid);
583752e453SMichael Ellerman 		return rc;
593752e453SMichael Ellerman 	}
603752e453SMichael Ellerman 
613752e453SMichael Ellerman 	/* Signal the child to install its EBB event and wait */
623752e453SMichael Ellerman 	if (sync_with_child(read_pipe, write_pipe))
633752e453SMichael Ellerman 		/* If it fails, wait for it to exit */
643752e453SMichael Ellerman 		goto wait;
653752e453SMichael Ellerman 
663752e453SMichael Ellerman 	/* Signal the child to run */
673752e453SMichael Ellerman 	FAIL_IF(sync_with_child(read_pipe, write_pipe));
683752e453SMichael Ellerman 
693752e453SMichael Ellerman wait:
703752e453SMichael Ellerman 	/* The EBB event should push the task event off so the child should succeed */
713752e453SMichael Ellerman 	FAIL_IF(wait_for_child(pid));
723752e453SMichael Ellerman 	FAIL_IF(event_disable(&event));
733752e453SMichael Ellerman 	FAIL_IF(event_read(&event));
743752e453SMichael Ellerman 
753752e453SMichael Ellerman 	event_report(&event);
763752e453SMichael Ellerman 
773752e453SMichael Ellerman 	/* The task event may have run, or not so we can't assert anything about it */
783752e453SMichael Ellerman 
793752e453SMichael Ellerman 	return 0;
803752e453SMichael Ellerman }
813752e453SMichael Ellerman 
main(void)823752e453SMichael Ellerman int main(void)
833752e453SMichael Ellerman {
843752e453SMichael Ellerman 	return test_harness(task_event_vs_ebb, "task_event_vs_ebb");
853752e453SMichael Ellerman }
86