blob: a90e73e0f1b863f61a65c26b21a0846d8465c1c2 [file] [log] [blame]
Daniel Vetter9b328942012-02-11 16:52:26 +01001/*
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_gpu_tools.h"
38
39bool daemonized;
40
41#define INFO_PRINT(...) \
42 do { \
43 if (daemonized) \
44 syslog(LOG_INFO, ##__VA_ARGS__); \
45 else \
46 fprintf(stdout, ##__VA_ARGS__); \
47 } while(0)
48
49static void
50help(char *prog) {
51 printf("%s Prevents the GT from sleeping.\n\n", prog);
52 printf("usage: %s [options] \n\n", prog);
53 printf("Options: \n");
54 printf(" -b Run in background/daemon mode\n");
55}
56
57static int
58is_alive(void) {
59 /* Read the timestamp, which should *almost* always be !0 */
60 return (intel_register_read(0x2358) != 0);
61}
62
63int main(int argc, char *argv[])
64{
65 int ret;
66
67 if (argc > 2 || (argc == 2 && !strncmp(argv[1], "-h", 2))) {
68 help(argv[1]);
69 exit(0);
70 }
71
72 if (argc == 2 && (!strncmp(argv[1], "-b", 2)))
73 daemonized = true;
74
75 if (daemonized) {
76 assert(daemon(0, 0) == 0);
77 openlog(argv[0], LOG_CONS | LOG_PID, LOG_USER);
78 INFO_PRINT("started daemon");
79 }
80
81 ret = intel_register_access_init(intel_get_pci_device(), 1);
82 if (ret) {
83 INFO_PRINT("Couldn't init register access\n");
84 exit(1);
85 } else {
86 INFO_PRINT("Forcewake locked\n");
87 }
88 while(1) {
89 if (!is_alive()) {
90 INFO_PRINT("gpu reset? restarting daemon\n");
91 intel_register_access_fini();
Ben Widawskybc625672012-08-30 14:17:01 -070092 ret = intel_register_access_init(intel_get_pci_device(), 1);
93 if (ret)
94 INFO_PRINT("Reg access init fail\n");
Daniel Vetter9b328942012-02-11 16:52:26 +010095 }
96 sleep(1);
97 }
98 intel_register_access_fini();
99 INFO_PRINT("Forcewake unlock\n");
100
101 if (daemonized) {
102 INFO_PRINT("finished\n");
103 closelog();
104 }
105
106 return 0;
107}