• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2011 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Ben Widawsky <ben@bwidawsk.net>
25  *
26  */
27 
28 #include <assert.h>
29 #include <err.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <stdbool.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <syslog.h>
36 #include <unistd.h>
37 #include "intel_io.h"
38 #include "intel_chipset.h"
39 #include "drmtest.h"
40 
41 bool daemonized;
42 
43 #define INFO_PRINT(...) \
44 	do { \
45 		if (daemonized) \
46 			syslog(LOG_INFO, ##__VA_ARGS__); \
47 		else \
48 			fprintf(stdout, ##__VA_ARGS__); \
49 	} while(0)
50 
51 static void
help(char * prog)52 help(char *prog) {
53 	printf("%s Prevents the GT from sleeping.\n\n", prog);
54 	printf("usage: %s [options] \n\n", prog);
55 	printf("Options: \n");
56 	printf("    -b        Run in background/daemon mode\n");
57 }
58 
59 static int
is_alive(void)60 is_alive(void) {
61 	/* Read the timestamp, which should *almost* always be !0 */
62 	return (intel_register_read(0x2358) != 0);
63 }
64 
main(int argc,char * argv[])65 int main(int argc, char *argv[])
66 {
67 	int ret;
68 
69 	if (argc > 2 || (argc == 2 && !strncmp(argv[1], "-h", 2))) {
70 		help(argv[1]);
71 		exit(0);
72 	}
73 
74 	if (argc == 2 && (!strncmp(argv[1], "-b", 2)))
75 		daemonized = true;
76 
77 	if (daemonized) {
78 		assert(daemon(0, 0) == 0);
79 		openlog(argv[0], LOG_CONS | LOG_PID, LOG_USER);
80 		INFO_PRINT("started daemon");
81 	}
82 
83 	ret = intel_register_access_init(intel_get_pci_device(), 1, -1);
84 	if (ret) {
85 		INFO_PRINT("Couldn't init register access\n");
86 		exit(1);
87 	} else {
88 		INFO_PRINT("Forcewake locked\n");
89 	}
90 	while(1) {
91 		if (!is_alive()) {
92 			INFO_PRINT("gpu reset? restarting daemon\n");
93 			intel_register_access_fini();
94 			ret = intel_register_access_init(intel_get_pci_device(), 1, -1);
95 			if (ret)
96 				INFO_PRINT("Reg access init fail\n");
97 		}
98 		sleep(1);
99 	}
100 	intel_register_access_fini();
101 	INFO_PRINT("Forcewake unlock\n");
102 
103 	if (daemonized) {
104 		INFO_PRINT("finished\n");
105 		closelog();
106 	}
107 
108 	return 0;
109 }
110