2 * Copyright (c) 2007 Paulo R. Zanoni, Tiago Vignatti
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use,
9 * copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
36 #include "pciaccess.h"
37 #include "pciaccess_private.h"
42 parse_string_to_decodes_rsrc(char *input, int *vga_count, struct pci_slot_match *match)
45 char *input_sp, *count_sp, *pci_sp;
48 tok = strtok_r(input,",",&input_sp);
52 strncpy(tmp, input, 15);
55 tok = strtok_r(tmp,":",&count_sp);
58 tok = strtok_r(NULL, ":",&count_sp);
62 *vga_count = strtoul(tok, NULL, 10);
63 if (*vga_count == LONG_MAX)
67 fprintf(stderr,"vga count is %d\n", *vga_count);
70 tok = strtok_r(NULL, ",",&input_sp);
75 strncpy(tmp, tok, 32);
77 tok = strtok_r(tmp, ":", &pci_sp);
80 tok = strtok_r(NULL, ":", &pci_sp);
83 match->domain = strtoul(tok, NULL, 16);
85 tok = strtok_r(NULL, ":", &pci_sp);
88 match->bus = strtoul(tok, NULL, 16);
90 tok = strtok_r(NULL, ".", &pci_sp);
93 match->dev = strtoul(tok, NULL, 16);
95 tok = strtok_r(NULL, ".", &pci_sp);
98 match->func = strtoul(tok, NULL, 16);
101 tok = strtok_r(NULL, ",",&input_sp);
104 tok = strtok_r(tok, "=", &input_sp);
107 tok = strtok_r(NULL, "=", &input_sp);
111 if (!strncmp(tok, "io+mem", 6))
112 return VGA_ARB_RSRC_LEGACY_IO | VGA_ARB_RSRC_LEGACY_MEM;
113 if (!strncmp(tok, "io", 2))
114 return VGA_ARB_RSRC_LEGACY_IO;
115 if (!strncmp(tok, "mem", 3))
116 return VGA_ARB_RSRC_LEGACY_MEM;
118 return VGA_ARB_RSRC_NONE;
122 pci_device_vgaarb_init(void)
124 struct pci_slot_match match;
127 if ((pci_sys->vgaarb_fd = open ("/dev/vga_arbiter", O_RDWR)) < 0) {
131 ret = read(pci_sys->vgaarb_fd, buf, BUFSIZE);
135 memset(&match, 0xff, sizeof(match));
136 /* need to find the device to go back to and what it was decoding */
137 rsrc = parse_string_to_decodes_rsrc(buf, &pci_sys->vga_count, &match);
139 pci_sys->vga_default_dev = pci_device_find_by_slot(match.domain, match.bus, match.dev, match.func);
141 if (pci_sys->vga_default_dev)
142 pci_sys->vga_default_dev->vgaarb_rsrc = rsrc;
147 pci_device_vgaarb_fini(void)
149 close(pci_sys->vgaarb_fd);
153 * Writes message on vga device. The messages are defined by the kernel
156 * \param fd vga arbiter device.
157 * \param buf message itself.
158 * \param len message length.
161 * Zero on success, 1 if something gets wrong and 2 if fd is busy (only for
165 vgaarb_write(int fd, char *buf, int len)
172 ret = write(fd, buf, len);
174 /* the user may have called "trylock" and didn't get the lock */
179 fprintf(stderr, "write error");
183 else if (ret != len) {
184 /* it's need to receive the exactly amount required. */
186 fprintf(stderr, "write error: wrote different than expected\n");
192 fprintf(stderr, "%s: successfully wrote: '%s'\n", __FUNCTION__, buf);
200 rsrc_to_str(int iostate)
203 case VGA_ARB_RSRC_LEGACY_IO | VGA_ARB_RSRC_LEGACY_MEM:
205 case VGA_ARB_RSRC_LEGACY_IO:
207 case VGA_ARB_RSRC_LEGACY_MEM:
215 pci_device_vgaarb_set_target(struct pci_device *dev)
222 dev = pci_sys->vga_default_dev;
226 len = snprintf(buf, BUFSIZE, "target PCI:%04x:%02x:%02x.%x",
227 dev->domain, dev->bus, dev->dev, dev->func);
229 ret = vgaarb_write(pci_sys->vgaarb_fd, buf, len);
233 ret = read(pci_sys->vgaarb_fd, buf, BUFSIZE);
237 dev->vgaarb_rsrc = parse_string_to_decodes_rsrc(buf, &pci_sys->vga_count, NULL);
238 pci_sys->vga_target = dev;
243 pci_device_vgaarb_decodes(int new_vgaarb_rsrc)
248 struct pci_device *dev = pci_sys->vga_target;
252 if (dev->vgaarb_rsrc == new_vgaarb_rsrc)
255 len = snprintf(buf, BUFSIZE, "decodes %s", rsrc_to_str(dev->vgaarb_rsrc));
256 ret = vgaarb_write(pci_sys->vgaarb_fd, buf, len);
258 dev->vgaarb_rsrc = new_vgaarb_rsrc;
263 pci_device_vgaarb_lock(void)
267 struct pci_device *dev = pci_sys->vga_target;
272 if (dev->vgaarb_rsrc == 0 || pci_sys->vga_count == 1)
275 len = snprintf(buf, BUFSIZE, "lock %s", rsrc_to_str(dev->vgaarb_rsrc));
277 return vgaarb_write(pci_sys->vgaarb_fd, buf, len);
281 pci_device_vgaarb_trylock(void)
285 struct pci_device *dev = pci_sys->vga_target;
290 if (dev->vgaarb_rsrc == 0 || pci_sys->vga_count == 1)
293 len = snprintf(buf, BUFSIZE, "trylock %s", rsrc_to_str(dev->vgaarb_rsrc));
295 return vgaarb_write(pci_sys->vgaarb_fd, buf, len);
299 pci_device_vgaarb_unlock(void)
303 struct pci_device *dev = pci_sys->vga_target;
308 if (dev->vgaarb_rsrc == 0 || pci_sys->vga_count == 1)
311 len = snprintf(buf, BUFSIZE, "unlock %s", rsrc_to_str(dev->vgaarb_rsrc));
313 return vgaarb_write(pci_sys->vgaarb_fd, buf, len);
316 int pci_device_vgaarb_get_info(struct pci_device *dev, int *vga_count, int *rsrc_decodes)
318 *vga_count = pci_sys->vga_count;
322 *rsrc_decodes = dev->vgaarb_rsrc;