summaryrefslogtreecommitdiff
path: root/tools/intel_l3_udev_listener.c
blob: 0b94c1c55c75607b8ab0eeee5c2914e08e5b991b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * Copyright © 2013 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#if HAVE_UDEV
#include <libudev.h>
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <syslog.h>
#include "i915_drm.h"
#include "intel_l3_parity.h"

#ifndef I915_L3_PARITY_UEVENT
#define I915_L3_PARITY_UEVENT "L3_PARITY_ERROR"
#endif

int l3_uevent_setup(struct l3_parity *par)
{
	struct udev *udev;
	struct udev_monitor *uevent_monitor;
	fd_set fdset;
	int fd, ret = -1;

	udev = udev_new();
	if (!udev) {
		return -1;
	}

	uevent_monitor = udev_monitor_new_from_netlink(udev, "udev");
	if (!uevent_monitor)
		goto err_out;

	ret = udev_monitor_filter_add_match_subsystem_devtype(uevent_monitor, "drm", "drm_minor");
	if (ret < 0)
		goto err_out;

	ret = udev_monitor_enable_receiving(uevent_monitor);
	if (ret < 0)
		goto err_out;

	fd = udev_monitor_get_fd(uevent_monitor);
	FD_ZERO(&fdset);
	FD_SET(fd, &fdset);

	par->udev = udev;
	par->fd = fd;
	par->fdset = fdset;
	par->uevent_monitor = uevent_monitor;
	return 0;

err_out:
	udev_unref(udev);
	return ret;
}

int l3_listen(struct l3_parity *par, bool daemon, struct l3_location *loc)
{
	struct udev_device *udev_dev;
	const char *parity_status;
	char *err_msg;
	int ret;

again:
	ret = select(par->fd + 1, &par->fdset, NULL, NULL, NULL);
	/* Number of bits set is returned, must be >= 1 */
	if (ret <= 0) {
		return ret;
	}

	assert(FD_ISSET(par->fd, &par->fdset));

	udev_dev = udev_monitor_receive_device(par->uevent_monitor);
	if (!udev_dev)
		return -1;

	parity_status = udev_device_get_property_value(udev_dev, I915_L3_PARITY_UEVENT);
	if (strncmp(parity_status, "1", 1))
		goto again;

	loc->slice = atoi(udev_device_get_property_value(udev_dev, "SLICE"));
	loc->row = atoi(udev_device_get_property_value(udev_dev, "ROW"));
	loc->bank = atoi(udev_device_get_property_value(udev_dev, "BANK"));
	loc->subbank = atoi(udev_device_get_property_value(udev_dev, "SUBBANK"));

	udev_device_unref(udev_dev);

	assert(asprintf(&err_msg, "Parity error detected on: %d,%d,%d,%d. "
			"Try to run intel_l3_parity -r %d -b %d -s %d -w %d -d",
			loc->slice, loc->row, loc->bank, loc->subbank,
			loc->row, loc->bank, loc->subbank, loc->slice) != -1);
	if (daemon) {
		syslog(LOG_INFO, "%s\n", err_msg);
		goto again;
	}

	fprintf(stderr, "%s\n", err_msg);

	free(err_msg);

	return 0;
}
#endif