summaryrefslogtreecommitdiffstats
path: root/src/gallium/auxiliary/pipe-loader/pipe_loader_drm.c
blob: 518f3da3107a9de0009763cf6c66d4be26a013b6 (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
/**************************************************************************
 *
 * Copyright 2011 Intel Corporation
 * Copyright 2012 Francisco Jerez
 * All Rights Reserved.
 *
 * 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, sub license, 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 NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS 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.
 *
 * Authors:
 *    Kristian Høgsberg <krh@bitplanet.net>
 *    Benjamin Franzke <benjaminfranzke@googlemail.com>
 *
 **************************************************************************/

#include <fcntl.h>
#include <stdio.h>
#include <libudev.h>
#include <xf86drm.h>

#include "state_tracker/drm_driver.h"
#include "pipe_loader_priv.h"

#include "util/u_memory.h"
#include "util/u_dl.h"
#include "util/u_debug.h"

#define DRIVER_MAP_GALLIUM_ONLY
#include "pci_ids/pci_id_driver_map.h"

struct pipe_loader_drm_device {
   struct pipe_loader_device base;
   struct util_dl_library *lib;
   int fd;
};

#define pipe_loader_drm_device(dev) ((struct pipe_loader_drm_device *)dev)

static boolean
find_drm_pci_id(struct pipe_loader_drm_device *ddev)
{
   struct udev *udev = NULL;
   struct udev_device *parent, *device = NULL;
   struct stat stat;
   const char *pci_id;

   if (fstat(ddev->fd, &stat) < 0)
      goto fail;

   udev = udev_new();
   if (!udev)
      goto fail;

   device = udev_device_new_from_devnum(udev, 'c', stat.st_rdev);
   if (!device)
      goto fail;

   parent = udev_device_get_parent(device);
   if (!parent)
      goto fail;

   pci_id = udev_device_get_property_value(parent, "PCI_ID");
   if (!pci_id ||
       sscanf(pci_id, "%x:%x", &ddev->base.u.pci.vendor_id,
              &ddev->base.u.pci.chip_id) != 2)
      goto fail;

   return TRUE;

  fail:
   if (device)
      udev_device_unref(device);
   if (udev)
      udev_unref(udev);

   debug_printf("pci id for fd %d not found\n", ddev->fd);
   return FALSE;
}

static boolean
find_drm_driver_name(struct pipe_loader_drm_device *ddev)
{
   struct pipe_loader_device *dev = &ddev->base;
   int i, j;

   for (i = 0; driver_map[i].driver; i++) {
      if (dev->u.pci.vendor_id != driver_map[i].vendor_id)
         continue;

      if (driver_map[i].num_chips_ids == -1) {
         dev->driver_name = driver_map[i].driver;
         goto found;
      }

      for (j = 0; j < driver_map[i].num_chips_ids; j++) {
         if (dev->u.pci.chip_id == driver_map[i].chip_ids[j]) {
            dev->driver_name = driver_map[i].driver;
            goto found;
         }
      }
   }

   return FALSE;

  found:
   debug_printf("driver for %04x:%04x: %s\n", dev->u.pci.vendor_id,
                dev->u.pci.chip_id, dev->driver_name);
   return TRUE;
}

static struct pipe_loader_ops pipe_loader_drm_ops;

boolean
pipe_loader_drm_probe_fd(struct pipe_loader_device **dev, int fd)
{
   struct pipe_loader_drm_device *ddev = CALLOC_STRUCT(pipe_loader_drm_device);

   ddev->base.type = PIPE_LOADER_DEVICE_PCI;
   ddev->base.ops = &pipe_loader_drm_ops;
   ddev->fd = fd;

   if (!find_drm_pci_id(ddev))
      goto fail;

   if (!find_drm_driver_name(ddev))
      goto fail;

   *dev = &ddev->base;
   return TRUE;

  fail:
   FREE(ddev);
   return FALSE;
}

static int
open_drm_minor(int minor)
{
   char path[PATH_MAX];
   snprintf(path, sizeof(path), DRM_DEV_NAME, DRM_DIR_NAME, minor);
   return open(path, O_RDWR, 0);
}

int
pipe_loader_drm_probe(struct pipe_loader_device **devs, int ndev)
{
   int i, j, fd;

   for (i = 0, j = 0; i < DRM_MAX_MINOR; i++) {
      fd = open_drm_minor(i);
      if (fd < 0)
         continue;

      if (j >= ndev || !pipe_loader_drm_probe_fd(&devs[j], fd))
         close(fd);

      j++;
   }

   return j;
}

static void
pipe_loader_drm_release(struct pipe_loader_device **dev)
{
   struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(*dev);

   if (ddev->lib)
      util_dl_close(ddev->lib);

   close(ddev->fd);
   FREE(ddev);
   *dev = NULL;
}

static struct pipe_screen *
pipe_loader_drm_create_screen(struct pipe_loader_device *dev,
                              const char *library_paths)
{
   struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(dev);
   const struct drm_driver_descriptor *dd;

   if (!ddev->lib)
      ddev->lib = pipe_loader_find_module(dev, library_paths);
   if (!ddev->lib)
      return NULL;

   dd = (const struct drm_driver_descriptor *)
      util_dl_get_proc_address(ddev->lib, "driver_descriptor");

   /* sanity check on the name */
   if (!dd || strcmp(dd->name, ddev->base.driver_name) != 0)
      return NULL;

   return dd->create_screen(ddev->fd);
}

static struct pipe_loader_ops pipe_loader_drm_ops = {
   .create_screen = pipe_loader_drm_create_screen,
   .release = pipe_loader_drm_release
};