summaryrefslogtreecommitdiff
path: root/src/common_vgaarb.c
blob: 7a7d204c9bc515fa3cffb7e646f19914bcc89a57 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 * Copyright (c) 2007 Paulo R. Zanoni, Tiago Vignatti
 *               2009 Tiago Vignatti
 *
 * 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 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

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <limits.h>

#include "pciaccess.h"
#include "pciaccess_private.h"

#define BUFSIZE 64

static int
parse_string_to_decodes_rsrc(char *input, int *vga_count, struct pci_slot_match *match)
{
    char *tok;
    char *input_sp = NULL, *count_sp, *pci_sp;
    char tmp[32];

    tok = strtok_r(input,",",&input_sp);
    if (!tok)
        goto fail;

    strncpy(tmp, input, 15);
    tmp[15] = 0;

    tok = strtok_r(tmp,":",&count_sp);
    if (!tok)
        goto fail;
    tok = strtok_r(NULL, ":",&count_sp);
    if (!tok)
        goto fail;

    *vga_count = strtoul(tok, NULL, 10);
    if (*vga_count == LONG_MAX)
        goto fail;

#ifdef DEBUG
    fprintf(stderr,"vga count is %d\n", *vga_count);
#endif

    tok = strtok_r(NULL, ",",&input_sp);
    if (!tok)
        goto fail;

    if (match) {
        strncpy(tmp, tok, 32);
        tmp[31] = 0;
        tok = strtok_r(tmp, ":", &pci_sp);
        if (!tok)
            goto fail;
        tok = strtok_r(NULL, ":", &pci_sp);
        if (!tok)
            goto fail;
        match->domain = strtoul(tok, NULL, 16);

        tok = strtok_r(NULL, ":", &pci_sp);
        if (!tok)
            goto fail;
        match->bus = strtoul(tok, NULL, 16);

        tok = strtok_r(NULL, ".", &pci_sp);
        if (!tok)
            goto fail;
        match->dev = strtoul(tok, NULL, 16);

        tok = strtok_r(NULL, ".", &pci_sp);
        if (!tok)
            goto fail;
        match->func = strtoul(tok, NULL, 16);
    }

    tok = strtok_r(NULL, ",",&input_sp);
    if (!tok)
        goto fail;
    tok = strtok_r(tok, "=", &input_sp);
    if (!tok)
        goto fail;
    tok = strtok_r(NULL, "=", &input_sp);
    if (!tok)
        goto fail;

    if (!strncmp(tok, "io+mem", 6))
        return VGA_ARB_RSRC_LEGACY_IO | VGA_ARB_RSRC_LEGACY_MEM;
    if (!strncmp(tok, "io", 2))
        return VGA_ARB_RSRC_LEGACY_IO;
    if (!strncmp(tok, "mem", 3))
        return VGA_ARB_RSRC_LEGACY_MEM;
fail:
    return VGA_ARB_RSRC_NONE;
}

int
pci_device_vgaarb_init(void)
{
    struct pci_slot_match match;
    char buf[BUFSIZE];
    int ret, rsrc;

    if (!pci_sys)
        return -1;

    if ((pci_sys->vgaarb_fd = open ("/dev/vga_arbiter", O_RDWR | O_CLOEXEC)) < 0) {
        return errno;
    }

    ret = read(pci_sys->vgaarb_fd, buf, BUFSIZE);
    if (ret <= 0)
        return -1;

    memset(&match, 0xff, sizeof(match));
    /* need to find the device to go back to and what it was decoding */
    rsrc = parse_string_to_decodes_rsrc(buf, &pci_sys->vga_count, &match);

    pci_sys->vga_default_dev = pci_device_find_by_slot(match.domain, match.bus, match.dev, match.func);

    if (pci_sys->vga_default_dev)
        pci_sys->vga_default_dev->vgaarb_rsrc = rsrc;
    return 0;
}

void
pci_device_vgaarb_fini(void)
{
    if (!pci_sys)
        return;

    close(pci_sys->vgaarb_fd);
}

/**
 * Writes message on vga device. The messages are defined by the kernel
 * implementation.
 *
 * \param fd    vga arbiter device.
 * \param buf   message itself.
 * \param len   message length.
 *
 * \return
 * Zero on success, 1 if something gets wrong and 2 if fd is busy (only for
 * 'trylock')
 */
static int
vgaarb_write(int fd, char *buf, int len)
{
    int ret;


    buf[len] = '\0';

    ret = write(fd, buf, len);
    if (ret == -1) {
        /* the user may have called "trylock" and didn't get the lock */
        if (errno == EBUSY)
            return 2;

#ifdef DEBUG
        fprintf(stderr, "write error");
#endif
        return 1;
    }
    else if (ret != len) {
        /* it's need to receive the exactly amount required. */
#ifdef DEBUG
        fprintf(stderr, "write error: wrote different than expected\n");
#endif
        return 1;
    }

#ifdef DEBUG
    fprintf(stderr, "%s: successfully wrote: '%s'\n", __FUNCTION__, buf);
#endif

    return 0;
}


static const char *
rsrc_to_str(int iostate)
{
    switch (iostate) {
    case VGA_ARB_RSRC_LEGACY_IO | VGA_ARB_RSRC_LEGACY_MEM:
        return "io+mem";
    case VGA_ARB_RSRC_LEGACY_IO:
        return "io";
    case VGA_ARB_RSRC_LEGACY_MEM:
        return "mem";
    }

    return "none";
}

int
pci_device_vgaarb_set_target(struct pci_device *dev)
{
    int len;
    char buf[BUFSIZE];
    int ret;

    if (!dev)
        dev = pci_sys->vga_default_dev;
    if (!dev)
        return -1;

    len = snprintf(buf, BUFSIZE, "target PCI:%04x:%02x:%02x.%x",
                   dev->domain, dev->bus, dev->dev, dev->func);

    ret = vgaarb_write(pci_sys->vgaarb_fd, buf, len);
    if (ret)
        return ret;

    ret = read(pci_sys->vgaarb_fd, buf, BUFSIZE);
    if (ret <= 0)
        return -1;

    dev->vgaarb_rsrc = parse_string_to_decodes_rsrc(buf, &pci_sys->vga_count, NULL);
    pci_sys->vga_target = dev;
    return 0;
}

int
pci_device_vgaarb_decodes(int new_vgaarb_rsrc)
{
    int len;
    char buf[BUFSIZE];
    int ret;
    struct pci_device *dev = pci_sys->vga_target;

    if (!dev)
        return -1;
    if (dev->vgaarb_rsrc == new_vgaarb_rsrc)
        return 0;

    len = snprintf(buf, BUFSIZE, "decodes %s", rsrc_to_str(new_vgaarb_rsrc));
    ret = vgaarb_write(pci_sys->vgaarb_fd, buf, len);
    if (ret == 0)
        dev->vgaarb_rsrc = new_vgaarb_rsrc;

    ret = read(pci_sys->vgaarb_fd, buf, BUFSIZE);
    if (ret <= 0)
        return -1;

    parse_string_to_decodes_rsrc(buf, &pci_sys->vga_count, NULL);

    return ret;
}

int
pci_device_vgaarb_lock(void)
{
    int len;
    char buf[BUFSIZE];
    struct pci_device *dev = pci_sys->vga_target;

    if (!dev)
	return -1;

    if (dev->vgaarb_rsrc == 0 || pci_sys->vga_count == 1)
        return 0;

    len = snprintf(buf, BUFSIZE, "lock %s", rsrc_to_str(dev->vgaarb_rsrc));

    return vgaarb_write(pci_sys->vgaarb_fd, buf, len);
}

int
pci_device_vgaarb_trylock(void)
{
    int len;
    char buf[BUFSIZE];
    struct pci_device *dev = pci_sys->vga_target;

    if (!dev)
        return -1;

    if (dev->vgaarb_rsrc == 0 || pci_sys->vga_count == 1)
        return 0;

    len = snprintf(buf, BUFSIZE, "trylock %s", rsrc_to_str(dev->vgaarb_rsrc));

    return vgaarb_write(pci_sys->vgaarb_fd, buf, len);
}

int
pci_device_vgaarb_unlock(void)
{
    int len;
    char buf[BUFSIZE];
    struct pci_device *dev = pci_sys->vga_target;

    if (!dev)
        return -1;

    if (dev->vgaarb_rsrc == 0 || pci_sys->vga_count == 1)
        return 0;

    len = snprintf(buf, BUFSIZE, "unlock %s", rsrc_to_str(dev->vgaarb_rsrc));

    return vgaarb_write(pci_sys->vgaarb_fd, buf, len);
}

int pci_device_vgaarb_get_info(struct pci_device *dev, int *vga_count, int *rsrc_decodes)
{
    *vga_count = pci_sys->vga_count;
    if (!dev)
        return 0;

    *rsrc_decodes = dev->vgaarb_rsrc;
        return 0;
}