1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2019 Red Hat 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21bf215546Sopenharmony_ci * IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci/* connect to an X server and work out the default device. */ 25bf215546Sopenharmony_ci 26bf215546Sopenharmony_ci#include <xcb/xcb.h> 27bf215546Sopenharmony_ci#include <xcb/dri3.h> 28bf215546Sopenharmony_ci#include <unistd.h> 29bf215546Sopenharmony_ci#include <stdlib.h> 30bf215546Sopenharmony_ci#include <fcntl.h> 31bf215546Sopenharmony_ci#include <xf86drm.h> 32bf215546Sopenharmony_ci 33bf215546Sopenharmony_ci#include "device_select.h" 34bf215546Sopenharmony_cistatic int 35bf215546Sopenharmony_cids_dri3_open(xcb_connection_t *conn, 36bf215546Sopenharmony_ci xcb_window_t root, 37bf215546Sopenharmony_ci uint32_t provider) 38bf215546Sopenharmony_ci{ 39bf215546Sopenharmony_ci xcb_dri3_open_cookie_t cookie; 40bf215546Sopenharmony_ci xcb_dri3_open_reply_t *reply; 41bf215546Sopenharmony_ci int fd; 42bf215546Sopenharmony_ci 43bf215546Sopenharmony_ci cookie = xcb_dri3_open(conn, 44bf215546Sopenharmony_ci root, 45bf215546Sopenharmony_ci provider); 46bf215546Sopenharmony_ci 47bf215546Sopenharmony_ci reply = xcb_dri3_open_reply(conn, cookie, NULL); 48bf215546Sopenharmony_ci if (!reply) 49bf215546Sopenharmony_ci return -1; 50bf215546Sopenharmony_ci 51bf215546Sopenharmony_ci if (reply->nfd != 1) { 52bf215546Sopenharmony_ci free(reply); 53bf215546Sopenharmony_ci return -1; 54bf215546Sopenharmony_ci } 55bf215546Sopenharmony_ci 56bf215546Sopenharmony_ci fd = xcb_dri3_open_reply_fds(conn, reply)[0]; 57bf215546Sopenharmony_ci free(reply); 58bf215546Sopenharmony_ci fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); 59bf215546Sopenharmony_ci 60bf215546Sopenharmony_ci return fd; 61bf215546Sopenharmony_ci} 62bf215546Sopenharmony_ci 63bf215546Sopenharmony_ciint device_select_find_xcb_pci_default(struct device_pci_info *devices, uint32_t device_count) 64bf215546Sopenharmony_ci{ 65bf215546Sopenharmony_ci const xcb_setup_t *setup; 66bf215546Sopenharmony_ci xcb_screen_iterator_t iter; 67bf215546Sopenharmony_ci int scrn; 68bf215546Sopenharmony_ci xcb_connection_t *conn; 69bf215546Sopenharmony_ci int default_idx = -1; 70bf215546Sopenharmony_ci drmDevicePtr xdev = NULL; 71bf215546Sopenharmony_ci 72bf215546Sopenharmony_ci conn = xcb_connect(NULL, &scrn); 73bf215546Sopenharmony_ci if (!conn) 74bf215546Sopenharmony_ci return -1; 75bf215546Sopenharmony_ci 76bf215546Sopenharmony_ci xcb_query_extension_cookie_t dri3_cookie; 77bf215546Sopenharmony_ci xcb_query_extension_reply_t *dri3_reply = NULL; 78bf215546Sopenharmony_ci 79bf215546Sopenharmony_ci dri3_cookie = xcb_query_extension(conn, 4, "DRI3"); 80bf215546Sopenharmony_ci dri3_reply = xcb_query_extension_reply(conn, dri3_cookie, NULL); 81bf215546Sopenharmony_ci 82bf215546Sopenharmony_ci if (!dri3_reply) 83bf215546Sopenharmony_ci goto out; 84bf215546Sopenharmony_ci 85bf215546Sopenharmony_ci if (dri3_reply->present == 0) 86bf215546Sopenharmony_ci goto out; 87bf215546Sopenharmony_ci 88bf215546Sopenharmony_ci setup = xcb_get_setup(conn); 89bf215546Sopenharmony_ci iter = xcb_setup_roots_iterator(setup); 90bf215546Sopenharmony_ci 91bf215546Sopenharmony_ci xcb_screen_t *screen = iter.data; 92bf215546Sopenharmony_ci 93bf215546Sopenharmony_ci int dri3_fd = ds_dri3_open(conn, screen->root, 0); 94bf215546Sopenharmony_ci if (dri3_fd == -1) 95bf215546Sopenharmony_ci goto out; 96bf215546Sopenharmony_ci 97bf215546Sopenharmony_ci int ret = drmGetDevice2(dri3_fd, 0, &xdev); 98bf215546Sopenharmony_ci close(dri3_fd); 99bf215546Sopenharmony_ci if (ret < 0) 100bf215546Sopenharmony_ci goto out; 101bf215546Sopenharmony_ci 102bf215546Sopenharmony_ci for (unsigned i = 0; i < device_count; i++) { 103bf215546Sopenharmony_ci if (devices[i].has_bus_info) { 104bf215546Sopenharmony_ci if (xdev->businfo.pci->domain == devices[i].bus_info.domain && 105bf215546Sopenharmony_ci xdev->businfo.pci->bus == devices[i].bus_info.bus && 106bf215546Sopenharmony_ci xdev->businfo.pci->dev == devices[i].bus_info.dev && 107bf215546Sopenharmony_ci xdev->businfo.pci->func == devices[i].bus_info.func) { 108bf215546Sopenharmony_ci default_idx = i; 109bf215546Sopenharmony_ci } 110bf215546Sopenharmony_ci } else { 111bf215546Sopenharmony_ci if (xdev->deviceinfo.pci->vendor_id == devices[i].dev_info.vendor_id && 112bf215546Sopenharmony_ci xdev->deviceinfo.pci->device_id == devices[i].dev_info.device_id) 113bf215546Sopenharmony_ci default_idx = i; 114bf215546Sopenharmony_ci } 115bf215546Sopenharmony_ci if (default_idx != -1) 116bf215546Sopenharmony_ci break; 117bf215546Sopenharmony_ci } 118bf215546Sopenharmony_ci 119bf215546Sopenharmony_ciout: 120bf215546Sopenharmony_ci free(dri3_reply); 121bf215546Sopenharmony_ci drmFreeDevice(&xdev); /* Is NULL pointer safe. */ 122bf215546Sopenharmony_ci xcb_disconnect(conn); 123bf215546Sopenharmony_ci return default_idx; 124bf215546Sopenharmony_ci} 125