blob: f6850aa27275c708ae1da8602d64fa58beeb788e [file] [log] [blame]
Jose Fonsecad2443b22003-05-27 00:37:33 +00001/**
Jan Vesely50d3c852016-06-30 14:22:52 -04002 * \file xf86drm.c
Jose Fonsecad2443b22003-05-27 00:37:33 +00003 * User-level interface to DRM device
Daryll Straussb3a57661999-12-05 01:19:48 +00004 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00005 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Kevin E. Martin <martin@valinux.com>
7 */
8
9/*
Brian Paul569da5a2000-06-08 14:38:22 +000010 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
Daryll Straussb3a57661999-12-05 01:19:48 +000012 * All Rights Reserved.
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
Gareth Hughes36047532001-02-15 08:12:14 +000020 *
Daryll Straussb3a57661999-12-05 01:19:48 +000021 * The above copyright notice and this permission notice (including the next
22 * paragraph) shall be included in all copies or substantial portions of the
23 * Software.
Gareth Hughes36047532001-02-15 08:12:14 +000024 *
Daryll Straussb3a57661999-12-05 01:19:48 +000025 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31 * DEALINGS IN THE SOFTWARE.
Daryll Straussb3a57661999-12-05 01:19:48 +000032 */
33
Dave Airlie79038752006-11-08 15:08:09 +110034#ifdef HAVE_CONFIG_H
35# include <config.h>
Daryll Straussb3a57661999-12-05 01:19:48 +000036#endif
Dave Airlie79038752006-11-08 15:08:09 +110037#include <stdio.h>
38#include <stdlib.h>
Emil Velikovfae59d72015-09-09 17:54:34 +010039#include <stdbool.h>
Dave Airlie79038752006-11-08 15:08:09 +110040#include <unistd.h>
41#include <string.h>
Ian Romanick015efd12009-07-06 09:23:59 -070042#include <strings.h>
Dave Airlie79038752006-11-08 15:08:09 +110043#include <ctype.h>
Emil Velikov0ca03a42015-03-07 00:58:39 +000044#include <dirent.h>
45#include <stddef.h>
Dave Airlie79038752006-11-08 15:08:09 +110046#include <fcntl.h>
47#include <errno.h>
Felix Janda4031dc12015-09-26 08:08:43 +020048#include <limits.h>
Dave Airlie79038752006-11-08 15:08:09 +110049#include <signal.h>
Jesse Barnesf4f76a62009-01-07 10:18:08 -080050#include <time.h>
Dave Airlie79038752006-11-08 15:08:09 +110051#include <sys/types.h>
52#include <sys/stat.h>
53#define stat_t struct stat
54#include <sys/ioctl.h>
Dave Airlie79038752006-11-08 15:08:09 +110055#include <sys/time.h>
56#include <stdarg.h>
Mike Frysinger8c8d5dd2016-06-21 12:18:15 -040057#ifdef MAJOR_IN_MKDEV
58#include <sys/mkdev.h>
59#endif
60#ifdef MAJOR_IN_SYSMACROS
61#include <sys/sysmacros.h>
Alan Coopersmith0e1135d2015-03-07 11:44:32 -080062#endif
Emil Velikovb556ea12015-08-17 11:09:06 +080063#include <math.h>
Daryll Straussb3a57661999-12-05 01:19:48 +000064
65/* Not all systems have MAP_FAILED defined */
66#ifndef MAP_FAILED
67#define MAP_FAILED ((void *)-1)
68#endif
69
Daryll Straussb3a57661999-12-05 01:19:48 +000070#include "xf86drm.h"
Emil Velikov42465fe2015-04-05 15:51:59 +010071#include "libdrm_macros.h"
Daryll Straussb3a57661999-12-05 01:19:48 +000072
Emil Velikov5f68d312015-09-07 13:54:32 +010073#include "util_math.h"
74
Jonathan Grayfc083322015-07-21 03:12:56 +100075#ifdef __OpenBSD__
Jan Vesely50d3c852016-06-30 14:22:52 -040076#define DRM_PRIMARY_MINOR_NAME "drm"
77#define DRM_CONTROL_MINOR_NAME "drmC"
78#define DRM_RENDER_MINOR_NAME "drmR"
Jonathan Grayfc083322015-07-21 03:12:56 +100079#else
Jan Vesely50d3c852016-06-30 14:22:52 -040080#define DRM_PRIMARY_MINOR_NAME "card"
81#define DRM_CONTROL_MINOR_NAME "controlD"
82#define DRM_RENDER_MINOR_NAME "renderD"
Jonathan Grayfc083322015-07-21 03:12:56 +100083#endif
84
Hasso Tepper27c37852008-04-07 15:27:43 +030085#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Eric Anholtcfa778a2003-02-21 23:23:09 +000086#define DRM_MAJOR 145
Rik Faith88dbee52001-02-28 09:27:44 +000087#endif
88
Eric Anholtcfa778a2003-02-21 23:23:09 +000089#ifdef __NetBSD__
90#define DRM_MAJOR 34
91#endif
92
Jonathan Gray66c3afb2015-07-21 03:12:10 +100093#ifdef __OpenBSD__
94#ifdef __i386__
95#define DRM_MAJOR 88
96#else
97#define DRM_MAJOR 87
98#endif
99#endif /* __OpenBSD__ */
Alan Hourihaneb0a92852003-09-24 14:39:25 +0000100
Eric Anholtcfa778a2003-02-21 23:23:09 +0000101#ifndef DRM_MAJOR
Jan Vesely50d3c852016-06-30 14:22:52 -0400102#define DRM_MAJOR 226 /* Linux */
Rik Faith88dbee52001-02-28 09:27:44 +0000103#endif
104
Jonathan Grayc0ef1d02016-12-01 15:18:41 +1100105#ifdef __OpenBSD__
106struct drm_pciinfo {
107 uint16_t domain;
108 uint8_t bus;
109 uint8_t dev;
110 uint8_t func;
111 uint16_t vendor_id;
112 uint16_t device_id;
113 uint16_t subvendor_id;
114 uint16_t subdevice_id;
115 uint8_t revision_id;
116};
117
118#define DRM_IOCTL_GET_PCIINFO DRM_IOR(0x15, struct drm_pciinfo)
119#endif
120
David Dawes56bd9c22001-07-30 19:59:39 +0000121#define DRM_MSG_VERBOSITY 3
122
Daniel Vetterfd387942015-02-11 12:41:04 +0100123#define memclear(s) memset(&s, 0, sizeof(s))
124
Dave Airlie79038752006-11-08 15:08:09 +1100125static drmServerInfoPtr drm_server_info;
126
127void drmSetServerInfo(drmServerInfoPtr info)
128{
Brianccd7b6e2007-05-29 14:54:00 -0600129 drm_server_info = info;
Dave Airlie79038752006-11-08 15:08:09 +1100130}
131
Jose Fonsecad2443b22003-05-27 00:37:33 +0000132/**
133 * Output a message to stderr.
134 *
135 * \param format printf() like format string.
136 *
137 * \internal
138 * This function is a wrapper around vfprintf().
139 */
Dave Airlie79038752006-11-08 15:08:09 +1100140
Thierry Reding44b08c02014-01-22 12:06:51 +0100141static int DRM_PRINTFLIKE(1, 0)
142drmDebugPrint(const char *format, va_list ap)
Dave Airlie79038752006-11-08 15:08:09 +1100143{
Brianccd7b6e2007-05-29 14:54:00 -0600144 return vfprintf(stderr, format, ap);
Dave Airlie79038752006-11-08 15:08:09 +1100145}
146
Eric Anholtc4857422008-06-03 10:20:49 -0700147void
David Dawes56bd9c22001-07-30 19:59:39 +0000148drmMsg(const char *format, ...)
149{
Jan Vesely50d3c852016-06-30 14:22:52 -0400150 va_list ap;
David Dawes56bd9c22001-07-30 19:59:39 +0000151 const char *env;
Rob Clarkeb7c2d52015-09-04 08:08:02 -0400152 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) ||
153 (drm_server_info && drm_server_info->debug_print))
David Dawes56bd9c22001-07-30 19:59:39 +0000154 {
Jan Vesely50d3c852016-06-30 14:22:52 -0400155 va_start(ap, format);
156 if (drm_server_info) {
157 drm_server_info->debug_print(format,ap);
158 } else {
159 drmDebugPrint(format, ap);
160 }
161 va_end(ap);
David Dawes56bd9c22001-07-30 19:59:39 +0000162 }
163}
164
Daryll Straussb3a57661999-12-05 01:19:48 +0000165static void *drmHashTable = NULL; /* Context switch callbacks */
166
Dave Airlie79038752006-11-08 15:08:09 +1100167void *drmGetHashTable(void)
168{
Brianccd7b6e2007-05-29 14:54:00 -0600169 return drmHashTable;
Dave Airlie79038752006-11-08 15:08:09 +1100170}
Daryll Straussb3a57661999-12-05 01:19:48 +0000171
172void *drmMalloc(int size)
173{
Emil Velikovd0e592d2015-04-28 13:33:46 +0100174 return calloc(1, size);
Daryll Straussb3a57661999-12-05 01:19:48 +0000175}
176
177void drmFree(void *pt)
178{
Emil Velikovd0e592d2015-04-28 13:33:46 +0100179 free(pt);
Daryll Straussb3a57661999-12-05 01:19:48 +0000180}
181
Keith Packard8b9ab102008-06-13 16:03:22 -0700182/**
183 * Call ioctl, restarting if it is interupted
184 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800185int
Coleman Kane41b83a92008-08-18 17:08:21 -0400186drmIoctl(int fd, unsigned long request, void *arg)
Keith Packard8b9ab102008-06-13 16:03:22 -0700187{
Jan Vesely50d3c852016-06-30 14:22:52 -0400188 int ret;
Keith Packard8b9ab102008-06-13 16:03:22 -0700189
190 do {
Jan Vesely50d3c852016-06-30 14:22:52 -0400191 ret = ioctl(fd, request, arg);
Keith Packard8b9ab102008-06-13 16:03:22 -0700192 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
193 return ret;
194}
Daryll Straussb3a57661999-12-05 01:19:48 +0000195
Daryll Straussb3a57661999-12-05 01:19:48 +0000196static unsigned long drmGetKeyFromFd(int fd)
197{
David Dawesfcc21062001-03-30 17:16:20 +0000198 stat_t st;
Daryll Straussb3a57661999-12-05 01:19:48 +0000199
200 st.st_rdev = 0;
201 fstat(fd, &st);
202 return st.st_rdev;
203}
204
Dave Airlie79038752006-11-08 15:08:09 +1100205drmHashEntry *drmGetEntry(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +0000206{
207 unsigned long key = drmGetKeyFromFd(fd);
208 void *value;
209 drmHashEntry *entry;
210
Brianccd7b6e2007-05-29 14:54:00 -0600211 if (!drmHashTable)
Jan Vesely50d3c852016-06-30 14:22:52 -0400212 drmHashTable = drmHashCreate();
Daryll Straussb3a57661999-12-05 01:19:48 +0000213
214 if (drmHashLookup(drmHashTable, key, &value)) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400215 entry = drmMalloc(sizeof(*entry));
216 entry->fd = fd;
217 entry->f = NULL;
218 entry->tagTable = drmHashCreate();
219 drmHashInsert(drmHashTable, key, entry);
Daryll Straussb3a57661999-12-05 01:19:48 +0000220 } else {
Jan Vesely50d3c852016-06-30 14:22:52 -0400221 entry = value;
Daryll Straussb3a57661999-12-05 01:19:48 +0000222 }
223 return entry;
224}
225
Jose Fonsecad2443b22003-05-27 00:37:33 +0000226/**
Eric Anholt06cb1322003-10-23 02:23:31 +0000227 * Compare two busid strings
228 *
229 * \param first
230 * \param second
231 *
232 * \return 1 if matched.
233 *
234 * \internal
235 * This function compares two bus ID strings. It understands the older
236 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
237 * domain, b is bus, d is device, f is function.
238 */
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000239static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
Eric Anholt06cb1322003-10-23 02:23:31 +0000240{
241 /* First, check if the IDs are exactly the same */
242 if (strcasecmp(id1, id2) == 0)
Jan Vesely50d3c852016-06-30 14:22:52 -0400243 return 1;
Eric Anholt06cb1322003-10-23 02:23:31 +0000244
245 /* Try to match old/new-style PCI bus IDs. */
246 if (strncasecmp(id1, "pci", 3) == 0) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400247 unsigned int o1, b1, d1, f1;
248 unsigned int o2, b2, d2, f2;
249 int ret;
Eric Anholt06cb1322003-10-23 02:23:31 +0000250
Jan Vesely50d3c852016-06-30 14:22:52 -0400251 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
252 if (ret != 4) {
253 o1 = 0;
254 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
255 if (ret != 3)
256 return 0;
257 }
Eric Anholt06cb1322003-10-23 02:23:31 +0000258
Jan Vesely50d3c852016-06-30 14:22:52 -0400259 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
260 if (ret != 4) {
261 o2 = 0;
262 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
263 if (ret != 3)
264 return 0;
265 }
Eric Anholt06cb1322003-10-23 02:23:31 +0000266
Jan Vesely50d3c852016-06-30 14:22:52 -0400267 /* If domains aren't properly supported by the kernel interface,
268 * just ignore them, which sucks less than picking a totally random
269 * card with "open by name"
270 */
271 if (!pci_domain_ok)
272 o1 = o2 = 0;
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000273
Jan Vesely50d3c852016-06-30 14:22:52 -0400274 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
275 return 0;
276 else
277 return 1;
Eric Anholt06cb1322003-10-23 02:23:31 +0000278 }
279 return 0;
280}
281
282/**
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300283 * Handles error checking for chown call.
284 *
285 * \param path to file.
286 * \param id of the new owner.
287 * \param id of the new group.
288 *
289 * \return zero if success or -1 if failure.
290 *
291 * \internal
292 * Checks for failure. If failure was caused by signal call chown again.
293 * If any other failure happened then it will output error mesage using
294 * drmMsg() call.
295 */
Jan Vesely6fc0e4b2015-02-27 12:54:34 -0500296#if !defined(UDEV)
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300297static int chown_check_return(const char *path, uid_t owner, gid_t group)
298{
Jan Vesely50d3c852016-06-30 14:22:52 -0400299 int rv;
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300300
Jan Vesely50d3c852016-06-30 14:22:52 -0400301 do {
302 rv = chown(path, owner, group);
303 } while (rv != 0 && errno == EINTR);
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300304
Jan Vesely50d3c852016-06-30 14:22:52 -0400305 if (rv == 0)
306 return 0;
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300307
Jan Vesely50d3c852016-06-30 14:22:52 -0400308 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
309 path, errno, strerror(errno));
310 return -1;
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300311}
Jan Vesely6fc0e4b2015-02-27 12:54:34 -0500312#endif
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300313
314/**
Jose Fonsecad2443b22003-05-27 00:37:33 +0000315 * Open the DRM device, creating it if necessary.
316 *
317 * \param dev major and minor numbers of the device.
318 * \param minor minor number of the device.
Jan Vesely50d3c852016-06-30 14:22:52 -0400319 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000320 * \return a file descriptor on success, or a negative value on error.
321 *
322 * \internal
323 * Assembles the device name from \p minor and opens it, creating the device
324 * special file node with the major and minor numbers specified by \p dev and
325 * parent directory if necessary and was called by root.
326 */
Jan Veselyde8532d2014-11-30 12:53:18 -0500327static int drmOpenDevice(dev_t dev, int minor, int type)
Daryll Straussb3a57661999-12-05 01:19:48 +0000328{
David Dawes9c775d02001-05-14 14:49:58 +0000329 stat_t st;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000330 const char *dev_name;
Rik Faith88dbee52001-02-28 09:27:44 +0000331 char buf[64];
332 int fd;
Dave Airlie79038752006-11-08 15:08:09 +1100333 mode_t devmode = DRM_DEV_MODE, serv_mode;
Jan Vesely0706c142015-02-27 12:47:46 -0500334 gid_t serv_group;
335#if !defined(UDEV)
Rik Faith88dbee52001-02-28 09:27:44 +0000336 int isroot = !geteuid();
Rik Faith88dbee52001-02-28 09:27:44 +0000337 uid_t user = DRM_DEV_UID;
Jan Vesely0706c142015-02-27 12:47:46 -0500338 gid_t group = DRM_DEV_GID;
339#endif
340
Frank Binns0c5aaee2015-01-14 14:07:51 +0000341 switch (type) {
342 case DRM_NODE_PRIMARY:
Jan Vesely50d3c852016-06-30 14:22:52 -0400343 dev_name = DRM_DEV_NAME;
344 break;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000345 case DRM_NODE_CONTROL:
Jan Vesely50d3c852016-06-30 14:22:52 -0400346 dev_name = DRM_CONTROL_DEV_NAME;
347 break;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000348 case DRM_NODE_RENDER:
Jan Vesely50d3c852016-06-30 14:22:52 -0400349 dev_name = DRM_RENDER_DEV_NAME;
350 break;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000351 default:
Jan Vesely50d3c852016-06-30 14:22:52 -0400352 return -EINVAL;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000353 };
354
355 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
Eric Anholt06cb1322003-10-23 02:23:31 +0000356 drmMsg("drmOpenDevice: node name is %s\n", buf);
David Dawes56bd9c22001-07-30 19:59:39 +0000357
Rob Clarkeb7c2d52015-09-04 08:08:02 -0400358 if (drm_server_info && drm_server_info->get_perms) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400359 drm_server_info->get_perms(&serv_group, &serv_mode);
360 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
361 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
Dave Airlie79038752006-11-08 15:08:09 +1100362 }
Brian Paul569da5a2000-06-08 14:38:22 +0000363
Dave Airlie9101a022008-08-24 16:54:43 +1000364#if !defined(UDEV)
Rik Faith88dbee52001-02-28 09:27:44 +0000365 if (stat(DRM_DIR_NAME, &st)) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400366 if (!isroot)
367 return DRM_ERR_NOT_ROOT;
368 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
369 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
370 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
Brian Paul569da5a2000-06-08 14:38:22 +0000371 }
Daryll Straussb3a57661999-12-05 01:19:48 +0000372
Eric Anholt06cb1322003-10-23 02:23:31 +0000373 /* Check if the device node exists and create it if necessary. */
Eric Anholtd2f2b422002-08-08 21:23:46 +0000374 if (stat(buf, &st)) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400375 if (!isroot)
376 return DRM_ERR_NOT_ROOT;
377 remove(buf);
378 mknod(buf, S_IFCHR | devmode, dev);
Daryll Straussb3a57661999-12-05 01:19:48 +0000379 }
Dave Airlie79038752006-11-08 15:08:09 +1100380
Rob Clarkeb7c2d52015-09-04 08:08:02 -0400381 if (drm_server_info && drm_server_info->get_perms) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400382 group = ((int)serv_group >= 0) ? serv_group : DRM_DEV_GID;
383 chown_check_return(buf, user, group);
384 chmod(buf, devmode);
Dave Airlie79038752006-11-08 15:08:09 +1100385 }
Dave Airlie9101a022008-08-24 16:54:43 +1000386#else
387 /* if we modprobed then wait for udev */
388 {
Jan Vesely50d3c852016-06-30 14:22:52 -0400389 int udev_count = 0;
Dave Airlie9101a022008-08-24 16:54:43 +1000390wait_for_udev:
391 if (stat(DRM_DIR_NAME, &st)) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400392 usleep(20);
393 udev_count++;
Dave Airlie9101a022008-08-24 16:54:43 +1000394
Jan Vesely50d3c852016-06-30 14:22:52 -0400395 if (udev_count == 50)
396 return -1;
397 goto wait_for_udev;
398 }
Dave Airlie9101a022008-08-24 16:54:43 +1000399
Jan Vesely50d3c852016-06-30 14:22:52 -0400400 if (stat(buf, &st)) {
401 usleep(20);
402 udev_count++;
Dave Airlie9101a022008-08-24 16:54:43 +1000403
Jan Vesely50d3c852016-06-30 14:22:52 -0400404 if (udev_count == 50)
405 return -1;
406 goto wait_for_udev;
407 }
Dave Airlie9101a022008-08-24 16:54:43 +1000408 }
409#endif
Rik Faith88dbee52001-02-28 09:27:44 +0000410
David Dawes56bd9c22001-07-30 19:59:39 +0000411 fd = open(buf, O_RDWR, 0);
412 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
Jan Vesely50d3c852016-06-30 14:22:52 -0400413 fd, fd < 0 ? strerror(errno) : "OK");
Brianccd7b6e2007-05-29 14:54:00 -0600414 if (fd >= 0)
Jan Vesely50d3c852016-06-30 14:22:52 -0400415 return fd;
Eric Anholtd2f2b422002-08-08 21:23:46 +0000416
Dave Airlie39e5e982010-12-07 14:26:09 +1000417#if !defined(UDEV)
Eric Anholt06cb1322003-10-23 02:23:31 +0000418 /* Check if the device node is not what we expect it to be, and recreate it
419 * and try again if so.
420 */
Eric Anholtd2f2b422002-08-08 21:23:46 +0000421 if (st.st_rdev != dev) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400422 if (!isroot)
423 return DRM_ERR_NOT_ROOT;
424 remove(buf);
425 mknod(buf, S_IFCHR | devmode, dev);
426 if (drm_server_info && drm_server_info->get_perms) {
427 chown_check_return(buf, user, group);
428 chmod(buf, devmode);
429 }
Eric Anholtd2f2b422002-08-08 21:23:46 +0000430 }
431 fd = open(buf, O_RDWR, 0);
432 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
Jan Vesely50d3c852016-06-30 14:22:52 -0400433 fd, fd < 0 ? strerror(errno) : "OK");
Brianccd7b6e2007-05-29 14:54:00 -0600434 if (fd >= 0)
Jan Vesely50d3c852016-06-30 14:22:52 -0400435 return fd;
Eric Anholtd2f2b422002-08-08 21:23:46 +0000436
David Dawes56bd9c22001-07-30 19:59:39 +0000437 drmMsg("drmOpenDevice: Open failed\n");
Rik Faith88dbee52001-02-28 09:27:44 +0000438 remove(buf);
Dave Airlie39e5e982010-12-07 14:26:09 +1000439#endif
Rik Faith88dbee52001-02-28 09:27:44 +0000440 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +0000441}
442
Jose Fonsecad2443b22003-05-27 00:37:33 +0000443
444/**
445 * Open the DRM device
446 *
447 * \param minor device minor number.
448 * \param create allow to create the device if set.
449 *
450 * \return a file descriptor on success, or a negative value on error.
Jan Vesely50d3c852016-06-30 14:22:52 -0400451 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000452 * \internal
453 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
454 * name from \p minor and opens it.
455 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800456static int drmOpenMinor(int minor, int create, int type)
Rik Faith88dbee52001-02-28 09:27:44 +0000457{
458 int fd;
459 char buf[64];
Frank Binns0c5aaee2015-01-14 14:07:51 +0000460 const char *dev_name;
Jan Vesely50d3c852016-06-30 14:22:52 -0400461
Brianccd7b6e2007-05-29 14:54:00 -0600462 if (create)
Jan Vesely50d3c852016-06-30 14:22:52 -0400463 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
464
Frank Binns0c5aaee2015-01-14 14:07:51 +0000465 switch (type) {
466 case DRM_NODE_PRIMARY:
Jan Vesely50d3c852016-06-30 14:22:52 -0400467 dev_name = DRM_DEV_NAME;
468 break;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000469 case DRM_NODE_CONTROL:
Jan Vesely50d3c852016-06-30 14:22:52 -0400470 dev_name = DRM_CONTROL_DEV_NAME;
471 break;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000472 case DRM_NODE_RENDER:
Jan Vesely50d3c852016-06-30 14:22:52 -0400473 dev_name = DRM_RENDER_DEV_NAME;
474 break;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000475 default:
Jan Vesely50d3c852016-06-30 14:22:52 -0400476 return -EINVAL;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000477 };
478
479 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
Brianccd7b6e2007-05-29 14:54:00 -0600480 if ((fd = open(buf, O_RDWR, 0)) >= 0)
Jan Vesely50d3c852016-06-30 14:22:52 -0400481 return fd;
Rik Faith88dbee52001-02-28 09:27:44 +0000482 return -errno;
483}
484
Brian Paul569da5a2000-06-08 14:38:22 +0000485
Jose Fonsecad2443b22003-05-27 00:37:33 +0000486/**
487 * Determine whether the DRM kernel driver has been loaded.
Jan Vesely50d3c852016-06-30 14:22:52 -0400488 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000489 * \return 1 if the DRM driver is loaded, 0 otherwise.
490 *
Jan Vesely50d3c852016-06-30 14:22:52 -0400491 * \internal
Jose Fonsecad2443b22003-05-27 00:37:33 +0000492 * Determine the presence of the kernel driver by attempting to open the 0
493 * minor and get version information. For backward compatibility with older
494 * Linux implementations, /proc/dri is also checked.
495 */
Brian Paul569da5a2000-06-08 14:38:22 +0000496int drmAvailable(void)
497{
Brian Paul569da5a2000-06-08 14:38:22 +0000498 drmVersionPtr version;
499 int retval = 0;
500 int fd;
Gareth Hughes36047532001-02-15 08:12:14 +0000501
Frank Binnsad8bbfd2015-01-14 14:07:50 +0000502 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
Alan Hourihaneb0a92852003-09-24 14:39:25 +0000503#ifdef __linux__
Jan Vesely50d3c852016-06-30 14:22:52 -0400504 /* Try proc for backward Linux compatibility */
505 if (!access("/proc/dri/0", R_OK))
506 return 1;
Alan Hourihaneb0a92852003-09-24 14:39:25 +0000507#endif
Jan Vesely50d3c852016-06-30 14:22:52 -0400508 return 0;
Brian Paul569da5a2000-06-08 14:38:22 +0000509 }
Jan Vesely50d3c852016-06-30 14:22:52 -0400510
Rik Faith88dbee52001-02-28 09:27:44 +0000511 if ((version = drmGetVersion(fd))) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400512 retval = 1;
513 drmFreeVersion(version);
Rik Faith88dbee52001-02-28 09:27:44 +0000514 }
515 close(fd);
Brian Paul569da5a2000-06-08 14:38:22 +0000516
517 return retval;
518}
519
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800520static int drmGetMinorBase(int type)
521{
522 switch (type) {
523 case DRM_NODE_PRIMARY:
524 return 0;
525 case DRM_NODE_CONTROL:
526 return 64;
527 case DRM_NODE_RENDER:
528 return 128;
529 default:
530 return -1;
531 };
532}
Jose Fonsecad2443b22003-05-27 00:37:33 +0000533
Frank Binns1f735782015-02-13 10:51:15 +0000534static int drmGetMinorType(int minor)
535{
536 int type = minor >> 6;
537
538 if (minor < 0)
539 return -1;
540
541 switch (type) {
542 case DRM_NODE_PRIMARY:
543 case DRM_NODE_CONTROL:
544 case DRM_NODE_RENDER:
545 return type;
546 default:
547 return -1;
548 }
549}
550
Emil Velikov0ca03a42015-03-07 00:58:39 +0000551static const char *drmGetMinorName(int type)
552{
553 switch (type) {
554 case DRM_NODE_PRIMARY:
Jonathan Grayfc083322015-07-21 03:12:56 +1000555 return DRM_PRIMARY_MINOR_NAME;
Emil Velikov0ca03a42015-03-07 00:58:39 +0000556 case DRM_NODE_CONTROL:
Jonathan Grayfc083322015-07-21 03:12:56 +1000557 return DRM_CONTROL_MINOR_NAME;
Emil Velikov0ca03a42015-03-07 00:58:39 +0000558 case DRM_NODE_RENDER:
Jonathan Grayfc083322015-07-21 03:12:56 +1000559 return DRM_RENDER_MINOR_NAME;
Emil Velikov0ca03a42015-03-07 00:58:39 +0000560 default:
561 return NULL;
562 }
563}
564
Jose Fonsecad2443b22003-05-27 00:37:33 +0000565/**
566 * Open the device by bus ID.
567 *
568 * \param busid bus ID.
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800569 * \param type device node type.
Jose Fonsecad2443b22003-05-27 00:37:33 +0000570 *
571 * \return a file descriptor on success, or a negative value on error.
572 *
573 * \internal
574 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
575 * comparing the device bus ID with the one supplied.
576 *
577 * \sa drmOpenMinor() and drmGetBusid().
578 */
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800579static int drmOpenByBusid(const char *busid, int type)
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000580{
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000581 int i, pci_domain_ok = 1;
Rik Faith88dbee52001-02-28 09:27:44 +0000582 int fd;
583 const char *buf;
Eric Anholt06cb1322003-10-23 02:23:31 +0000584 drmSetVersion sv;
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800585 int base = drmGetMinorBase(type);
586
587 if (base < 0)
588 return -1;
Eric Anholt06cb1322003-10-23 02:23:31 +0000589
590 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800591 for (i = base; i < base + DRM_MAX_MINOR; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400592 fd = drmOpenMinor(i, 1, type);
593 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
594 if (fd >= 0) {
595 /* We need to try for 1.4 first for proper PCI domain support
596 * and if that fails, we know the kernel is busted
597 */
598 sv.drm_di_major = 1;
599 sv.drm_di_minor = 4;
600 sv.drm_dd_major = -1; /* Don't care */
601 sv.drm_dd_minor = -1; /* Don't care */
602 if (drmSetInterfaceVersion(fd, &sv)) {
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000603#ifndef __alpha__
Jan Vesely50d3c852016-06-30 14:22:52 -0400604 pci_domain_ok = 0;
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000605#endif
Jan Vesely50d3c852016-06-30 14:22:52 -0400606 sv.drm_di_major = 1;
607 sv.drm_di_minor = 1;
608 sv.drm_dd_major = -1; /* Don't care */
609 sv.drm_dd_minor = -1; /* Don't care */
610 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
611 drmSetInterfaceVersion(fd, &sv);
612 }
613 buf = drmGetBusid(fd);
614 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
615 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
616 drmFreeBusid(buf);
617 return fd;
618 }
619 if (buf)
620 drmFreeBusid(buf);
621 close(fd);
622 }
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000623 }
624 return -1;
625}
626
Jose Fonsecad2443b22003-05-27 00:37:33 +0000627
628/**
629 * Open the device by name.
630 *
631 * \param name driver name.
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800632 * \param type the device node type.
Jan Vesely50d3c852016-06-30 14:22:52 -0400633 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000634 * \return a file descriptor on success, or a negative value on error.
Jan Vesely50d3c852016-06-30 14:22:52 -0400635 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000636 * \internal
637 * This function opens the first minor number that matches the driver name and
638 * isn't already in use. If it's in use it then it will already have a bus ID
639 * assigned.
Jan Vesely50d3c852016-06-30 14:22:52 -0400640 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000641 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
642 */
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800643static int drmOpenByName(const char *name, int type)
Daryll Straussb3a57661999-12-05 01:19:48 +0000644{
Rik Faith88dbee52001-02-28 09:27:44 +0000645 int i;
646 int fd;
647 drmVersionPtr version;
David Dawes56bd9c22001-07-30 19:59:39 +0000648 char * id;
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800649 int base = drmGetMinorBase(type);
650
651 if (base < 0)
652 return -1;
Dave Airliedb85ed22008-02-13 12:20:02 +1000653
David Dawes56bd9c22001-07-30 19:59:39 +0000654 /*
655 * Open the first minor number that matches the driver name and isn't
656 * already in use. If it's in use it will have a busid assigned already.
657 */
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800658 for (i = base; i < base + DRM_MAX_MINOR; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400659 if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
660 if ((version = drmGetVersion(fd))) {
661 if (!strcmp(version->name, name)) {
662 drmFreeVersion(version);
663 id = drmGetBusid(fd);
664 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
665 if (!id || !*id) {
666 if (id)
667 drmFreeBusid(id);
668 return fd;
669 } else {
670 drmFreeBusid(id);
671 }
672 } else {
673 drmFreeVersion(version);
674 }
675 }
676 close(fd);
677 }
Rik Faith88dbee52001-02-28 09:27:44 +0000678 }
679
680#ifdef __linux__
Adam Jackson22e41ef2006-02-20 23:09:00 +0000681 /* Backward-compatibility /proc support */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000682 for (i = 0; i < 8; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400683 char proc_name[64], buf[512];
684 char *driver, *pt, *devstring;
685 int retcode;
686
687 sprintf(proc_name, "/proc/dri/%d/name", i);
688 if ((fd = open(proc_name, 0, 0)) >= 0) {
689 retcode = read(fd, buf, sizeof(buf)-1);
690 close(fd);
691 if (retcode) {
692 buf[retcode-1] = '\0';
693 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
694 ;
695 if (*pt) { /* Device is next */
696 *pt = '\0';
697 if (!strcmp(driver, name)) { /* Match */
698 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
699 ;
700 if (*pt) { /* Found busid */
701 return drmOpenByBusid(++pt, type);
702 } else { /* No busid */
703 return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
704 }
705 }
706 }
707 }
708 }
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000709 }
Rik Faith88dbee52001-02-28 09:27:44 +0000710#endif
711
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000712 return -1;
713}
714
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000715
Jose Fonsecad2443b22003-05-27 00:37:33 +0000716/**
717 * Open the DRM device.
718 *
719 * Looks up the specified name and bus ID, and opens the device found. The
720 * entry in /dev/dri is created if necessary and if called by root.
721 *
722 * \param name driver name. Not referenced if bus ID is supplied.
723 * \param busid bus ID. Zero if not known.
Jan Vesely50d3c852016-06-30 14:22:52 -0400724 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000725 * \return a file descriptor on success, or a negative value on error.
Jan Vesely50d3c852016-06-30 14:22:52 -0400726 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000727 * \internal
728 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
729 * otherwise.
730 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000731int drmOpen(const char *name, const char *busid)
Daryll Straussb3a57661999-12-05 01:19:48 +0000732{
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800733 return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
734}
735
736/**
737 * Open the DRM device with specified type.
738 *
739 * Looks up the specified name and bus ID, and opens the device found. The
740 * entry in /dev/dri is created if necessary and if called by root.
741 *
742 * \param name driver name. Not referenced if bus ID is supplied.
743 * \param busid bus ID. Zero if not known.
744 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
745 *
746 * \return a file descriptor on success, or a negative value on error.
747 *
748 * \internal
749 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
750 * otherwise.
751 */
752int drmOpenWithType(const char *name, const char *busid, int type)
753{
Rob Clarkeb7c2d52015-09-04 08:08:02 -0400754 if (!drmAvailable() && name != NULL && drm_server_info &&
755 drm_server_info->load_module) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400756 /* try to load the kernel module */
757 if (!drm_server_info->load_module(name)) {
758 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
759 return -1;
760 }
Eric Anholt06cb1322003-10-23 02:23:31 +0000761 }
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000762
Eric Anholt06cb1322003-10-23 02:23:31 +0000763 if (busid) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400764 int fd = drmOpenByBusid(busid, type);
765 if (fd >= 0)
766 return fd;
Eric Anholt06cb1322003-10-23 02:23:31 +0000767 }
Jan Vesely50d3c852016-06-30 14:22:52 -0400768
Eric Anholt06cb1322003-10-23 02:23:31 +0000769 if (name)
Jan Vesely50d3c852016-06-30 14:22:52 -0400770 return drmOpenByName(name, type);
Adam Jackson22e41ef2006-02-20 23:09:00 +0000771
Eric Anholt06cb1322003-10-23 02:23:31 +0000772 return -1;
Daryll Straussb3a57661999-12-05 01:19:48 +0000773}
774
Jesse Barnes731cd552008-12-17 10:09:49 -0800775int drmOpenControl(int minor)
776{
777 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
778}
Jose Fonsecad2443b22003-05-27 00:37:33 +0000779
Frank Binns0c5aaee2015-01-14 14:07:51 +0000780int drmOpenRender(int minor)
781{
782 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
783}
784
Jose Fonsecad2443b22003-05-27 00:37:33 +0000785/**
786 * Free the version information returned by drmGetVersion().
787 *
788 * \param v pointer to the version information.
789 *
790 * \internal
791 * It frees the memory pointed by \p %v as well as all the non-null strings
792 * pointers in it.
793 */
Daryll Straussb3a57661999-12-05 01:19:48 +0000794void drmFreeVersion(drmVersionPtr v)
795{
Brianccd7b6e2007-05-29 14:54:00 -0600796 if (!v)
Jan Vesely50d3c852016-06-30 14:22:52 -0400797 return;
Jakob Bornecrantz9d8ba2d2007-02-25 10:48:26 +1100798 drmFree(v->name);
799 drmFree(v->date);
800 drmFree(v->desc);
Daryll Straussb3a57661999-12-05 01:19:48 +0000801 drmFree(v);
802}
803
Jose Fonsecad2443b22003-05-27 00:37:33 +0000804
805/**
806 * Free the non-public version information returned by the kernel.
807 *
808 * \param v pointer to the version information.
809 *
810 * \internal
811 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
812 * the non-null strings pointers in it.
813 */
Daryll Straussb3a57661999-12-05 01:19:48 +0000814static void drmFreeKernelVersion(drm_version_t *v)
815{
Brianccd7b6e2007-05-29 14:54:00 -0600816 if (!v)
Jan Vesely50d3c852016-06-30 14:22:52 -0400817 return;
Jakob Bornecrantz9d8ba2d2007-02-25 10:48:26 +1100818 drmFree(v->name);
819 drmFree(v->date);
820 drmFree(v->desc);
Daryll Straussb3a57661999-12-05 01:19:48 +0000821 drmFree(v);
822}
823
Jose Fonsecad2443b22003-05-27 00:37:33 +0000824
825/**
826 * Copy version information.
Jan Vesely50d3c852016-06-30 14:22:52 -0400827 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000828 * \param d destination pointer.
829 * \param s source pointer.
Jan Vesely50d3c852016-06-30 14:22:52 -0400830 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000831 * \internal
832 * Used by drmGetVersion() to translate the information returned by the ioctl
833 * interface in a private structure into the public structure counterpart.
834 */
Brian Paul569da5a2000-06-08 14:38:22 +0000835static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
Daryll Straussb3a57661999-12-05 01:19:48 +0000836{
837 d->version_major = s->version_major;
838 d->version_minor = s->version_minor;
839 d->version_patchlevel = s->version_patchlevel;
840 d->name_len = s->name_len;
Adam Jackson0a1ff352010-10-27 18:44:53 -0400841 d->name = strdup(s->name);
Daryll Straussb3a57661999-12-05 01:19:48 +0000842 d->date_len = s->date_len;
Adam Jackson0a1ff352010-10-27 18:44:53 -0400843 d->date = strdup(s->date);
Daryll Straussb3a57661999-12-05 01:19:48 +0000844 d->desc_len = s->desc_len;
Adam Jackson0a1ff352010-10-27 18:44:53 -0400845 d->desc = strdup(s->desc);
Daryll Straussb3a57661999-12-05 01:19:48 +0000846}
847
Daryll Straussb3a57661999-12-05 01:19:48 +0000848
Jose Fonsecad2443b22003-05-27 00:37:33 +0000849/**
850 * Query the driver version information.
851 *
852 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -0400853 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000854 * \return pointer to a drmVersion structure which should be freed with
855 * drmFreeVersion().
Jan Vesely50d3c852016-06-30 14:22:52 -0400856 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000857 * \note Similar information is available via /proc/dri.
Jan Vesely50d3c852016-06-30 14:22:52 -0400858 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000859 * \internal
860 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
861 * first with zeros to get the string lengths, and then the actually strings.
862 * It also null-terminates them since they might not be already.
863 */
Daryll Straussb3a57661999-12-05 01:19:48 +0000864drmVersionPtr drmGetVersion(int fd)
865{
866 drmVersionPtr retval;
867 drm_version_t *version = drmMalloc(sizeof(*version));
868
Daniel Vetter95f23cf2015-02-11 17:25:30 +0100869 memclear(*version);
Gareth Hughes36047532001-02-15 08:12:14 +0000870
Keith Packard8b9ab102008-06-13 16:03:22 -0700871 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400872 drmFreeKernelVersion(version);
873 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +0000874 }
875
Daryll Straussb3a57661999-12-05 01:19:48 +0000876 if (version->name_len)
Jan Vesely50d3c852016-06-30 14:22:52 -0400877 version->name = drmMalloc(version->name_len + 1);
Daryll Straussb3a57661999-12-05 01:19:48 +0000878 if (version->date_len)
Jan Vesely50d3c852016-06-30 14:22:52 -0400879 version->date = drmMalloc(version->date_len + 1);
Daryll Straussb3a57661999-12-05 01:19:48 +0000880 if (version->desc_len)
Jan Vesely50d3c852016-06-30 14:22:52 -0400881 version->desc = drmMalloc(version->desc_len + 1);
Gareth Hughes36047532001-02-15 08:12:14 +0000882
Keith Packard8b9ab102008-06-13 16:03:22 -0700883 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400884 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
885 drmFreeKernelVersion(version);
886 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +0000887 }
888
Adam Jackson22e41ef2006-02-20 23:09:00 +0000889 /* The results might not be null-terminated strings, so terminate them. */
Daryll Straussb3a57661999-12-05 01:19:48 +0000890 if (version->name_len) version->name[version->name_len] = '\0';
891 if (version->date_len) version->date[version->date_len] = '\0';
892 if (version->desc_len) version->desc[version->desc_len] = '\0';
893
Daryll Straussb3a57661999-12-05 01:19:48 +0000894 retval = drmMalloc(sizeof(*retval));
895 drmCopyVersion(retval, version);
896 drmFreeKernelVersion(version);
897 return retval;
898}
899
Jens Owen3903e5a2002-04-09 21:54:56 +0000900
Jose Fonsecad2443b22003-05-27 00:37:33 +0000901/**
902 * Get version information for the DRM user space library.
Jan Vesely50d3c852016-06-30 14:22:52 -0400903 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000904 * This version number is driver independent.
Jan Vesely50d3c852016-06-30 14:22:52 -0400905 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000906 * \param fd file descriptor.
907 *
908 * \return version information.
Jan Vesely50d3c852016-06-30 14:22:52 -0400909 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000910 * \internal
911 * This function allocates and fills a drm_version structure with a hard coded
912 * version number.
913 */
Jens Owen3903e5a2002-04-09 21:54:56 +0000914drmVersionPtr drmGetLibVersion(int fd)
915{
916 drm_version_t *version = drmMalloc(sizeof(*version));
917
918 /* Version history:
Dave Airlie79038752006-11-08 15:08:09 +1100919 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
Jens Owen3903e5a2002-04-09 21:54:56 +0000920 * revision 1.0.x = original DRM interface with no drmGetLibVersion
921 * entry point and many drm<Device> extensions
922 * revision 1.1.x = added drmCommand entry points for device extensions
923 * added drmGetLibVersion to identify libdrm.a version
Eric Anholt06cb1322003-10-23 02:23:31 +0000924 * revision 1.2.x = added drmSetInterfaceVersion
925 * modified drmOpen to handle both busid and name
Dave Airlie79038752006-11-08 15:08:09 +1100926 * revision 1.3.x = added server + memory manager
Jens Owen3903e5a2002-04-09 21:54:56 +0000927 */
Dave Airlie79038752006-11-08 15:08:09 +1100928 version->version_major = 1;
929 version->version_minor = 3;
Jens Owen3903e5a2002-04-09 21:54:56 +0000930 version->version_patchlevel = 0;
931
932 return (drmVersionPtr)version;
933}
934
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000935int drmGetCap(int fd, uint64_t capability, uint64_t *value)
936{
Jan Vesely50d3c852016-06-30 14:22:52 -0400937 struct drm_get_cap cap;
938 int ret;
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000939
Jan Vesely50d3c852016-06-30 14:22:52 -0400940 memclear(cap);
941 cap.capability = capability;
Daniel Vetterfd387942015-02-11 12:41:04 +0100942
Jan Vesely50d3c852016-06-30 14:22:52 -0400943 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
944 if (ret)
945 return ret;
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000946
Jan Vesely50d3c852016-06-30 14:22:52 -0400947 *value = cap.value;
948 return 0;
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000949}
Jose Fonsecad2443b22003-05-27 00:37:33 +0000950
Damien Lespiauddbbdb12013-09-03 15:34:41 +0100951int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
952{
Jan Vesely50d3c852016-06-30 14:22:52 -0400953 struct drm_set_client_cap cap;
Daniel Vetterfd387942015-02-11 12:41:04 +0100954
Jan Vesely50d3c852016-06-30 14:22:52 -0400955 memclear(cap);
956 cap.capability = capability;
957 cap.value = value;
Damien Lespiauddbbdb12013-09-03 15:34:41 +0100958
Jan Vesely50d3c852016-06-30 14:22:52 -0400959 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
Damien Lespiauddbbdb12013-09-03 15:34:41 +0100960}
961
Jose Fonsecad2443b22003-05-27 00:37:33 +0000962/**
963 * Free the bus ID information.
964 *
965 * \param busid bus ID information string as given by drmGetBusid().
966 *
967 * \internal
968 * This function is just frees the memory pointed by \p busid.
969 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000970void drmFreeBusid(const char *busid)
Daryll Straussb3a57661999-12-05 01:19:48 +0000971{
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000972 drmFree((void *)busid);
Daryll Straussb3a57661999-12-05 01:19:48 +0000973}
974
Jose Fonsecad2443b22003-05-27 00:37:33 +0000975
976/**
977 * Get the bus ID of the device.
978 *
979 * \param fd file descriptor.
980 *
981 * \return bus ID string.
982 *
983 * \internal
984 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
985 * get the string length and data, passing the arguments in a drm_unique
986 * structure.
987 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000988char *drmGetBusid(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +0000989{
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000990 drm_unique_t u;
991
Daniel Vetterfd387942015-02-11 12:41:04 +0100992 memclear(u);
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000993
Keith Packard8b9ab102008-06-13 16:03:22 -0700994 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
Jan Vesely50d3c852016-06-30 14:22:52 -0400995 return NULL;
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000996 u.unique = drmMalloc(u.unique_len + 1);
Keith Packard8b9ab102008-06-13 16:03:22 -0700997 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
Jan Vesely50d3c852016-06-30 14:22:52 -0400998 return NULL;
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000999 u.unique[u.unique_len] = '\0';
Eric Anholt06cb1322003-10-23 02:23:31 +00001000
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001001 return u.unique;
Daryll Straussb3a57661999-12-05 01:19:48 +00001002}
1003
Jose Fonsecad2443b22003-05-27 00:37:33 +00001004
1005/**
1006 * Set the bus ID of the device.
1007 *
1008 * \param fd file descriptor.
1009 * \param busid bus ID string.
1010 *
1011 * \return zero on success, negative on failure.
1012 *
1013 * \internal
1014 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
1015 * the arguments in a drm_unique structure.
1016 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001017int drmSetBusid(int fd, const char *busid)
Daryll Straussb3a57661999-12-05 01:19:48 +00001018{
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001019 drm_unique_t u;
Daryll Straussb3a57661999-12-05 01:19:48 +00001020
Daniel Vetterfd387942015-02-11 12:41:04 +01001021 memclear(u);
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001022 u.unique = (char *)busid;
1023 u.unique_len = strlen(busid);
Daryll Straussb3a57661999-12-05 01:19:48 +00001024
Keith Packard8b9ab102008-06-13 16:03:22 -07001025 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04001026 return -errno;
David Dawes56bd9c22001-07-30 19:59:39 +00001027 }
Daryll Straussb3a57661999-12-05 01:19:48 +00001028 return 0;
1029}
1030
Jon Smirl8696e712004-07-07 04:36:36 +00001031int drmGetMagic(int fd, drm_magic_t * magic)
Daryll Straussb3a57661999-12-05 01:19:48 +00001032{
1033 drm_auth_t auth;
1034
Daniel Vetterfd387942015-02-11 12:41:04 +01001035 memclear(auth);
1036
Daryll Straussb3a57661999-12-05 01:19:48 +00001037 *magic = 0;
Keith Packard8b9ab102008-06-13 16:03:22 -07001038 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
Jan Vesely50d3c852016-06-30 14:22:52 -04001039 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001040 *magic = auth.magic;
1041 return 0;
1042}
1043
Jon Smirl8696e712004-07-07 04:36:36 +00001044int drmAuthMagic(int fd, drm_magic_t magic)
Daryll Straussb3a57661999-12-05 01:19:48 +00001045{
1046 drm_auth_t auth;
1047
Daniel Vetterfd387942015-02-11 12:41:04 +01001048 memclear(auth);
Daryll Straussb3a57661999-12-05 01:19:48 +00001049 auth.magic = magic;
Keith Packard8b9ab102008-06-13 16:03:22 -07001050 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
Jan Vesely50d3c852016-06-30 14:22:52 -04001051 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001052 return 0;
1053}
1054
Jose Fonsecad2443b22003-05-27 00:37:33 +00001055/**
1056 * Specifies a range of memory that is available for mapping by a
1057 * non-root process.
1058 *
1059 * \param fd file descriptor.
1060 * \param offset usually the physical address. The actual meaning depends of
1061 * the \p type parameter. See below.
1062 * \param size of the memory in bytes.
1063 * \param type type of the memory to be mapped.
1064 * \param flags combination of several flags to modify the function actions.
1065 * \param handle will be set to a value that may be used as the offset
1066 * parameter for mmap().
Jan Vesely50d3c852016-06-30 14:22:52 -04001067 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001068 * \return zero on success or a negative value on error.
1069 *
1070 * \par Mapping the frame buffer
1071 * For the frame buffer
1072 * - \p offset will be the physical address of the start of the frame buffer,
1073 * - \p size will be the size of the frame buffer in bytes, and
1074 * - \p type will be DRM_FRAME_BUFFER.
1075 *
1076 * \par
1077 * The area mapped will be uncached. If MTRR support is available in the
Jan Vesely50d3c852016-06-30 14:22:52 -04001078 * kernel, the frame buffer area will be set to write combining.
Jose Fonsecad2443b22003-05-27 00:37:33 +00001079 *
1080 * \par Mapping the MMIO register area
1081 * For the MMIO register area,
1082 * - \p offset will be the physical address of the start of the register area,
1083 * - \p size will be the size of the register area bytes, and
1084 * - \p type will be DRM_REGISTERS.
1085 * \par
Jan Vesely50d3c852016-06-30 14:22:52 -04001086 * The area mapped will be uncached.
1087 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001088 * \par Mapping the SAREA
1089 * For the SAREA,
1090 * - \p offset will be ignored and should be set to zero,
1091 * - \p size will be the desired size of the SAREA in bytes,
1092 * - \p type will be DRM_SHM.
Jan Vesely50d3c852016-06-30 14:22:52 -04001093 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001094 * \par
1095 * A shared memory area of the requested size will be created and locked in
1096 * kernel memory. This area may be mapped into client-space by using the handle
Jan Vesely50d3c852016-06-30 14:22:52 -04001097 * returned.
1098 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001099 * \note May only be called by root.
1100 *
1101 * \internal
1102 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
1103 * the arguments in a drm_map structure.
1104 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00001105int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
Jan Vesely50d3c852016-06-30 14:22:52 -04001106 drmMapFlags flags, drm_handle_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001107{
1108 drm_map_t map;
1109
Daniel Vetterfd387942015-02-11 12:41:04 +01001110 memclear(map);
Daryll Straussb3a57661999-12-05 01:19:48 +00001111 map.offset = offset;
1112 map.size = size;
Daryll Straussb3a57661999-12-05 01:19:48 +00001113 map.type = type;
1114 map.flags = flags;
Keith Packard8b9ab102008-06-13 16:03:22 -07001115 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
Jan Vesely50d3c852016-06-30 14:22:52 -04001116 return -errno;
Brianccd7b6e2007-05-29 14:54:00 -06001117 if (handle)
Jan Vesely50d3c852016-06-30 14:22:52 -04001118 *handle = (drm_handle_t)(uintptr_t)map.handle;
Daryll Straussb3a57661999-12-05 01:19:48 +00001119 return 0;
1120}
1121
Jon Smirl8696e712004-07-07 04:36:36 +00001122int drmRmMap(int fd, drm_handle_t handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +00001123{
1124 drm_map_t map;
1125
Daniel Vetterfd387942015-02-11 12:41:04 +01001126 memclear(map);
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -07001127 map.handle = (void *)(uintptr_t)handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +00001128
Keith Packard8b9ab102008-06-13 16:03:22 -07001129 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
Jan Vesely50d3c852016-06-30 14:22:52 -04001130 return -errno;
Kevin E Martin74e19a42001-03-14 22:22:50 +00001131 return 0;
1132}
1133
Jose Fonsecad2443b22003-05-27 00:37:33 +00001134/**
1135 * Make buffers available for DMA transfers.
Jan Vesely50d3c852016-06-30 14:22:52 -04001136 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001137 * \param fd file descriptor.
1138 * \param count number of buffers.
1139 * \param size size of each buffer.
1140 * \param flags buffer allocation flags.
Jan Vesely50d3c852016-06-30 14:22:52 -04001141 * \param agp_offset offset in the AGP aperture
Jose Fonsecad2443b22003-05-27 00:37:33 +00001142 *
1143 * \return number of buffers allocated, negative on error.
1144 *
1145 * \internal
1146 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1147 *
1148 * \sa drm_buf_desc.
1149 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001150int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
Jan Vesely50d3c852016-06-30 14:22:52 -04001151 int agp_offset)
Daryll Straussb3a57661999-12-05 01:19:48 +00001152{
1153 drm_buf_desc_t request;
Gareth Hughes36047532001-02-15 08:12:14 +00001154
Daniel Vetterfd387942015-02-11 12:41:04 +01001155 memclear(request);
Daryll Straussb3a57661999-12-05 01:19:48 +00001156 request.count = count;
1157 request.size = size;
Daryll Straussb3a57661999-12-05 01:19:48 +00001158 request.flags = flags;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001159 request.agp_start = agp_offset;
Gareth Hughes36047532001-02-15 08:12:14 +00001160
Keith Packard8b9ab102008-06-13 16:03:22 -07001161 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
Jan Vesely50d3c852016-06-30 14:22:52 -04001162 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001163 return request.count;
1164}
1165
1166int drmMarkBufs(int fd, double low, double high)
1167{
1168 drm_buf_info_t info;
1169 int i;
1170
Daniel Vetterfd387942015-02-11 12:41:04 +01001171 memclear(info);
Daryll Straussb3a57661999-12-05 01:19:48 +00001172
Keith Packard8b9ab102008-06-13 16:03:22 -07001173 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
Jan Vesely50d3c852016-06-30 14:22:52 -04001174 return -EINVAL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001175
Brianccd7b6e2007-05-29 14:54:00 -06001176 if (!info.count)
Jan Vesely50d3c852016-06-30 14:22:52 -04001177 return -EINVAL;
Gareth Hughes36047532001-02-15 08:12:14 +00001178
Daryll Straussb3a57661999-12-05 01:19:48 +00001179 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
Jan Vesely50d3c852016-06-30 14:22:52 -04001180 return -ENOMEM;
Gareth Hughes36047532001-02-15 08:12:14 +00001181
Keith Packard8b9ab102008-06-13 16:03:22 -07001182 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04001183 int retval = -errno;
1184 drmFree(info.list);
1185 return retval;
Daryll Straussb3a57661999-12-05 01:19:48 +00001186 }
Gareth Hughes36047532001-02-15 08:12:14 +00001187
Daryll Straussb3a57661999-12-05 01:19:48 +00001188 for (i = 0; i < info.count; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -04001189 info.list[i].low_mark = low * info.list[i].count;
1190 info.list[i].high_mark = high * info.list[i].count;
1191 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1192 int retval = -errno;
1193 drmFree(info.list);
1194 return retval;
1195 }
Daryll Straussb3a57661999-12-05 01:19:48 +00001196 }
1197 drmFree(info.list);
Gareth Hughes36047532001-02-15 08:12:14 +00001198
Daryll Straussb3a57661999-12-05 01:19:48 +00001199 return 0;
1200}
1201
Jose Fonsecad2443b22003-05-27 00:37:33 +00001202/**
1203 * Free buffers.
1204 *
1205 * \param fd file descriptor.
1206 * \param count number of buffers to free.
1207 * \param list list of buffers to be freed.
1208 *
1209 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001210 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001211 * \note This function is primarily used for debugging.
Jan Vesely50d3c852016-06-30 14:22:52 -04001212 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001213 * \internal
1214 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1215 * the arguments in a drm_buf_free structure.
1216 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001217int drmFreeBufs(int fd, int count, int *list)
1218{
1219 drm_buf_free_t request;
1220
Daniel Vetterfd387942015-02-11 12:41:04 +01001221 memclear(request);
Daryll Straussb3a57661999-12-05 01:19:48 +00001222 request.count = count;
1223 request.list = list;
Keith Packard8b9ab102008-06-13 16:03:22 -07001224 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
Jan Vesely50d3c852016-06-30 14:22:52 -04001225 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001226 return 0;
1227}
1228
Jose Fonsecad2443b22003-05-27 00:37:33 +00001229
1230/**
1231 * Close the device.
1232 *
1233 * \param fd file descriptor.
1234 *
1235 * \internal
1236 * This function closes the file descriptor.
1237 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001238int drmClose(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +00001239{
1240 unsigned long key = drmGetKeyFromFd(fd);
1241 drmHashEntry *entry = drmGetEntry(fd);
1242
1243 drmHashDestroy(entry->tagTable);
1244 entry->fd = 0;
1245 entry->f = NULL;
1246 entry->tagTable = NULL;
1247
1248 drmHashDelete(drmHashTable, key);
1249 drmFree(entry);
1250
1251 return close(fd);
1252}
1253
Jose Fonsecad2443b22003-05-27 00:37:33 +00001254
1255/**
1256 * Map a region of memory.
1257 *
1258 * \param fd file descriptor.
1259 * \param handle handle returned by drmAddMap().
1260 * \param size size in bytes. Must match the size used by drmAddMap().
1261 * \param address will contain the user-space virtual address where the mapping
1262 * begins.
1263 *
1264 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001265 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001266 * \internal
1267 * This function is a wrapper for mmap().
1268 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00001269int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
Daryll Straussb3a57661999-12-05 01:19:48 +00001270{
Alan Hourihanec7558d82000-09-24 09:34:10 +00001271 static unsigned long pagesize_mask = 0;
1272
Brianccd7b6e2007-05-29 14:54:00 -06001273 if (fd < 0)
Jan Vesely50d3c852016-06-30 14:22:52 -04001274 return -EINVAL;
Alan Hourihanec7558d82000-09-24 09:34:10 +00001275
1276 if (!pagesize_mask)
Jan Vesely50d3c852016-06-30 14:22:52 -04001277 pagesize_mask = getpagesize() - 1;
Alan Hourihanec7558d82000-09-24 09:34:10 +00001278
1279 size = (size + pagesize_mask) & ~pagesize_mask;
1280
Emil Velikovfaf51d52014-09-07 20:03:05 +01001281 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
Brianccd7b6e2007-05-29 14:54:00 -06001282 if (*address == MAP_FAILED)
Jan Vesely50d3c852016-06-30 14:22:52 -04001283 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001284 return 0;
1285}
1286
Jose Fonsecad2443b22003-05-27 00:37:33 +00001287
1288/**
1289 * Unmap mappings obtained with drmMap().
1290 *
1291 * \param address address as given by drmMap().
1292 * \param size size in bytes. Must match the size used by drmMap().
Jan Vesely50d3c852016-06-30 14:22:52 -04001293 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001294 * \return zero on success, or a negative value on failure.
1295 *
1296 * \internal
Adam Jackson22e41ef2006-02-20 23:09:00 +00001297 * This function is a wrapper for munmap().
Jose Fonsecad2443b22003-05-27 00:37:33 +00001298 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001299int drmUnmap(drmAddress address, drmSize size)
1300{
Emil Velikovfaf51d52014-09-07 20:03:05 +01001301 return drm_munmap(address, size);
Daryll Straussb3a57661999-12-05 01:19:48 +00001302}
1303
1304drmBufInfoPtr drmGetBufInfo(int fd)
1305{
1306 drm_buf_info_t info;
1307 drmBufInfoPtr retval;
1308 int i;
1309
Daniel Vetterfd387942015-02-11 12:41:04 +01001310 memclear(info);
Daryll Straussb3a57661999-12-05 01:19:48 +00001311
Keith Packard8b9ab102008-06-13 16:03:22 -07001312 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
Jan Vesely50d3c852016-06-30 14:22:52 -04001313 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001314
1315 if (info.count) {
Jan Vesely50d3c852016-06-30 14:22:52 -04001316 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1317 return NULL;
Gareth Hughes36047532001-02-15 08:12:14 +00001318
Jan Vesely50d3c852016-06-30 14:22:52 -04001319 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1320 drmFree(info.list);
1321 return NULL;
1322 }
Adam Jackson22e41ef2006-02-20 23:09:00 +00001323
Jan Vesely50d3c852016-06-30 14:22:52 -04001324 retval = drmMalloc(sizeof(*retval));
1325 retval->count = info.count;
1326 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1327 for (i = 0; i < info.count; i++) {
1328 retval->list[i].count = info.list[i].count;
1329 retval->list[i].size = info.list[i].size;
1330 retval->list[i].low_mark = info.list[i].low_mark;
1331 retval->list[i].high_mark = info.list[i].high_mark;
1332 }
1333 drmFree(info.list);
1334 return retval;
Daryll Straussb3a57661999-12-05 01:19:48 +00001335 }
1336 return NULL;
1337}
1338
Jose Fonsecad2443b22003-05-27 00:37:33 +00001339/**
1340 * Map all DMA buffers into client-virtual space.
1341 *
1342 * \param fd file descriptor.
1343 *
1344 * \return a pointer to a ::drmBufMap structure.
1345 *
1346 * \note The client may not use these buffers until obtaining buffer indices
1347 * with drmDMA().
Jan Vesely50d3c852016-06-30 14:22:52 -04001348 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001349 * \internal
1350 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1351 * information about the buffers in a drm_buf_map structure into the
1352 * client-visible data structures.
Jan Vesely50d3c852016-06-30 14:22:52 -04001353 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001354drmBufMapPtr drmMapBufs(int fd)
1355{
1356 drm_buf_map_t bufs;
1357 drmBufMapPtr retval;
1358 int i;
Gareth Hughes36047532001-02-15 08:12:14 +00001359
Daniel Vetterfd387942015-02-11 12:41:04 +01001360 memclear(bufs);
Keith Packard8b9ab102008-06-13 16:03:22 -07001361 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
Jan Vesely50d3c852016-06-30 14:22:52 -04001362 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001363
Brianccd7b6e2007-05-29 14:54:00 -06001364 if (!bufs.count)
Jan Vesely50d3c852016-06-30 14:22:52 -04001365 return NULL;
Jon Smirl8696e712004-07-07 04:36:36 +00001366
Jan Vesely50d3c852016-06-30 14:22:52 -04001367 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1368 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001369
Jan Vesely50d3c852016-06-30 14:22:52 -04001370 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1371 drmFree(bufs.list);
1372 return NULL;
1373 }
Adam Jackson22e41ef2006-02-20 23:09:00 +00001374
Jan Vesely50d3c852016-06-30 14:22:52 -04001375 retval = drmMalloc(sizeof(*retval));
1376 retval->count = bufs.count;
1377 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1378 for (i = 0; i < bufs.count; i++) {
1379 retval->list[i].idx = bufs.list[i].idx;
1380 retval->list[i].total = bufs.list[i].total;
1381 retval->list[i].used = 0;
1382 retval->list[i].address = bufs.list[i].address;
1383 }
Jon Smirl8696e712004-07-07 04:36:36 +00001384
Jan Vesely50d3c852016-06-30 14:22:52 -04001385 drmFree(bufs.list);
1386 return retval;
Daryll Straussb3a57661999-12-05 01:19:48 +00001387}
1388
Jose Fonsecad2443b22003-05-27 00:37:33 +00001389
1390/**
1391 * Unmap buffers allocated with drmMapBufs().
1392 *
1393 * \return zero on success, or negative value on failure.
1394 *
1395 * \internal
Jon Smirl8696e712004-07-07 04:36:36 +00001396 * Calls munmap() for every buffer stored in \p bufs and frees the
1397 * memory allocated by drmMapBufs().
Jose Fonsecad2443b22003-05-27 00:37:33 +00001398 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001399int drmUnmapBufs(drmBufMapPtr bufs)
1400{
1401 int i;
Gareth Hughes36047532001-02-15 08:12:14 +00001402
Daryll Straussb3a57661999-12-05 01:19:48 +00001403 for (i = 0; i < bufs->count; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -04001404 drm_munmap(bufs->list[i].address, bufs->list[i].total);
Daryll Straussb3a57661999-12-05 01:19:48 +00001405 }
Jon Smirl8696e712004-07-07 04:36:36 +00001406
1407 drmFree(bufs->list);
1408 drmFree(bufs);
Daryll Straussb3a57661999-12-05 01:19:48 +00001409 return 0;
1410}
1411
Jose Fonsecad2443b22003-05-27 00:37:33 +00001412
Jan Vesely50d3c852016-06-30 14:22:52 -04001413#define DRM_DMA_RETRY 16
Gareth Hughes36047532001-02-15 08:12:14 +00001414
Jose Fonsecad2443b22003-05-27 00:37:33 +00001415/**
1416 * Reserve DMA buffers.
1417 *
1418 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001419 * \param request
1420 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001421 * \return zero on success, or a negative value on failure.
1422 *
1423 * \internal
1424 * Assemble the arguments into a drm_dma structure and keeps issuing the
1425 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1426 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001427int drmDMA(int fd, drmDMAReqPtr request)
1428{
1429 drm_dma_t dma;
Gareth Hughes36047532001-02-15 08:12:14 +00001430 int ret, i = 0;
Daryll Straussb3a57661999-12-05 01:19:48 +00001431
Daryll Straussb3a57661999-12-05 01:19:48 +00001432 dma.context = request->context;
1433 dma.send_count = request->send_count;
1434 dma.send_indices = request->send_list;
1435 dma.send_sizes = request->send_sizes;
1436 dma.flags = request->flags;
1437 dma.request_count = request->request_count;
1438 dma.request_size = request->request_size;
1439 dma.request_indices = request->request_list;
1440 dma.request_sizes = request->request_sizes;
Jon Smirl8696e712004-07-07 04:36:36 +00001441 dma.granted_count = 0;
Gareth Hughes36047532001-02-15 08:12:14 +00001442
1443 do {
Jan Vesely50d3c852016-06-30 14:22:52 -04001444 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
Gareth Hughes36047532001-02-15 08:12:14 +00001445 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1446
1447 if ( ret == 0 ) {
Jan Vesely50d3c852016-06-30 14:22:52 -04001448 request->granted_count = dma.granted_count;
1449 return 0;
Gareth Hughes36047532001-02-15 08:12:14 +00001450 } else {
Jan Vesely50d3c852016-06-30 14:22:52 -04001451 return -errno;
Gareth Hughes36047532001-02-15 08:12:14 +00001452 }
Daryll Straussb3a57661999-12-05 01:19:48 +00001453}
1454
Jose Fonsecad2443b22003-05-27 00:37:33 +00001455
1456/**
1457 * Obtain heavyweight hardware lock.
1458 *
1459 * \param fd file descriptor.
1460 * \param context context.
1461 * \param flags flags that determine the sate of the hardware when the function
1462 * returns.
Jan Vesely50d3c852016-06-30 14:22:52 -04001463 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001464 * \return always zero.
Jan Vesely50d3c852016-06-30 14:22:52 -04001465 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001466 * \internal
1467 * This function translates the arguments into a drm_lock structure and issue
1468 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1469 */
Jon Smirl8696e712004-07-07 04:36:36 +00001470int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001471{
1472 drm_lock_t lock;
1473
Daniel Vetterfd387942015-02-11 12:41:04 +01001474 memclear(lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00001475 lock.context = context;
1476 lock.flags = 0;
1477 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1478 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1479 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1480 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1481 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1482 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
Gareth Hughes36047532001-02-15 08:12:14 +00001483
Keith Packard8b9ab102008-06-13 16:03:22 -07001484 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
Jan Vesely50d3c852016-06-30 14:22:52 -04001485 ;
Daryll Straussb3a57661999-12-05 01:19:48 +00001486 return 0;
1487}
1488
Jose Fonsecad2443b22003-05-27 00:37:33 +00001489/**
1490 * Release the hardware lock.
1491 *
1492 * \param fd file descriptor.
1493 * \param context context.
Jan Vesely50d3c852016-06-30 14:22:52 -04001494 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001495 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001496 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001497 * \internal
1498 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1499 * argument in a drm_lock structure.
1500 */
Jon Smirl8696e712004-07-07 04:36:36 +00001501int drmUnlock(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00001502{
1503 drm_lock_t lock;
1504
Daniel Vetterfd387942015-02-11 12:41:04 +01001505 memclear(lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00001506 lock.context = context;
Keith Packard8b9ab102008-06-13 16:03:22 -07001507 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00001508}
1509
Adam Jackson22e41ef2006-02-20 23:09:00 +00001510drm_context_t *drmGetReservedContextList(int fd, int *count)
Daryll Straussb3a57661999-12-05 01:19:48 +00001511{
1512 drm_ctx_res_t res;
1513 drm_ctx_t *list;
Jon Smirl8696e712004-07-07 04:36:36 +00001514 drm_context_t * retval;
Daryll Straussb3a57661999-12-05 01:19:48 +00001515 int i;
1516
Daniel Vetterfd387942015-02-11 12:41:04 +01001517 memclear(res);
Keith Packard8b9ab102008-06-13 16:03:22 -07001518 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
Jan Vesely50d3c852016-06-30 14:22:52 -04001519 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001520
Brianccd7b6e2007-05-29 14:54:00 -06001521 if (!res.count)
Jan Vesely50d3c852016-06-30 14:22:52 -04001522 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001523
Brianccd7b6e2007-05-29 14:54:00 -06001524 if (!(list = drmMalloc(res.count * sizeof(*list))))
Jan Vesely50d3c852016-06-30 14:22:52 -04001525 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001526 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
Jan Vesely50d3c852016-06-30 14:22:52 -04001527 drmFree(list);
1528 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001529 }
1530
1531 res.contexts = list;
Keith Packard8b9ab102008-06-13 16:03:22 -07001532 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
Jan Vesely50d3c852016-06-30 14:22:52 -04001533 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001534
Brianccd7b6e2007-05-29 14:54:00 -06001535 for (i = 0; i < res.count; i++)
Jan Vesely50d3c852016-06-30 14:22:52 -04001536 retval[i] = list[i].handle;
Daryll Straussb3a57661999-12-05 01:19:48 +00001537 drmFree(list);
1538
1539 *count = res.count;
1540 return retval;
1541}
1542
Adam Jackson22e41ef2006-02-20 23:09:00 +00001543void drmFreeReservedContextList(drm_context_t *pt)
Daryll Straussb3a57661999-12-05 01:19:48 +00001544{
1545 drmFree(pt);
1546}
1547
Jose Fonsecad2443b22003-05-27 00:37:33 +00001548/**
1549 * Create context.
1550 *
1551 * Used by the X server during GLXContext initialization. This causes
1552 * per-context kernel-level resources to be allocated.
1553 *
1554 * \param fd file descriptor.
1555 * \param handle is set on success. To be used by the client when requesting DMA
1556 * dispatch with drmDMA().
Jan Vesely50d3c852016-06-30 14:22:52 -04001557 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001558 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001559 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001560 * \note May only be called by root.
Jan Vesely50d3c852016-06-30 14:22:52 -04001561 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001562 * \internal
1563 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1564 * argument in a drm_ctx structure.
1565 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00001566int drmCreateContext(int fd, drm_context_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001567{
1568 drm_ctx_t ctx;
1569
Daniel Vetterfd387942015-02-11 12:41:04 +01001570 memclear(ctx);
Keith Packard8b9ab102008-06-13 16:03:22 -07001571 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
Jan Vesely50d3c852016-06-30 14:22:52 -04001572 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001573 *handle = ctx.handle;
1574 return 0;
1575}
1576
Jon Smirl8696e712004-07-07 04:36:36 +00001577int drmSwitchToContext(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00001578{
1579 drm_ctx_t ctx;
1580
Daniel Vetterfd387942015-02-11 12:41:04 +01001581 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001582 ctx.handle = context;
Keith Packard8b9ab102008-06-13 16:03:22 -07001583 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
Jan Vesely50d3c852016-06-30 14:22:52 -04001584 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001585 return 0;
1586}
1587
Jon Smirl8696e712004-07-07 04:36:36 +00001588int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001589{
1590 drm_ctx_t ctx;
1591
Adam Jackson22e41ef2006-02-20 23:09:00 +00001592 /*
1593 * Context preserving means that no context switches are done between DMA
1594 * buffers from one context and the next. This is suitable for use in the
1595 * X server (which promises to maintain hardware context), or in the
1596 * client-side library when buffers are swapped on behalf of two threads.
1597 */
Daniel Vetterfd387942015-02-11 12:41:04 +01001598 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001599 ctx.handle = context;
Brianccd7b6e2007-05-29 14:54:00 -06001600 if (flags & DRM_CONTEXT_PRESERVED)
Jan Vesely50d3c852016-06-30 14:22:52 -04001601 ctx.flags |= _DRM_CONTEXT_PRESERVED;
Brianccd7b6e2007-05-29 14:54:00 -06001602 if (flags & DRM_CONTEXT_2DONLY)
Jan Vesely50d3c852016-06-30 14:22:52 -04001603 ctx.flags |= _DRM_CONTEXT_2DONLY;
Keith Packard8b9ab102008-06-13 16:03:22 -07001604 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
Jan Vesely50d3c852016-06-30 14:22:52 -04001605 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001606 return 0;
1607}
1608
Adam Jackson22e41ef2006-02-20 23:09:00 +00001609int drmGetContextFlags(int fd, drm_context_t context,
1610 drm_context_tFlagsPtr flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001611{
1612 drm_ctx_t ctx;
1613
Daniel Vetterfd387942015-02-11 12:41:04 +01001614 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001615 ctx.handle = context;
Keith Packard8b9ab102008-06-13 16:03:22 -07001616 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
Jan Vesely50d3c852016-06-30 14:22:52 -04001617 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001618 *flags = 0;
Brianccd7b6e2007-05-29 14:54:00 -06001619 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
Jan Vesely50d3c852016-06-30 14:22:52 -04001620 *flags |= DRM_CONTEXT_PRESERVED;
Brianccd7b6e2007-05-29 14:54:00 -06001621 if (ctx.flags & _DRM_CONTEXT_2DONLY)
Jan Vesely50d3c852016-06-30 14:22:52 -04001622 *flags |= DRM_CONTEXT_2DONLY;
Daryll Straussb3a57661999-12-05 01:19:48 +00001623 return 0;
1624}
Gareth Hughes36047532001-02-15 08:12:14 +00001625
Jose Fonsecad2443b22003-05-27 00:37:33 +00001626/**
1627 * Destroy context.
1628 *
1629 * Free any kernel-level resources allocated with drmCreateContext() associated
1630 * with the context.
Jan Vesely50d3c852016-06-30 14:22:52 -04001631 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001632 * \param fd file descriptor.
1633 * \param handle handle given by drmCreateContext().
Jan Vesely50d3c852016-06-30 14:22:52 -04001634 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001635 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001636 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001637 * \note May only be called by root.
Jan Vesely50d3c852016-06-30 14:22:52 -04001638 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001639 * \internal
1640 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1641 * argument in a drm_ctx structure.
1642 */
Jon Smirl8696e712004-07-07 04:36:36 +00001643int drmDestroyContext(int fd, drm_context_t handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001644{
1645 drm_ctx_t ctx;
Daniel Vetterfd387942015-02-11 12:41:04 +01001646
1647 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001648 ctx.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001649 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
Jan Vesely50d3c852016-06-30 14:22:52 -04001650 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001651 return 0;
1652}
1653
Adam Jackson22e41ef2006-02-20 23:09:00 +00001654int drmCreateDrawable(int fd, drm_drawable_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001655{
1656 drm_draw_t draw;
Daniel Vetterfd387942015-02-11 12:41:04 +01001657
1658 memclear(draw);
Keith Packard8b9ab102008-06-13 16:03:22 -07001659 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
Jan Vesely50d3c852016-06-30 14:22:52 -04001660 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001661 *handle = draw.handle;
1662 return 0;
1663}
1664
Jon Smirl8696e712004-07-07 04:36:36 +00001665int drmDestroyDrawable(int fd, drm_drawable_t handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001666{
1667 drm_draw_t draw;
Daniel Vetterfd387942015-02-11 12:41:04 +01001668
1669 memclear(draw);
Daryll Straussb3a57661999-12-05 01:19:48 +00001670 draw.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001671 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
Jan Vesely50d3c852016-06-30 14:22:52 -04001672 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001673 return 0;
1674}
1675
Michel Dänzer9810ec22006-08-22 16:40:07 +02001676int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
Jan Vesely50d3c852016-06-30 14:22:52 -04001677 drm_drawable_info_type_t type, unsigned int num,
1678 void *data)
Michel Dänzer9810ec22006-08-22 16:40:07 +02001679{
1680 drm_update_draw_t update;
1681
Daniel Vetterfd387942015-02-11 12:41:04 +01001682 memclear(update);
Michel Dänzer9810ec22006-08-22 16:40:07 +02001683 update.handle = handle;
1684 update.type = type;
1685 update.num = num;
1686 update.data = (unsigned long long)(unsigned long)data;
1687
Keith Packard8b9ab102008-06-13 16:03:22 -07001688 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
Jan Vesely50d3c852016-06-30 14:22:52 -04001689 return -errno;
Michel Dänzer9810ec22006-08-22 16:40:07 +02001690
1691 return 0;
1692}
1693
Jose Fonsecad2443b22003-05-27 00:37:33 +00001694/**
1695 * Acquire the AGP device.
1696 *
1697 * Must be called before any of the other AGP related calls.
1698 *
1699 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001700 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001701 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001702 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001703 * \internal
1704 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1705 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001706int drmAgpAcquire(int fd)
1707{
Keith Packard8b9ab102008-06-13 16:03:22 -07001708 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
Jan Vesely50d3c852016-06-30 14:22:52 -04001709 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001710 return 0;
1711}
1712
Jose Fonsecad2443b22003-05-27 00:37:33 +00001713
1714/**
1715 * Release the AGP device.
1716 *
1717 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001718 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001719 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001720 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001721 * \internal
1722 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1723 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001724int drmAgpRelease(int fd)
1725{
Keith Packard8b9ab102008-06-13 16:03:22 -07001726 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
Jan Vesely50d3c852016-06-30 14:22:52 -04001727 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001728 return 0;
1729}
1730
Jose Fonsecad2443b22003-05-27 00:37:33 +00001731
1732/**
1733 * Set the AGP mode.
1734 *
1735 * \param fd file descriptor.
1736 * \param mode AGP mode.
Jan Vesely50d3c852016-06-30 14:22:52 -04001737 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001738 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001739 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001740 * \internal
1741 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1742 * argument in a drm_agp_mode structure.
1743 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001744int drmAgpEnable(int fd, unsigned long mode)
1745{
1746 drm_agp_mode_t m;
1747
Connor Behan14900552015-03-24 13:53:51 -04001748 memclear(m);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001749 m.mode = mode;
Keith Packard8b9ab102008-06-13 16:03:22 -07001750 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
Jan Vesely50d3c852016-06-30 14:22:52 -04001751 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001752 return 0;
1753}
1754
Jose Fonsecad2443b22003-05-27 00:37:33 +00001755
1756/**
1757 * Allocate a chunk of AGP memory.
1758 *
1759 * \param fd file descriptor.
1760 * \param size requested memory size in bytes. Will be rounded to page boundary.
1761 * \param type type of memory to allocate.
1762 * \param address if not zero, will be set to the physical address of the
1763 * allocated memory.
1764 * \param handle on success will be set to a handle of the allocated memory.
Jan Vesely50d3c852016-06-30 14:22:52 -04001765 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001766 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001767 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001768 * \internal
1769 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1770 * arguments in a drm_agp_buffer structure.
1771 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001772int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
Jan Vesely50d3c852016-06-30 14:22:52 -04001773 unsigned long *address, drm_handle_t *handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001774{
1775 drm_agp_buffer_t b;
Alan Hourihaneb0a92852003-09-24 14:39:25 +00001776
Daniel Vetterfd387942015-02-11 12:41:04 +01001777 memclear(b);
Alan Hourihaneb0a92852003-09-24 14:39:25 +00001778 *handle = DRM_AGP_NO_HANDLE;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001779 b.size = size;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001780 b.type = type;
Keith Packard8b9ab102008-06-13 16:03:22 -07001781 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
Jan Vesely50d3c852016-06-30 14:22:52 -04001782 return -errno;
Brianccd7b6e2007-05-29 14:54:00 -06001783 if (address != 0UL)
Jan Vesely50d3c852016-06-30 14:22:52 -04001784 *address = b.physical;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001785 *handle = b.handle;
1786 return 0;
1787}
1788
Jose Fonsecad2443b22003-05-27 00:37:33 +00001789
1790/**
1791 * Free a chunk of AGP memory.
1792 *
1793 * \param fd file descriptor.
1794 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
Jan Vesely50d3c852016-06-30 14:22:52 -04001795 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001796 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001797 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001798 * \internal
1799 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1800 * argument in a drm_agp_buffer structure.
1801 */
Dave Airlie7ede2092005-11-29 09:50:47 +00001802int drmAgpFree(int fd, drm_handle_t handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001803{
1804 drm_agp_buffer_t b;
1805
Daniel Vetterfd387942015-02-11 12:41:04 +01001806 memclear(b);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001807 b.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001808 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
Jan Vesely50d3c852016-06-30 14:22:52 -04001809 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001810 return 0;
1811}
1812
Jose Fonsecad2443b22003-05-27 00:37:33 +00001813
1814/**
1815 * Bind a chunk of AGP memory.
1816 *
1817 * \param fd file descriptor.
1818 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1819 * \param offset offset in bytes. It will round to page boundary.
Jan Vesely50d3c852016-06-30 14:22:52 -04001820 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001821 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001822 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001823 * \internal
1824 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1825 * argument in a drm_agp_binding structure.
1826 */
Dave Airlie7ede2092005-11-29 09:50:47 +00001827int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001828{
1829 drm_agp_binding_t b;
1830
Daniel Vetterfd387942015-02-11 12:41:04 +01001831 memclear(b);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001832 b.handle = handle;
1833 b.offset = offset;
Keith Packard8b9ab102008-06-13 16:03:22 -07001834 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
Jan Vesely50d3c852016-06-30 14:22:52 -04001835 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001836 return 0;
1837}
1838
Jose Fonsecad2443b22003-05-27 00:37:33 +00001839
1840/**
1841 * Unbind a chunk of AGP memory.
1842 *
1843 * \param fd file descriptor.
1844 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
Jan Vesely50d3c852016-06-30 14:22:52 -04001845 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001846 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001847 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001848 * \internal
1849 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1850 * the argument in a drm_agp_binding structure.
1851 */
Dave Airlie7ede2092005-11-29 09:50:47 +00001852int drmAgpUnbind(int fd, drm_handle_t handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001853{
1854 drm_agp_binding_t b;
1855
Daniel Vetterfd387942015-02-11 12:41:04 +01001856 memclear(b);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001857 b.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001858 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
Jan Vesely50d3c852016-06-30 14:22:52 -04001859 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001860 return 0;
1861}
1862
Jose Fonsecad2443b22003-05-27 00:37:33 +00001863
1864/**
1865 * Get AGP driver major version number.
1866 *
1867 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001868 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001869 * \return major version number on success, or a negative value on failure..
Jan Vesely50d3c852016-06-30 14:22:52 -04001870 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001871 * \internal
1872 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1873 * necessary information in a drm_agp_info structure.
1874 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001875int drmAgpVersionMajor(int fd)
1876{
1877 drm_agp_info_t i;
1878
Daniel Vetterfd387942015-02-11 12:41:04 +01001879 memclear(i);
1880
Keith Packard8b9ab102008-06-13 16:03:22 -07001881 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04001882 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001883 return i.agp_version_major;
1884}
1885
Jose Fonsecad2443b22003-05-27 00:37:33 +00001886
1887/**
1888 * Get AGP driver minor version number.
1889 *
1890 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001891 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001892 * \return minor version number on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001893 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001894 * \internal
1895 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1896 * necessary information in a drm_agp_info structure.
1897 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001898int drmAgpVersionMinor(int fd)
1899{
1900 drm_agp_info_t i;
1901
Daniel Vetterfd387942015-02-11 12:41:04 +01001902 memclear(i);
1903
Keith Packard8b9ab102008-06-13 16:03:22 -07001904 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04001905 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001906 return i.agp_version_minor;
1907}
1908
Jose Fonsecad2443b22003-05-27 00:37:33 +00001909
1910/**
1911 * Get AGP mode.
1912 *
1913 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001914 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001915 * \return mode on success, or zero on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001916 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001917 * \internal
1918 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1919 * necessary information in a drm_agp_info structure.
1920 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001921unsigned long drmAgpGetMode(int fd)
1922{
1923 drm_agp_info_t i;
1924
Daniel Vetterfd387942015-02-11 12:41:04 +01001925 memclear(i);
1926
Keith Packard8b9ab102008-06-13 16:03:22 -07001927 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04001928 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001929 return i.mode;
1930}
1931
Jose Fonsecad2443b22003-05-27 00:37:33 +00001932
1933/**
1934 * Get AGP aperture base.
1935 *
1936 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001937 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001938 * \return aperture base on success, zero on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001939 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001940 * \internal
1941 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1942 * necessary information in a drm_agp_info structure.
1943 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001944unsigned long drmAgpBase(int fd)
1945{
1946 drm_agp_info_t i;
1947
Daniel Vetterfd387942015-02-11 12:41:04 +01001948 memclear(i);
1949
Keith Packard8b9ab102008-06-13 16:03:22 -07001950 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04001951 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001952 return i.aperture_base;
1953}
1954
Jose Fonsecad2443b22003-05-27 00:37:33 +00001955
1956/**
1957 * Get AGP aperture size.
1958 *
1959 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001960 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001961 * \return aperture size on success, zero on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001962 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001963 * \internal
1964 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1965 * necessary information in a drm_agp_info structure.
1966 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001967unsigned long drmAgpSize(int fd)
1968{
1969 drm_agp_info_t i;
1970
Daniel Vetterfd387942015-02-11 12:41:04 +01001971 memclear(i);
1972
Keith Packard8b9ab102008-06-13 16:03:22 -07001973 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04001974 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001975 return i.aperture_size;
1976}
1977
Jose Fonsecad2443b22003-05-27 00:37:33 +00001978
1979/**
1980 * Get used AGP memory.
1981 *
1982 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001983 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001984 * \return memory used on success, or zero on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001985 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001986 * \internal
1987 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1988 * necessary information in a drm_agp_info structure.
1989 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001990unsigned long drmAgpMemoryUsed(int fd)
1991{
1992 drm_agp_info_t i;
1993
Daniel Vetterfd387942015-02-11 12:41:04 +01001994 memclear(i);
1995
Keith Packard8b9ab102008-06-13 16:03:22 -07001996 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04001997 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001998 return i.memory_used;
1999}
2000
Jose Fonsecad2443b22003-05-27 00:37:33 +00002001
2002/**
2003 * Get available AGP memory.
2004 *
2005 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002006 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002007 * \return memory available on success, or zero on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002008 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002009 * \internal
2010 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2011 * necessary information in a drm_agp_info structure.
2012 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002013unsigned long drmAgpMemoryAvail(int fd)
2014{
2015 drm_agp_info_t i;
2016
Daniel Vetterfd387942015-02-11 12:41:04 +01002017 memclear(i);
2018
Keith Packard8b9ab102008-06-13 16:03:22 -07002019 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04002020 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002021 return i.memory_allowed;
2022}
2023
Jose Fonsecad2443b22003-05-27 00:37:33 +00002024
2025/**
2026 * Get hardware vendor ID.
2027 *
2028 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002029 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002030 * \return vendor ID on success, or zero on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002031 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002032 * \internal
2033 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2034 * necessary information in a drm_agp_info structure.
2035 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002036unsigned int drmAgpVendorId(int fd)
2037{
2038 drm_agp_info_t i;
2039
Daniel Vetterfd387942015-02-11 12:41:04 +01002040 memclear(i);
2041
Keith Packard8b9ab102008-06-13 16:03:22 -07002042 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04002043 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002044 return i.id_vendor;
2045}
2046
Jose Fonsecad2443b22003-05-27 00:37:33 +00002047
2048/**
2049 * Get hardware device ID.
2050 *
2051 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002052 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002053 * \return zero on success, or zero on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002054 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002055 * \internal
2056 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2057 * necessary information in a drm_agp_info structure.
2058 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002059unsigned int drmAgpDeviceId(int fd)
2060{
2061 drm_agp_info_t i;
2062
Daniel Vetterfd387942015-02-11 12:41:04 +01002063 memclear(i);
2064
Keith Packard8b9ab102008-06-13 16:03:22 -07002065 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04002066 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002067 return i.id_device;
2068}
2069
Dave Airlie7ede2092005-11-29 09:50:47 +00002070int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002071{
2072 drm_scatter_gather_t sg;
2073
Daniel Vetterfd387942015-02-11 12:41:04 +01002074 memclear(sg);
2075
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002076 *handle = 0;
2077 sg.size = size;
Keith Packard8b9ab102008-06-13 16:03:22 -07002078 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
Jan Vesely50d3c852016-06-30 14:22:52 -04002079 return -errno;
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002080 *handle = sg.handle;
2081 return 0;
2082}
2083
Dave Airlie7ede2092005-11-29 09:50:47 +00002084int drmScatterGatherFree(int fd, drm_handle_t handle)
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002085{
2086 drm_scatter_gather_t sg;
2087
Daniel Vetterfd387942015-02-11 12:41:04 +01002088 memclear(sg);
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002089 sg.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07002090 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
Jan Vesely50d3c852016-06-30 14:22:52 -04002091 return -errno;
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002092 return 0;
2093}
2094
Jose Fonsecad2443b22003-05-27 00:37:33 +00002095/**
2096 * Wait for VBLANK.
2097 *
2098 * \param fd file descriptor.
2099 * \param vbl pointer to a drmVBlank structure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002100 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002101 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002102 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002103 * \internal
2104 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2105 */
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002106int drmWaitVBlank(int fd, drmVBlankPtr vbl)
2107{
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002108 struct timespec timeout, cur;
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002109 int ret;
2110
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002111 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2112 if (ret < 0) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002113 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
2114 goto out;
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002115 }
2116 timeout.tv_sec++;
2117
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002118 do {
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002119 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
Michel Daenzerc7d471b2003-02-02 03:06:47 +00002120 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
Jesse Barnesca370772009-01-07 10:48:26 -08002121 if (ret && errno == EINTR) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002122 clock_gettime(CLOCK_MONOTONIC, &cur);
2123 /* Timeout after 1s */
2124 if (cur.tv_sec > timeout.tv_sec + 1 ||
2125 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2126 timeout.tv_nsec)) {
2127 errno = EBUSY;
2128 ret = -1;
2129 break;
2130 }
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002131 }
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002132 } while (ret && errno == EINTR);
2133
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002134out:
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002135 return ret;
2136}
2137
Daryll Straussb3a57661999-12-05 01:19:48 +00002138int drmError(int err, const char *label)
2139{
2140 switch (err) {
Brianccd7b6e2007-05-29 14:54:00 -06002141 case DRM_ERR_NO_DEVICE:
Jan Vesely50d3c852016-06-30 14:22:52 -04002142 fprintf(stderr, "%s: no device\n", label);
2143 break;
Brianccd7b6e2007-05-29 14:54:00 -06002144 case DRM_ERR_NO_ACCESS:
Jan Vesely50d3c852016-06-30 14:22:52 -04002145 fprintf(stderr, "%s: no access\n", label);
2146 break;
Brianccd7b6e2007-05-29 14:54:00 -06002147 case DRM_ERR_NOT_ROOT:
Jan Vesely50d3c852016-06-30 14:22:52 -04002148 fprintf(stderr, "%s: not root\n", label);
2149 break;
Brianccd7b6e2007-05-29 14:54:00 -06002150 case DRM_ERR_INVALID:
Jan Vesely50d3c852016-06-30 14:22:52 -04002151 fprintf(stderr, "%s: invalid args\n", label);
2152 break;
Daryll Straussb3a57661999-12-05 01:19:48 +00002153 default:
Jan Vesely50d3c852016-06-30 14:22:52 -04002154 if (err < 0)
2155 err = -err;
2156 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2157 break;
Daryll Straussb3a57661999-12-05 01:19:48 +00002158 }
2159
2160 return 1;
2161}
2162
Jose Fonsecad2443b22003-05-27 00:37:33 +00002163/**
2164 * Install IRQ handler.
2165 *
2166 * \param fd file descriptor.
2167 * \param irq IRQ number.
Jan Vesely50d3c852016-06-30 14:22:52 -04002168 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002169 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002170 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002171 * \internal
2172 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2173 * argument in a drm_control structure.
2174 */
Daryll Straussb3a57661999-12-05 01:19:48 +00002175int drmCtlInstHandler(int fd, int irq)
2176{
2177 drm_control_t ctl;
2178
Daniel Vetterfd387942015-02-11 12:41:04 +01002179 memclear(ctl);
Daryll Straussb3a57661999-12-05 01:19:48 +00002180 ctl.func = DRM_INST_HANDLER;
Daryll Straussb3a57661999-12-05 01:19:48 +00002181 ctl.irq = irq;
Keith Packard8b9ab102008-06-13 16:03:22 -07002182 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
Jan Vesely50d3c852016-06-30 14:22:52 -04002183 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002184 return 0;
2185}
2186
Jose Fonsecad2443b22003-05-27 00:37:33 +00002187
2188/**
2189 * Uninstall IRQ handler.
2190 *
2191 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002192 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002193 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002194 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002195 * \internal
2196 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2197 * argument in a drm_control structure.
2198 */
Daryll Straussb3a57661999-12-05 01:19:48 +00002199int drmCtlUninstHandler(int fd)
2200{
2201 drm_control_t ctl;
2202
Daniel Vetterfd387942015-02-11 12:41:04 +01002203 memclear(ctl);
Daryll Straussb3a57661999-12-05 01:19:48 +00002204 ctl.func = DRM_UNINST_HANDLER;
Daryll Straussb3a57661999-12-05 01:19:48 +00002205 ctl.irq = 0;
Keith Packard8b9ab102008-06-13 16:03:22 -07002206 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
Jan Vesely50d3c852016-06-30 14:22:52 -04002207 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002208 return 0;
2209}
2210
2211int drmFinish(int fd, int context, drmLockFlags flags)
2212{
2213 drm_lock_t lock;
2214
Daniel Vetterfd387942015-02-11 12:41:04 +01002215 memclear(lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00002216 lock.context = context;
Daryll Straussb3a57661999-12-05 01:19:48 +00002217 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2218 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2219 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2220 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2221 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2222 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
Keith Packard8b9ab102008-06-13 16:03:22 -07002223 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
Jan Vesely50d3c852016-06-30 14:22:52 -04002224 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002225 return 0;
2226}
2227
Jose Fonsecad2443b22003-05-27 00:37:33 +00002228/**
2229 * Get IRQ from bus ID.
2230 *
2231 * \param fd file descriptor.
2232 * \param busnum bus number.
2233 * \param devnum device number.
2234 * \param funcnum function number.
Jan Vesely50d3c852016-06-30 14:22:52 -04002235 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002236 * \return IRQ number on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002237 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002238 * \internal
2239 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2240 * arguments in a drm_irq_busid structure.
2241 */
Daryll Straussb3a57661999-12-05 01:19:48 +00002242int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2243{
2244 drm_irq_busid_t p;
2245
Daniel Vetterfd387942015-02-11 12:41:04 +01002246 memclear(p);
Daryll Straussb3a57661999-12-05 01:19:48 +00002247 p.busnum = busnum;
2248 p.devnum = devnum;
2249 p.funcnum = funcnum;
Keith Packard8b9ab102008-06-13 16:03:22 -07002250 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
Jan Vesely50d3c852016-06-30 14:22:52 -04002251 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002252 return p.irq;
2253}
2254
Jon Smirl8696e712004-07-07 04:36:36 +00002255int drmAddContextTag(int fd, drm_context_t context, void *tag)
Daryll Straussb3a57661999-12-05 01:19:48 +00002256{
2257 drmHashEntry *entry = drmGetEntry(fd);
2258
2259 if (drmHashInsert(entry->tagTable, context, tag)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002260 drmHashDelete(entry->tagTable, context);
2261 drmHashInsert(entry->tagTable, context, tag);
Daryll Straussb3a57661999-12-05 01:19:48 +00002262 }
2263 return 0;
2264}
2265
Jon Smirl8696e712004-07-07 04:36:36 +00002266int drmDelContextTag(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00002267{
2268 drmHashEntry *entry = drmGetEntry(fd);
2269
2270 return drmHashDelete(entry->tagTable, context);
2271}
2272
Jon Smirl8696e712004-07-07 04:36:36 +00002273void *drmGetContextTag(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00002274{
2275 drmHashEntry *entry = drmGetEntry(fd);
2276 void *value;
Gareth Hughes36047532001-02-15 08:12:14 +00002277
Brianccd7b6e2007-05-29 14:54:00 -06002278 if (drmHashLookup(entry->tagTable, context, &value))
Jan Vesely50d3c852016-06-30 14:22:52 -04002279 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00002280
2281 return value;
2282}
2283
Adam Jackson22e41ef2006-02-20 23:09:00 +00002284int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2285 drm_handle_t handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +00002286{
2287 drm_ctx_priv_map_t map;
2288
Daniel Vetterfd387942015-02-11 12:41:04 +01002289 memclear(map);
Kevin E Martin74e19a42001-03-14 22:22:50 +00002290 map.ctx_id = ctx_id;
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -07002291 map.handle = (void *)(uintptr_t)handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002292
Keith Packard8b9ab102008-06-13 16:03:22 -07002293 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
Jan Vesely50d3c852016-06-30 14:22:52 -04002294 return -errno;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002295 return 0;
2296}
2297
Adam Jackson22e41ef2006-02-20 23:09:00 +00002298int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2299 drm_handle_t *handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +00002300{
2301 drm_ctx_priv_map_t map;
2302
Daniel Vetterfd387942015-02-11 12:41:04 +01002303 memclear(map);
Kevin E Martin74e19a42001-03-14 22:22:50 +00002304 map.ctx_id = ctx_id;
2305
Keith Packard8b9ab102008-06-13 16:03:22 -07002306 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
Jan Vesely50d3c852016-06-30 14:22:52 -04002307 return -errno;
Brianccd7b6e2007-05-29 14:54:00 -06002308 if (handle)
Jan Vesely50d3c852016-06-30 14:22:52 -04002309 *handle = (drm_handle_t)(uintptr_t)map.handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002310
2311 return 0;
2312}
2313
Jon Smirl8696e712004-07-07 04:36:36 +00002314int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
Jan Vesely50d3c852016-06-30 14:22:52 -04002315 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2316 int *mtrr)
Rik Faith88dbee52001-02-28 09:27:44 +00002317{
2318 drm_map_t map;
2319
Daniel Vetterfd387942015-02-11 12:41:04 +01002320 memclear(map);
Rik Faith88dbee52001-02-28 09:27:44 +00002321 map.offset = idx;
Keith Packard8b9ab102008-06-13 16:03:22 -07002322 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
Jan Vesely50d3c852016-06-30 14:22:52 -04002323 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002324 *offset = map.offset;
2325 *size = map.size;
2326 *type = map.type;
2327 *flags = map.flags;
2328 *handle = (unsigned long)map.handle;
2329 *mtrr = map.mtrr;
2330 return 0;
2331}
2332
2333int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
Jan Vesely50d3c852016-06-30 14:22:52 -04002334 unsigned long *magic, unsigned long *iocs)
Rik Faith88dbee52001-02-28 09:27:44 +00002335{
2336 drm_client_t client;
2337
Daniel Vetterfd387942015-02-11 12:41:04 +01002338 memclear(client);
Rik Faith88dbee52001-02-28 09:27:44 +00002339 client.idx = idx;
Keith Packard8b9ab102008-06-13 16:03:22 -07002340 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
Jan Vesely50d3c852016-06-30 14:22:52 -04002341 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002342 *auth = client.auth;
2343 *pid = client.pid;
2344 *uid = client.uid;
2345 *magic = client.magic;
2346 *iocs = client.iocs;
2347 return 0;
2348}
2349
2350int drmGetStats(int fd, drmStatsT *stats)
2351{
2352 drm_stats_t s;
Jan Veselyde8532d2014-11-30 12:53:18 -05002353 unsigned i;
Rik Faith88dbee52001-02-28 09:27:44 +00002354
Daniel Vetterfd387942015-02-11 12:41:04 +01002355 memclear(s);
Keith Packard8b9ab102008-06-13 16:03:22 -07002356 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
Jan Vesely50d3c852016-06-30 14:22:52 -04002357 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002358
2359 stats->count = 0;
2360 memset(stats, 0, sizeof(*stats));
2361 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
Jan Vesely50d3c852016-06-30 14:22:52 -04002362 return -1;
Rik Faith88dbee52001-02-28 09:27:44 +00002363
2364#define SET_VALUE \
2365 stats->data[i].long_format = "%-20.20s"; \
2366 stats->data[i].rate_format = "%8.8s"; \
2367 stats->data[i].isvalue = 1; \
2368 stats->data[i].verbose = 0
2369
2370#define SET_COUNT \
2371 stats->data[i].long_format = "%-20.20s"; \
2372 stats->data[i].rate_format = "%5.5s"; \
2373 stats->data[i].isvalue = 0; \
2374 stats->data[i].mult_names = "kgm"; \
2375 stats->data[i].mult = 1000; \
2376 stats->data[i].verbose = 0
2377
2378#define SET_BYTE \
2379 stats->data[i].long_format = "%-20.20s"; \
2380 stats->data[i].rate_format = "%5.5s"; \
2381 stats->data[i].isvalue = 0; \
2382 stats->data[i].mult_names = "KGM"; \
2383 stats->data[i].mult = 1024; \
2384 stats->data[i].verbose = 0
2385
2386
2387 stats->count = s.count;
2388 for (i = 0; i < s.count; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002389 stats->data[i].value = s.data[i].value;
2390 switch (s.data[i].type) {
2391 case _DRM_STAT_LOCK:
2392 stats->data[i].long_name = "Lock";
2393 stats->data[i].rate_name = "Lock";
2394 SET_VALUE;
2395 break;
2396 case _DRM_STAT_OPENS:
2397 stats->data[i].long_name = "Opens";
2398 stats->data[i].rate_name = "O";
2399 SET_COUNT;
2400 stats->data[i].verbose = 1;
2401 break;
2402 case _DRM_STAT_CLOSES:
2403 stats->data[i].long_name = "Closes";
2404 stats->data[i].rate_name = "Lock";
2405 SET_COUNT;
2406 stats->data[i].verbose = 1;
2407 break;
2408 case _DRM_STAT_IOCTLS:
2409 stats->data[i].long_name = "Ioctls";
2410 stats->data[i].rate_name = "Ioc/s";
2411 SET_COUNT;
2412 break;
2413 case _DRM_STAT_LOCKS:
2414 stats->data[i].long_name = "Locks";
2415 stats->data[i].rate_name = "Lck/s";
2416 SET_COUNT;
2417 break;
2418 case _DRM_STAT_UNLOCKS:
2419 stats->data[i].long_name = "Unlocks";
2420 stats->data[i].rate_name = "Unl/s";
2421 SET_COUNT;
2422 break;
2423 case _DRM_STAT_IRQ:
2424 stats->data[i].long_name = "IRQs";
2425 stats->data[i].rate_name = "IRQ/s";
2426 SET_COUNT;
2427 break;
2428 case _DRM_STAT_PRIMARY:
2429 stats->data[i].long_name = "Primary Bytes";
2430 stats->data[i].rate_name = "PB/s";
2431 SET_BYTE;
2432 break;
2433 case _DRM_STAT_SECONDARY:
2434 stats->data[i].long_name = "Secondary Bytes";
2435 stats->data[i].rate_name = "SB/s";
2436 SET_BYTE;
2437 break;
2438 case _DRM_STAT_DMA:
2439 stats->data[i].long_name = "DMA";
2440 stats->data[i].rate_name = "DMA/s";
2441 SET_COUNT;
2442 break;
2443 case _DRM_STAT_SPECIAL:
2444 stats->data[i].long_name = "Special DMA";
2445 stats->data[i].rate_name = "dma/s";
2446 SET_COUNT;
2447 break;
2448 case _DRM_STAT_MISSED:
2449 stats->data[i].long_name = "Miss";
2450 stats->data[i].rate_name = "Ms/s";
2451 SET_COUNT;
2452 break;
2453 case _DRM_STAT_VALUE:
2454 stats->data[i].long_name = "Value";
2455 stats->data[i].rate_name = "Value";
2456 SET_VALUE;
2457 break;
2458 case _DRM_STAT_BYTE:
2459 stats->data[i].long_name = "Bytes";
2460 stats->data[i].rate_name = "B/s";
2461 SET_BYTE;
2462 break;
2463 case _DRM_STAT_COUNT:
2464 default:
2465 stats->data[i].long_name = "Count";
2466 stats->data[i].rate_name = "Cnt/s";
2467 SET_COUNT;
2468 break;
2469 }
Rik Faith88dbee52001-02-28 09:27:44 +00002470 }
2471 return 0;
2472}
2473
Jose Fonsecad2443b22003-05-27 00:37:33 +00002474/**
Eric Anholt06cb1322003-10-23 02:23:31 +00002475 * Issue a set-version ioctl.
2476 *
2477 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002478 * \param drmCommandIndex command index
Eric Anholt06cb1322003-10-23 02:23:31 +00002479 * \param data source pointer of the data to be read and written.
2480 * \param size size of the data to be read and written.
Jan Vesely50d3c852016-06-30 14:22:52 -04002481 *
Eric Anholt06cb1322003-10-23 02:23:31 +00002482 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002483 *
Eric Anholt06cb1322003-10-23 02:23:31 +00002484 * \internal
Jan Vesely50d3c852016-06-30 14:22:52 -04002485 * It issues a read-write ioctl given by
Eric Anholt06cb1322003-10-23 02:23:31 +00002486 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2487 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002488int drmSetInterfaceVersion(int fd, drmSetVersion *version)
Eric Anholt06cb1322003-10-23 02:23:31 +00002489{
2490 int retcode = 0;
2491 drm_set_version_t sv;
2492
Daniel Vetterfd387942015-02-11 12:41:04 +01002493 memclear(sv);
Eric Anholt06cb1322003-10-23 02:23:31 +00002494 sv.drm_di_major = version->drm_di_major;
2495 sv.drm_di_minor = version->drm_di_minor;
2496 sv.drm_dd_major = version->drm_dd_major;
2497 sv.drm_dd_minor = version->drm_dd_minor;
2498
Keith Packard8b9ab102008-06-13 16:03:22 -07002499 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002500 retcode = -errno;
Eric Anholt06cb1322003-10-23 02:23:31 +00002501 }
2502
2503 version->drm_di_major = sv.drm_di_major;
2504 version->drm_di_minor = sv.drm_di_minor;
2505 version->drm_dd_major = sv.drm_dd_major;
2506 version->drm_dd_minor = sv.drm_dd_minor;
2507
2508 return retcode;
2509}
2510
2511/**
Jose Fonsecad2443b22003-05-27 00:37:33 +00002512 * Send a device-specific command.
2513 *
2514 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002515 * \param drmCommandIndex command index
2516 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002517 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002518 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002519 * \internal
Jan Vesely50d3c852016-06-30 14:22:52 -04002520 * It issues a ioctl given by
Jose Fonsecad2443b22003-05-27 00:37:33 +00002521 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2522 */
Jens Owen3903e5a2002-04-09 21:54:56 +00002523int drmCommandNone(int fd, unsigned long drmCommandIndex)
2524{
Jens Owen3903e5a2002-04-09 21:54:56 +00002525 unsigned long request;
2526
2527 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2528
Daniel Vetterfd387942015-02-11 12:41:04 +01002529 if (drmIoctl(fd, request, NULL)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002530 return -errno;
Jens Owen3903e5a2002-04-09 21:54:56 +00002531 }
2532 return 0;
2533}
2534
Jose Fonsecad2443b22003-05-27 00:37:33 +00002535
2536/**
2537 * Send a device-specific read command.
2538 *
2539 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002540 * \param drmCommandIndex command index
Jose Fonsecad2443b22003-05-27 00:37:33 +00002541 * \param data destination pointer of the data to be read.
2542 * \param size size of the data to be read.
Jan Vesely50d3c852016-06-30 14:22:52 -04002543 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002544 * \return zero on success, or a negative value on failure.
2545 *
2546 * \internal
Jan Vesely50d3c852016-06-30 14:22:52 -04002547 * It issues a read ioctl given by
Jose Fonsecad2443b22003-05-27 00:37:33 +00002548 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2549 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002550int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2551 unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002552{
2553 unsigned long request;
2554
Jan Vesely50d3c852016-06-30 14:22:52 -04002555 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2556 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002557
Keith Packard8b9ab102008-06-13 16:03:22 -07002558 if (drmIoctl(fd, request, data)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002559 return -errno;
Jens Owen3903e5a2002-04-09 21:54:56 +00002560 }
2561 return 0;
2562}
2563
Jose Fonsecad2443b22003-05-27 00:37:33 +00002564
2565/**
2566 * Send a device-specific write command.
2567 *
2568 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002569 * \param drmCommandIndex command index
Jose Fonsecad2443b22003-05-27 00:37:33 +00002570 * \param data source pointer of the data to be written.
2571 * \param size size of the data to be written.
Jan Vesely50d3c852016-06-30 14:22:52 -04002572 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002573 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002574 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002575 * \internal
Jan Vesely50d3c852016-06-30 14:22:52 -04002576 * It issues a write ioctl given by
Jose Fonsecad2443b22003-05-27 00:37:33 +00002577 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2578 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002579int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2580 unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002581{
2582 unsigned long request;
2583
Jan Vesely50d3c852016-06-30 14:22:52 -04002584 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2585 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002586
Keith Packard8b9ab102008-06-13 16:03:22 -07002587 if (drmIoctl(fd, request, data)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002588 return -errno;
Jens Owen3903e5a2002-04-09 21:54:56 +00002589 }
2590 return 0;
2591}
2592
Jose Fonsecad2443b22003-05-27 00:37:33 +00002593
2594/**
2595 * Send a device-specific read-write command.
2596 *
2597 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002598 * \param drmCommandIndex command index
Jose Fonsecad2443b22003-05-27 00:37:33 +00002599 * \param data source pointer of the data to be read and written.
2600 * \param size size of the data to be read and written.
Jan Vesely50d3c852016-06-30 14:22:52 -04002601 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002602 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002603 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002604 * \internal
Jan Vesely50d3c852016-06-30 14:22:52 -04002605 * It issues a read-write ioctl given by
Jose Fonsecad2443b22003-05-27 00:37:33 +00002606 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2607 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002608int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2609 unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002610{
2611 unsigned long request;
2612
Jan Vesely50d3c852016-06-30 14:22:52 -04002613 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2614 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002615
Keith Packard8b9ab102008-06-13 16:03:22 -07002616 if (drmIoctl(fd, request, data))
Jan Vesely50d3c852016-06-30 14:22:52 -04002617 return -errno;
Jens Owen3903e5a2002-04-09 21:54:56 +00002618 return 0;
2619}
Thomas Hellstrom166da932006-08-21 21:02:08 +02002620
Dave Airlied51e1bb2006-11-09 08:55:58 +11002621#define DRM_MAX_FDS 16
2622static struct {
Brianccd7b6e2007-05-29 14:54:00 -06002623 char *BusID;
2624 int fd;
2625 int refcount;
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002626 int type;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002627} connection[DRM_MAX_FDS];
2628
2629static int nr_fds = 0;
2630
Jan Vesely50d3c852016-06-30 14:22:52 -04002631int drmOpenOnce(void *unused,
2632 const char *BusID,
2633 int *newlyopened)
Dave Airlied51e1bb2006-11-09 08:55:58 +11002634{
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002635 return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
2636}
2637
2638int drmOpenOnceWithType(const char *BusID, int *newlyopened, int type)
2639{
Brianccd7b6e2007-05-29 14:54:00 -06002640 int i;
2641 int fd;
Jan Vesely50d3c852016-06-30 14:22:52 -04002642
Brianccd7b6e2007-05-29 14:54:00 -06002643 for (i = 0; i < nr_fds; i++)
Jan Vesely50d3c852016-06-30 14:22:52 -04002644 if ((strcmp(BusID, connection[i].BusID) == 0) &&
2645 (connection[i].type == type)) {
2646 connection[i].refcount++;
2647 *newlyopened = 0;
2648 return connection[i].fd;
2649 }
Dave Airlied51e1bb2006-11-09 08:55:58 +11002650
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002651 fd = drmOpenWithType(NULL, BusID, type);
Emil Velikovc1cd3d92015-07-14 15:05:18 +01002652 if (fd < 0 || nr_fds == DRM_MAX_FDS)
Jan Vesely50d3c852016-06-30 14:22:52 -04002653 return fd;
2654
Brianccd7b6e2007-05-29 14:54:00 -06002655 connection[nr_fds].BusID = strdup(BusID);
2656 connection[nr_fds].fd = fd;
2657 connection[nr_fds].refcount = 1;
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002658 connection[nr_fds].type = type;
Brianccd7b6e2007-05-29 14:54:00 -06002659 *newlyopened = 1;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002660
Brianccd7b6e2007-05-29 14:54:00 -06002661 if (0)
Jan Vesely50d3c852016-06-30 14:22:52 -04002662 fprintf(stderr, "saved connection %d for %s %d\n",
2663 nr_fds, connection[nr_fds].BusID,
2664 strcmp(BusID, connection[nr_fds].BusID));
Dave Airlied51e1bb2006-11-09 08:55:58 +11002665
Brianccd7b6e2007-05-29 14:54:00 -06002666 nr_fds++;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002667
Brianccd7b6e2007-05-29 14:54:00 -06002668 return fd;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002669}
2670
2671void drmCloseOnce(int fd)
2672{
Brianccd7b6e2007-05-29 14:54:00 -06002673 int i;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002674
Brianccd7b6e2007-05-29 14:54:00 -06002675 for (i = 0; i < nr_fds; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002676 if (fd == connection[i].fd) {
2677 if (--connection[i].refcount == 0) {
2678 drmClose(connection[i].fd);
2679 free(connection[i].BusID);
Dave Airlied51e1bb2006-11-09 08:55:58 +11002680
Jan Vesely50d3c852016-06-30 14:22:52 -04002681 if (i < --nr_fds)
2682 connection[i] = connection[nr_fds];
2683
2684 return;
2685 }
2686 }
Brianccd7b6e2007-05-29 14:54:00 -06002687 }
Dave Airlied51e1bb2006-11-09 08:55:58 +11002688}
Jesse Barnes731cd552008-12-17 10:09:49 -08002689
2690int drmSetMaster(int fd)
2691{
Jan Vesely50d3c852016-06-30 14:22:52 -04002692 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
Jesse Barnes731cd552008-12-17 10:09:49 -08002693}
2694
2695int drmDropMaster(int fd)
2696{
Jan Vesely50d3c852016-06-30 14:22:52 -04002697 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
Jesse Barnes731cd552008-12-17 10:09:49 -08002698}
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002699
2700char *drmGetDeviceNameFromFd(int fd)
2701{
Jan Vesely50d3c852016-06-30 14:22:52 -04002702 char name[128];
2703 struct stat sbuf;
2704 dev_t d;
2705 int i;
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002706
Jan Vesely50d3c852016-06-30 14:22:52 -04002707 /* The whole drmOpen thing is a fiasco and we need to find a way
2708 * back to just using open(2). For now, however, lets just make
2709 * things worse with even more ad hoc directory walking code to
2710 * discover the device file name. */
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002711
Jan Vesely50d3c852016-06-30 14:22:52 -04002712 fstat(fd, &sbuf);
2713 d = sbuf.st_rdev;
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002714
Jan Vesely50d3c852016-06-30 14:22:52 -04002715 for (i = 0; i < DRM_MAX_MINOR; i++) {
2716 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2717 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2718 break;
2719 }
2720 if (i == DRM_MAX_MINOR)
2721 return NULL;
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002722
Jan Vesely50d3c852016-06-30 14:22:52 -04002723 return strdup(name);
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002724}
Dave Airliecc0a1452012-07-14 09:52:17 +00002725
Frank Binns1f735782015-02-13 10:51:15 +00002726int drmGetNodeTypeFromFd(int fd)
2727{
Jan Vesely50d3c852016-06-30 14:22:52 -04002728 struct stat sbuf;
2729 int maj, min, type;
Frank Binns1f735782015-02-13 10:51:15 +00002730
Jan Vesely50d3c852016-06-30 14:22:52 -04002731 if (fstat(fd, &sbuf))
2732 return -1;
Frank Binns1f735782015-02-13 10:51:15 +00002733
Jan Vesely50d3c852016-06-30 14:22:52 -04002734 maj = major(sbuf.st_rdev);
2735 min = minor(sbuf.st_rdev);
Frank Binns1f735782015-02-13 10:51:15 +00002736
Jan Vesely50d3c852016-06-30 14:22:52 -04002737 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) {
2738 errno = EINVAL;
2739 return -1;
2740 }
Frank Binns1f735782015-02-13 10:51:15 +00002741
Jan Vesely50d3c852016-06-30 14:22:52 -04002742 type = drmGetMinorType(min);
2743 if (type == -1)
2744 errno = ENODEV;
2745 return type;
Frank Binns1f735782015-02-13 10:51:15 +00002746}
2747
Dave Airliecc0a1452012-07-14 09:52:17 +00002748int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd)
2749{
Jan Vesely50d3c852016-06-30 14:22:52 -04002750 struct drm_prime_handle args;
2751 int ret;
Dave Airliecc0a1452012-07-14 09:52:17 +00002752
Jan Vesely50d3c852016-06-30 14:22:52 -04002753 memclear(args);
2754 args.fd = -1;
2755 args.handle = handle;
2756 args.flags = flags;
2757 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2758 if (ret)
2759 return ret;
Dave Airliecc0a1452012-07-14 09:52:17 +00002760
Jan Vesely50d3c852016-06-30 14:22:52 -04002761 *prime_fd = args.fd;
2762 return 0;
Dave Airliecc0a1452012-07-14 09:52:17 +00002763}
2764
2765int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2766{
Jan Vesely50d3c852016-06-30 14:22:52 -04002767 struct drm_prime_handle args;
2768 int ret;
Dave Airliecc0a1452012-07-14 09:52:17 +00002769
Jan Vesely50d3c852016-06-30 14:22:52 -04002770 memclear(args);
2771 args.fd = prime_fd;
2772 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2773 if (ret)
2774 return ret;
Dave Airliecc0a1452012-07-14 09:52:17 +00002775
Jan Vesely50d3c852016-06-30 14:22:52 -04002776 *handle = args.handle;
2777 return 0;
Dave Airliecc0a1452012-07-14 09:52:17 +00002778}
2779
Emil Velikov0ca03a42015-03-07 00:58:39 +00002780static char *drmGetMinorNameForFD(int fd, int type)
2781{
2782#ifdef __linux__
Jan Vesely50d3c852016-06-30 14:22:52 -04002783 DIR *sysdir;
2784 struct dirent *pent, *ent;
2785 struct stat sbuf;
2786 const char *name = drmGetMinorName(type);
2787 int len;
2788 char dev_name[64], buf[64];
2789 long name_max;
2790 int maj, min;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002791
Jan Vesely50d3c852016-06-30 14:22:52 -04002792 if (!name)
2793 return NULL;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002794
Jan Vesely50d3c852016-06-30 14:22:52 -04002795 len = strlen(name);
Emil Velikov0ca03a42015-03-07 00:58:39 +00002796
Jan Vesely50d3c852016-06-30 14:22:52 -04002797 if (fstat(fd, &sbuf))
2798 return NULL;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002799
Jan Vesely50d3c852016-06-30 14:22:52 -04002800 maj = major(sbuf.st_rdev);
2801 min = minor(sbuf.st_rdev);
Emil Velikov0ca03a42015-03-07 00:58:39 +00002802
Jan Vesely50d3c852016-06-30 14:22:52 -04002803 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
2804 return NULL;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002805
Jan Vesely50d3c852016-06-30 14:22:52 -04002806 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
Emil Velikov0ca03a42015-03-07 00:58:39 +00002807
Jan Vesely50d3c852016-06-30 14:22:52 -04002808 sysdir = opendir(buf);
2809 if (!sysdir)
2810 return NULL;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002811
Jan Vesely50d3c852016-06-30 14:22:52 -04002812 name_max = fpathconf(dirfd(sysdir), _PC_NAME_MAX);
2813 if (name_max == -1)
2814 goto out_close_dir;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002815
Jan Vesely50d3c852016-06-30 14:22:52 -04002816 pent = malloc(offsetof(struct dirent, d_name) + name_max + 1);
2817 if (pent == NULL)
2818 goto out_close_dir;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002819
Jan Vesely50d3c852016-06-30 14:22:52 -04002820 while (readdir_r(sysdir, pent, &ent) == 0 && ent != NULL) {
2821 if (strncmp(ent->d_name, name, len) == 0) {
2822 snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
2823 ent->d_name);
Mathias Tillman5c42b5e2015-08-24 11:56:13 +08002824
Jan Vesely50d3c852016-06-30 14:22:52 -04002825 free(pent);
2826 closedir(sysdir);
Emil Velikov0ca03a42015-03-07 00:58:39 +00002827
Jan Vesely50d3c852016-06-30 14:22:52 -04002828 return strdup(dev_name);
2829 }
2830 }
Emil Velikov0ca03a42015-03-07 00:58:39 +00002831
Jan Vesely50d3c852016-06-30 14:22:52 -04002832 free(pent);
Emil Velikov0ca03a42015-03-07 00:58:39 +00002833
2834out_close_dir:
Jan Vesely50d3c852016-06-30 14:22:52 -04002835 closedir(sysdir);
Emil Velikov8415a002015-09-07 18:29:05 +01002836#else
Jonathan Grayf1890112016-12-01 15:18:39 +11002837 struct stat sbuf;
2838 char buf[PATH_MAX + 1];
2839 const char *dev_name;
2840 unsigned int maj, min;
Jonathan Grayd5cf3f92016-12-17 16:09:51 +11002841 int n, base;
Jonathan Grayf1890112016-12-01 15:18:39 +11002842
2843 if (fstat(fd, &sbuf))
2844 return NULL;
2845
2846 maj = major(sbuf.st_rdev);
2847 min = minor(sbuf.st_rdev);
2848
2849 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
2850 return NULL;
2851
2852 switch (type) {
2853 case DRM_NODE_PRIMARY:
2854 dev_name = DRM_DEV_NAME;
2855 break;
2856 case DRM_NODE_CONTROL:
2857 dev_name = DRM_CONTROL_DEV_NAME;
2858 break;
2859 case DRM_NODE_RENDER:
2860 dev_name = DRM_RENDER_DEV_NAME;
2861 break;
2862 default:
2863 return NULL;
2864 };
2865
Jonathan Grayd5cf3f92016-12-17 16:09:51 +11002866 base = drmGetMinorBase(type);
2867 if (base < 0)
2868 return NULL;
2869
2870 n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, min - base);
Jonathan Grayf1890112016-12-01 15:18:39 +11002871 if (n == -1 || n >= sizeof(buf))
2872 return NULL;
2873
2874 return strdup(buf);
Emil Velikov0ca03a42015-03-07 00:58:39 +00002875#endif
Jan Vesely50d3c852016-06-30 14:22:52 -04002876 return NULL;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002877}
2878
2879char *drmGetPrimaryDeviceNameFromFd(int fd)
2880{
Jan Vesely50d3c852016-06-30 14:22:52 -04002881 return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
Emil Velikov0ca03a42015-03-07 00:58:39 +00002882}
2883
2884char *drmGetRenderDeviceNameFromFd(int fd)
2885{
Jan Vesely50d3c852016-06-30 14:22:52 -04002886 return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
Emil Velikov0ca03a42015-03-07 00:58:39 +00002887}
Emil Velikovb556ea12015-08-17 11:09:06 +08002888
Emil Velikova250fce2015-09-07 12:54:27 +01002889static int drmParseSubsystemType(int maj, int min)
Emil Velikovb556ea12015-08-17 11:09:06 +08002890{
Emil Velikov291b2bb2015-09-07 18:26:34 +01002891#ifdef __linux__
Emil Velikova250fce2015-09-07 12:54:27 +01002892 char path[PATH_MAX + 1];
Emil Velikovb556ea12015-08-17 11:09:06 +08002893 char link[PATH_MAX + 1] = "";
2894 char *name;
2895
Emil Velikova250fce2015-09-07 12:54:27 +01002896 snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/subsystem",
2897 maj, min);
2898
2899 if (readlink(path, link, PATH_MAX) < 0)
2900 return -errno;
Emil Velikovb556ea12015-08-17 11:09:06 +08002901
2902 name = strrchr(link, '/');
2903 if (!name)
2904 return -EINVAL;
2905
Emil Velikova250fce2015-09-07 12:54:27 +01002906 if (strncmp(name, "/pci", 4) == 0)
Emil Velikovb556ea12015-08-17 11:09:06 +08002907 return DRM_BUS_PCI;
2908
2909 return -EINVAL;
Jonathan Grayd05b9f22016-12-01 15:18:40 +11002910#elif defined(__OpenBSD__)
2911 return DRM_BUS_PCI;
Emil Velikov291b2bb2015-09-07 18:26:34 +01002912#else
2913#warning "Missing implementation of drmParseSubsystemType"
2914 return -EINVAL;
2915#endif
Emil Velikovb556ea12015-08-17 11:09:06 +08002916}
2917
Emil Velikov536e0de2015-09-07 12:37:57 +01002918static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
Emil Velikovb556ea12015-08-17 11:09:06 +08002919{
Emil Velikov291b2bb2015-09-07 18:26:34 +01002920#ifdef __linux__
Emil Velikov536e0de2015-09-07 12:37:57 +01002921 char path[PATH_MAX + 1];
Emil Velikovf53d3542016-11-11 19:04:11 +00002922 char data[512 + 1];
Emil Velikov536e0de2015-09-07 12:37:57 +01002923 char *str;
Emil Velikovb556ea12015-08-17 11:09:06 +08002924 int domain, bus, dev, func;
Emil Velikov536e0de2015-09-07 12:37:57 +01002925 int fd, ret;
Emil Velikovb556ea12015-08-17 11:09:06 +08002926
Emil Velikov536e0de2015-09-07 12:37:57 +01002927 snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/uevent", maj, min);
2928 fd = open(path, O_RDONLY);
2929 if (fd < 0)
2930 return -errno;
2931
2932 ret = read(fd, data, sizeof(data));
Emil Velikovf53d3542016-11-11 19:04:11 +00002933 data[sizeof(data)-1] = '\0';
Emil Velikov536e0de2015-09-07 12:37:57 +01002934 close(fd);
2935 if (ret < 0)
2936 return -errno;
2937
2938#define TAG "PCI_SLOT_NAME="
2939 str = strstr(data, TAG);
Emil Velikovb556ea12015-08-17 11:09:06 +08002940 if (str == NULL)
2941 return -EINVAL;
2942
Emil Velikov536e0de2015-09-07 12:37:57 +01002943 if (sscanf(str, TAG "%04x:%02x:%02x.%1u",
Emil Velikovb556ea12015-08-17 11:09:06 +08002944 &domain, &bus, &dev, &func) != 4)
2945 return -EINVAL;
Emil Velikov536e0de2015-09-07 12:37:57 +01002946#undef TAG
Emil Velikovb556ea12015-08-17 11:09:06 +08002947
2948 info->domain = domain;
2949 info->bus = bus;
2950 info->dev = dev;
2951 info->func = func;
2952
2953 return 0;
Jonathan Grayfd190562016-12-01 15:18:42 +11002954#elif defined(__OpenBSD__)
2955 struct drm_pciinfo pinfo;
2956 int fd, type;
2957
2958 type = drmGetMinorType(min);
2959 if (type == -1)
2960 return -ENODEV;
2961
2962 fd = drmOpenMinor(min, 0, type);
2963 if (fd < 0)
2964 return -errno;
2965
2966 if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
2967 close(fd);
2968 return -errno;
2969 }
2970 close(fd);
2971
2972 info->domain = pinfo.domain;
2973 info->bus = pinfo.bus;
2974 info->dev = pinfo.dev;
2975 info->func = pinfo.func;
2976
2977 return 0;
Emil Velikov291b2bb2015-09-07 18:26:34 +01002978#else
2979#warning "Missing implementation of drmParsePciBusInfo"
2980 return -EINVAL;
2981#endif
Emil Velikovb556ea12015-08-17 11:09:06 +08002982}
2983
Emil Velikovbc2aca92015-09-07 13:51:54 +01002984static int drmCompareBusInfo(drmDevicePtr a, drmDevicePtr b)
Emil Velikovb556ea12015-08-17 11:09:06 +08002985{
Emil Velikovbc2aca92015-09-07 13:51:54 +01002986 if (a == NULL || b == NULL)
2987 return -1;
2988
Emil Velikovb556ea12015-08-17 11:09:06 +08002989 if (a->bustype != b->bustype)
Emil Velikovbc2aca92015-09-07 13:51:54 +01002990 return -1;
Emil Velikovb556ea12015-08-17 11:09:06 +08002991
2992 switch (a->bustype) {
2993 case DRM_BUS_PCI:
Emil Velikovbc2aca92015-09-07 13:51:54 +01002994 return memcmp(a->businfo.pci, b->businfo.pci, sizeof(drmPciBusInfo));
Emil Velikovb556ea12015-08-17 11:09:06 +08002995 default:
2996 break;
2997 }
2998
Emil Velikovbc2aca92015-09-07 13:51:54 +01002999 return -1;
Emil Velikovb556ea12015-08-17 11:09:06 +08003000}
3001
3002static int drmGetNodeType(const char *name)
3003{
3004 if (strncmp(name, DRM_PRIMARY_MINOR_NAME,
3005 sizeof(DRM_PRIMARY_MINOR_NAME) - 1) == 0)
3006 return DRM_NODE_PRIMARY;
3007
3008 if (strncmp(name, DRM_CONTROL_MINOR_NAME,
3009 sizeof(DRM_CONTROL_MINOR_NAME ) - 1) == 0)
3010 return DRM_NODE_CONTROL;
3011
3012 if (strncmp(name, DRM_RENDER_MINOR_NAME,
3013 sizeof(DRM_RENDER_MINOR_NAME) - 1) == 0)
3014 return DRM_NODE_RENDER;
3015
3016 return -EINVAL;
3017}
3018
Emil Velikov5f68d312015-09-07 13:54:32 +01003019static int drmGetMaxNodeName(void)
3020{
3021 return sizeof(DRM_DIR_NAME) +
3022 MAX3(sizeof(DRM_PRIMARY_MINOR_NAME),
3023 sizeof(DRM_CONTROL_MINOR_NAME),
3024 sizeof(DRM_RENDER_MINOR_NAME)) +
Eric Engestromce975072016-04-03 19:48:12 +01003025 3 /* length of the node number */;
Emil Velikov5f68d312015-09-07 13:54:32 +01003026}
3027
Emil Velikov291b2bb2015-09-07 18:26:34 +01003028#ifdef __linux__
Emil Velikovaae3f312016-11-01 18:04:06 +00003029static int parse_separate_sysfs_files(int maj, int min,
Emil Velikov11687bf2016-11-30 17:24:21 +00003030 drmPciDeviceInfoPtr device,
3031 bool ignore_revision)
Emil Velikovaae3f312016-11-01 18:04:06 +00003032{
3033#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
3034 static const char *attrs[] = {
3035 "revision", /* Older kernels are missing the file, so check for it first */
3036 "vendor",
3037 "device",
3038 "subsystem_vendor",
3039 "subsystem_device",
3040 };
3041 char path[PATH_MAX + 1];
3042 unsigned int data[ARRAY_SIZE(attrs)];
3043 FILE *fp;
3044 int ret;
3045
Emil Velikov11687bf2016-11-30 17:24:21 +00003046 for (unsigned i = ignore_revision ? 1 : 0; i < ARRAY_SIZE(attrs); i++) {
Emil Velikovaae3f312016-11-01 18:04:06 +00003047 snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/%s", maj, min,
3048 attrs[i]);
3049 fp = fopen(path, "r");
3050 if (!fp)
3051 return -errno;
3052
3053 ret = fscanf(fp, "%x", &data[i]);
3054 fclose(fp);
3055 if (ret != 1)
3056 return -errno;
3057
3058 }
3059
Emil Velikov11687bf2016-11-30 17:24:21 +00003060 device->revision_id = ignore_revision ? 0xff : data[0] & 0xff;
Emil Velikovaae3f312016-11-01 18:04:06 +00003061 device->vendor_id = data[1] & 0xffff;
3062 device->device_id = data[2] & 0xffff;
3063 device->subvendor_id = data[3] & 0xffff;
3064 device->subdevice_id = data[4] & 0xffff;
3065
3066 return 0;
3067}
3068
3069static int parse_config_sysfs_file(int maj, int min,
3070 drmPciDeviceInfoPtr device)
3071{
Emil Velikovef5192e2015-09-07 12:47:47 +01003072 char path[PATH_MAX + 1];
3073 unsigned char config[64];
3074 int fd, ret;
3075
Emil Velikovb40a65d2016-11-30 16:41:27 +00003076 snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/config", maj, min);
Emil Velikovef5192e2015-09-07 12:47:47 +01003077 fd = open(path, O_RDONLY);
3078 if (fd < 0)
3079 return -errno;
3080
3081 ret = read(fd, config, sizeof(config));
3082 close(fd);
3083 if (ret < 0)
3084 return -errno;
Emil Velikovb556ea12015-08-17 11:09:06 +08003085
3086 device->vendor_id = config[0] | (config[1] << 8);
3087 device->device_id = config[2] | (config[3] << 8);
3088 device->revision_id = config[8];
3089 device->subvendor_id = config[44] | (config[45] << 8);
3090 device->subdevice_id = config[46] | (config[47] << 8);
3091
3092 return 0;
Emil Velikovaae3f312016-11-01 18:04:06 +00003093}
3094#endif
3095
3096static int drmParsePciDeviceInfo(int maj, int min,
3097 drmPciDeviceInfoPtr device,
3098 uint32_t flags)
3099{
3100#ifdef __linux__
Emil Velikov11687bf2016-11-30 17:24:21 +00003101 if (!(flags & DRM_DEVICE_GET_PCI_REVISION))
3102 return parse_separate_sysfs_files(maj, min, device, true);
3103
3104 if (parse_separate_sysfs_files(maj, min, device, false))
Emil Velikovaae3f312016-11-01 18:04:06 +00003105 return parse_config_sysfs_file(maj, min, device);
3106
3107 return 0;
Jonathan Grayc0ef1d02016-12-01 15:18:41 +11003108#elif defined(__OpenBSD__)
3109 struct drm_pciinfo pinfo;
3110 int fd, type;
3111
3112 type = drmGetMinorType(min);
3113 if (type == -1)
3114 return -ENODEV;
3115
3116 fd = drmOpenMinor(min, 0, type);
3117 if (fd < 0)
3118 return -errno;
3119
3120 if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
3121 close(fd);
3122 return -errno;
3123 }
3124 close(fd);
3125
3126 device->vendor_id = pinfo.vendor_id;
3127 device->device_id = pinfo.device_id;
3128 device->revision_id = pinfo.revision_id;
3129 device->subvendor_id = pinfo.subvendor_id;
3130 device->subdevice_id = pinfo.subdevice_id;
3131
3132 return 0;
Emil Velikov291b2bb2015-09-07 18:26:34 +01003133#else
3134#warning "Missing implementation of drmParsePciDeviceInfo"
3135 return -EINVAL;
3136#endif
Emil Velikovb556ea12015-08-17 11:09:06 +08003137}
3138
Emil Velikovccedf662015-09-09 16:02:18 +01003139void drmFreeDevice(drmDevicePtr *device)
Emil Velikovb556ea12015-08-17 11:09:06 +08003140{
Emil Velikovb556ea12015-08-17 11:09:06 +08003141 if (device == NULL)
3142 return;
3143
Emil Velikov5f68d312015-09-07 13:54:32 +01003144 free(*device);
3145 *device = NULL;
Emil Velikovb556ea12015-08-17 11:09:06 +08003146}
3147
3148void drmFreeDevices(drmDevicePtr devices[], int count)
3149{
3150 int i;
3151
3152 if (devices == NULL)
3153 return;
3154
Qiang Yu6c056ee2016-07-14 17:10:56 +08003155 for (i = 0; i < count; i++)
3156 if (devices[i])
3157 drmFreeDevice(&devices[i]);
Emil Velikovb556ea12015-08-17 11:09:06 +08003158}
3159
Emil Velikovb40a65d2016-11-30 16:41:27 +00003160static int drmProcessPciDevice(drmDevicePtr *device,
Emil Velikovfae59d72015-09-09 17:54:34 +01003161 const char *node, int node_type,
Emil Velikov138d2312016-11-30 17:13:51 +00003162 int maj, int min, bool fetch_deviceinfo,
3163 uint32_t flags)
Emil Velikovfae59d72015-09-09 17:54:34 +01003164{
Nicolai Hähnle9bdec972016-05-13 01:11:13 -05003165 const int max_node_str = ALIGN(drmGetMaxNodeName(), sizeof(void *));
Emil Velikovfae59d72015-09-09 17:54:34 +01003166 int ret, i;
Michel Dänzer30455232015-10-14 12:48:52 +09003167 char *addr;
Emil Velikovfae59d72015-09-09 17:54:34 +01003168
Michel Dänzer30455232015-10-14 12:48:52 +09003169 *device = calloc(1, sizeof(drmDevice) +
Jan Vesely50d3c852016-06-30 14:22:52 -04003170 (DRM_NODE_MAX * (sizeof(void *) + max_node_str)) +
3171 sizeof(drmPciBusInfo) +
3172 sizeof(drmPciDeviceInfo));
Emil Velikovfae59d72015-09-09 17:54:34 +01003173 if (!*device)
3174 return -ENOMEM;
3175
Michel Dänzer30455232015-10-14 12:48:52 +09003176 addr = (char*)*device;
Jan Vesely50d3c852016-06-30 14:22:52 -04003177
Emil Velikovfae59d72015-09-09 17:54:34 +01003178 (*device)->bustype = DRM_BUS_PCI;
3179 (*device)->available_nodes = 1 << node_type;
3180
3181 addr += sizeof(drmDevice);
Michel Dänzer30455232015-10-14 12:48:52 +09003182 (*device)->nodes = (char**)addr;
Emil Velikovfae59d72015-09-09 17:54:34 +01003183
3184 addr += DRM_NODE_MAX * sizeof(void *);
3185 for (i = 0; i < DRM_NODE_MAX; i++) {
3186 (*device)->nodes[i] = addr;
3187 addr += max_node_str;
3188 }
3189 memcpy((*device)->nodes[node_type], node, max_node_str);
3190
Michel Dänzer30455232015-10-14 12:48:52 +09003191 (*device)->businfo.pci = (drmPciBusInfoPtr)addr;
Emil Velikovfae59d72015-09-09 17:54:34 +01003192
3193 ret = drmParsePciBusInfo(maj, min, (*device)->businfo.pci);
3194 if (ret)
3195 goto free_device;
3196
3197 // Fetch the device info if the user has requested it
3198 if (fetch_deviceinfo) {
3199 addr += sizeof(drmPciBusInfo);
Michel Dänzer30455232015-10-14 12:48:52 +09003200 (*device)->deviceinfo.pci = (drmPciDeviceInfoPtr)addr;
Emil Velikovfae59d72015-09-09 17:54:34 +01003201
Emil Velikov138d2312016-11-30 17:13:51 +00003202 ret = drmParsePciDeviceInfo(maj, min, (*device)->deviceinfo.pci, flags);
Emil Velikovfae59d72015-09-09 17:54:34 +01003203 if (ret)
3204 goto free_device;
3205 }
3206 return 0;
3207
3208free_device:
3209 free(*device);
3210 *device = NULL;
3211 return ret;
3212}
3213
Qiang Yu3c208932016-07-14 17:10:55 +08003214/* Consider devices located on the same bus as duplicate and fold the respective
3215 * entries into a single one.
3216 *
3217 * Note: this leaves "gaps" in the array, while preserving the length.
3218 */
Emil Velikovfae59d72015-09-09 17:54:34 +01003219static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count)
3220{
3221 int node_type, i, j;
3222
3223 for (i = 0; i < count; i++) {
3224 for (j = i + 1; j < count; j++) {
3225 if (drmCompareBusInfo(local_devices[i], local_devices[j]) == 0) {
3226 local_devices[i]->available_nodes |= local_devices[j]->available_nodes;
3227 node_type = log2(local_devices[j]->available_nodes);
3228 memcpy(local_devices[i]->nodes[node_type],
3229 local_devices[j]->nodes[node_type], drmGetMaxNodeName());
3230 drmFreeDevice(&local_devices[j]);
3231 }
3232 }
3233 }
3234}
3235
Emil Velikov11687bf2016-11-30 17:24:21 +00003236/* Check that the given flags are valid returning 0 on success */
3237static int
3238drm_device_validate_flags(uint32_t flags)
3239{
3240 return (flags & ~DRM_DEVICE_GET_PCI_REVISION);
3241}
3242
Emil Velikovb556ea12015-08-17 11:09:06 +08003243/**
Emil Velikovccedf662015-09-09 16:02:18 +01003244 * Get information about the opened drm device
3245 *
3246 * \param fd file descriptor of the drm device
Emil Velikov11687bf2016-11-30 17:24:21 +00003247 * \param flags feature/behaviour bitmask
Emil Velikovccedf662015-09-09 16:02:18 +01003248 * \param device the address of a drmDevicePtr where the information
3249 * will be allocated in stored
3250 *
3251 * \return zero on success, negative error code otherwise.
Emil Velikov11687bf2016-11-30 17:24:21 +00003252 *
3253 * \note Unlike drmGetDevice it does not retrieve the pci device revision field
3254 * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
Emil Velikovccedf662015-09-09 16:02:18 +01003255 */
Emil Velikov11687bf2016-11-30 17:24:21 +00003256int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device)
Emil Velikovccedf662015-09-09 16:02:18 +01003257{
Jonathan Gray08257922016-12-01 15:18:43 +11003258#ifdef __OpenBSD__
3259 /*
3260 * DRI device nodes on OpenBSD are not in their own directory, they reside
3261 * in /dev along with a large number of statically generated /dev nodes.
3262 * Avoid stat'ing all of /dev needlessly by implementing this custom path.
3263 */
3264 drmDevicePtr d;
3265 struct stat sbuf;
3266 char node[PATH_MAX + 1];
3267 const char *dev_name;
3268 int node_type, subsystem_type;
Jonathan Grayd5cf3f92016-12-17 16:09:51 +11003269 int maj, min, n, ret, base;
Jonathan Gray08257922016-12-01 15:18:43 +11003270
3271 if (fd == -1 || device == NULL)
3272 return -EINVAL;
3273
3274 if (fstat(fd, &sbuf))
3275 return -errno;
3276
3277 maj = major(sbuf.st_rdev);
3278 min = minor(sbuf.st_rdev);
3279
3280 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
3281 return -EINVAL;
3282
3283 node_type = drmGetMinorType(min);
3284 if (node_type == -1)
3285 return -ENODEV;
3286
3287 switch (node_type) {
3288 case DRM_NODE_PRIMARY:
3289 dev_name = DRM_DEV_NAME;
3290 break;
3291 case DRM_NODE_CONTROL:
3292 dev_name = DRM_CONTROL_DEV_NAME;
3293 break;
3294 case DRM_NODE_RENDER:
3295 dev_name = DRM_RENDER_DEV_NAME;
3296 break;
3297 default:
3298 return -EINVAL;
3299 };
3300
Jonathan Grayd5cf3f92016-12-17 16:09:51 +11003301 base = drmGetMinorBase(node_type);
3302 if (base < 0)
3303 return -EINVAL;
3304
3305 n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min - base);
Jonathan Gray08257922016-12-01 15:18:43 +11003306 if (n == -1 || n >= PATH_MAX)
3307 return -errno;
3308 if (stat(node, &sbuf))
3309 return -EINVAL;
3310
3311 subsystem_type = drmParseSubsystemType(maj, min);
3312 if (subsystem_type != DRM_BUS_PCI)
3313 return -ENODEV;
3314
3315 ret = drmProcessPciDevice(&d, node, node_type, maj, min, true, flags);
3316 if (ret)
3317 return ret;
3318
3319 *device = d;
3320
3321 return 0;
3322#else
Emil Velikovccedf662015-09-09 16:02:18 +01003323 drmDevicePtr *local_devices;
3324 drmDevicePtr d;
3325 DIR *sysdir;
3326 struct dirent *dent;
3327 struct stat sbuf;
3328 char node[PATH_MAX + 1];
3329 int node_type, subsystem_type;
3330 int maj, min;
3331 int ret, i, node_count;
3332 int max_count = 16;
Qiang Yu3c208932016-07-14 17:10:55 +08003333 dev_t find_rdev;
Emil Velikov11687bf2016-11-30 17:24:21 +00003334
3335 if (drm_device_validate_flags(flags))
3336 return -EINVAL;
Emil Velikovccedf662015-09-09 16:02:18 +01003337
3338 if (fd == -1 || device == NULL)
3339 return -EINVAL;
3340
3341 if (fstat(fd, &sbuf))
3342 return -errno;
3343
Qiang Yu3c208932016-07-14 17:10:55 +08003344 find_rdev = sbuf.st_rdev;
Emil Velikovccedf662015-09-09 16:02:18 +01003345 maj = major(sbuf.st_rdev);
3346 min = minor(sbuf.st_rdev);
3347
3348 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
3349 return -EINVAL;
3350
3351 subsystem_type = drmParseSubsystemType(maj, min);
3352
3353 local_devices = calloc(max_count, sizeof(drmDevicePtr));
3354 if (local_devices == NULL)
3355 return -ENOMEM;
3356
3357 sysdir = opendir(DRM_DIR_NAME);
3358 if (!sysdir) {
3359 ret = -errno;
Matt Ropercf0f0362015-10-16 15:11:23 -07003360 goto free_locals;
Emil Velikovccedf662015-09-09 16:02:18 +01003361 }
3362
3363 i = 0;
3364 while ((dent = readdir(sysdir))) {
3365 node_type = drmGetNodeType(dent->d_name);
3366 if (node_type < 0)
3367 continue;
3368
3369 snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, dent->d_name);
3370 if (stat(node, &sbuf))
3371 continue;
3372
3373 maj = major(sbuf.st_rdev);
3374 min = minor(sbuf.st_rdev);
3375
3376 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
3377 continue;
3378
3379 if (drmParseSubsystemType(maj, min) != subsystem_type)
3380 continue;
3381
3382 switch (subsystem_type) {
3383 case DRM_BUS_PCI:
Emil Velikov138d2312016-11-30 17:13:51 +00003384 ret = drmProcessPciDevice(&d, node, node_type, maj, min, true, flags);
Emil Velikovccedf662015-09-09 16:02:18 +01003385 if (ret)
3386 goto free_devices;
3387
3388 break;
3389 default:
Matt Roperba5a6ec2015-10-16 15:11:24 -07003390 continue;
Emil Velikovccedf662015-09-09 16:02:18 +01003391 }
3392
3393 if (i >= max_count) {
3394 drmDevicePtr *temp;
3395
3396 max_count += 16;
3397 temp = realloc(local_devices, max_count * sizeof(drmDevicePtr));
3398 if (!temp)
3399 goto free_devices;
3400 local_devices = temp;
3401 }
3402
Qiang Yu3c208932016-07-14 17:10:55 +08003403 /* store target at local_devices[0] for ease to use below */
3404 if (find_rdev == sbuf.st_rdev && i) {
3405 local_devices[i] = local_devices[0];
3406 local_devices[0] = d;
3407 }
3408 else
3409 local_devices[i] = d;
Emil Velikovccedf662015-09-09 16:02:18 +01003410 i++;
3411 }
3412 node_count = i;
3413
Emil Velikovccedf662015-09-09 16:02:18 +01003414 drmFoldDuplicatedDevices(local_devices, node_count);
3415
3416 *device = local_devices[0];
Qiang Yu3c208932016-07-14 17:10:55 +08003417 drmFreeDevices(&local_devices[1], node_count - 1);
Emil Velikovccedf662015-09-09 16:02:18 +01003418
Emil Velikovccedf662015-09-09 16:02:18 +01003419 closedir(sysdir);
Matt Ropercf0f0362015-10-16 15:11:23 -07003420 free(local_devices);
Rob Herring677cd972016-10-21 10:07:59 -07003421 if (*device == NULL)
3422 return -ENODEV;
Emil Velikovccedf662015-09-09 16:02:18 +01003423 return 0;
3424
3425free_devices:
3426 drmFreeDevices(local_devices, i);
Emil Velikovccedf662015-09-09 16:02:18 +01003427 closedir(sysdir);
Matt Ropercf0f0362015-10-16 15:11:23 -07003428
3429free_locals:
3430 free(local_devices);
Emil Velikovccedf662015-09-09 16:02:18 +01003431 return ret;
Jonathan Gray08257922016-12-01 15:18:43 +11003432#endif
Emil Velikovccedf662015-09-09 16:02:18 +01003433}
3434
3435/**
Emil Velikov11687bf2016-11-30 17:24:21 +00003436 * Get information about the opened drm device
3437 *
3438 * \param fd file descriptor of the drm device
3439 * \param device the address of a drmDevicePtr where the information
3440 * will be allocated in stored
3441 *
3442 * \return zero on success, negative error code otherwise.
3443 */
3444int drmGetDevice(int fd, drmDevicePtr *device)
3445{
3446 return drmGetDevice2(fd, DRM_DEVICE_GET_PCI_REVISION, device);
3447}
3448
3449/**
Emil Velikovb556ea12015-08-17 11:09:06 +08003450 * Get drm devices on the system
3451 *
Emil Velikov11687bf2016-11-30 17:24:21 +00003452 * \param flags feature/behaviour bitmask
Emil Velikovb556ea12015-08-17 11:09:06 +08003453 * \param devices the array of devices with drmDevicePtr elements
3454 * can be NULL to get the device number first
3455 * \param max_devices the maximum number of devices for the array
3456 *
3457 * \return on error - negative error code,
3458 * if devices is NULL - total number of devices available on the system,
3459 * alternatively the number of devices stored in devices[], which is
3460 * capped by the max_devices.
Emil Velikov11687bf2016-11-30 17:24:21 +00003461 *
3462 * \note Unlike drmGetDevices it does not retrieve the pci device revision field
3463 * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
Emil Velikovb556ea12015-08-17 11:09:06 +08003464 */
Emil Velikov11687bf2016-11-30 17:24:21 +00003465int drmGetDevices2(uint32_t flags, drmDevicePtr devices[], int max_devices)
Emil Velikovb556ea12015-08-17 11:09:06 +08003466{
Emil Velikov5f68d312015-09-07 13:54:32 +01003467 drmDevicePtr *local_devices;
3468 drmDevicePtr device;
3469 DIR *sysdir;
3470 struct dirent *dent;
3471 struct stat sbuf;
3472 char node[PATH_MAX + 1];
Emil Velikovb556ea12015-08-17 11:09:06 +08003473 int node_type, subsystem_type;
3474 int maj, min;
Emil Velikovfae59d72015-09-09 17:54:34 +01003475 int ret, i, node_count, device_count;
Emil Velikovb556ea12015-08-17 11:09:06 +08003476 int max_count = 16;
Emil Velikov11687bf2016-11-30 17:24:21 +00003477
3478 if (drm_device_validate_flags(flags))
3479 return -EINVAL;
Emil Velikovb556ea12015-08-17 11:09:06 +08003480
Emil Velikov5f68d312015-09-07 13:54:32 +01003481 local_devices = calloc(max_count, sizeof(drmDevicePtr));
3482 if (local_devices == NULL)
Emil Velikovb556ea12015-08-17 11:09:06 +08003483 return -ENOMEM;
3484
3485 sysdir = opendir(DRM_DIR_NAME);
3486 if (!sysdir) {
3487 ret = -errno;
Matt Roper8c4a1cb2015-09-30 09:30:51 -07003488 goto free_locals;
Emil Velikovb556ea12015-08-17 11:09:06 +08003489 }
3490
Emil Velikov5f68d312015-09-07 13:54:32 +01003491 i = 0;
Emil Velikovb556ea12015-08-17 11:09:06 +08003492 while ((dent = readdir(sysdir))) {
3493 node_type = drmGetNodeType(dent->d_name);
3494 if (node_type < 0)
3495 continue;
3496
3497 snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, dent->d_name);
3498 if (stat(node, &sbuf))
3499 continue;
3500
3501 maj = major(sbuf.st_rdev);
3502 min = minor(sbuf.st_rdev);
3503
3504 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
3505 continue;
3506
Emil Velikova250fce2015-09-07 12:54:27 +01003507 subsystem_type = drmParseSubsystemType(maj, min);
Emil Velikovb556ea12015-08-17 11:09:06 +08003508
3509 if (subsystem_type < 0)
3510 continue;
3511
3512 switch (subsystem_type) {
3513 case DRM_BUS_PCI:
Emil Velikovb40a65d2016-11-30 16:41:27 +00003514 ret = drmProcessPciDevice(&device, node, node_type,
Emil Velikov138d2312016-11-30 17:13:51 +00003515 maj, min, devices != NULL, flags);
Emil Velikovb556ea12015-08-17 11:09:06 +08003516 if (ret)
Emil Velikov5f68d312015-09-07 13:54:32 +01003517 goto free_devices;
Emil Velikovb556ea12015-08-17 11:09:06 +08003518
Emil Velikovb556ea12015-08-17 11:09:06 +08003519 break;
3520 default:
Matt Roperba5a6ec2015-10-16 15:11:24 -07003521 continue;
Emil Velikovb556ea12015-08-17 11:09:06 +08003522 }
Emil Velikov5f68d312015-09-07 13:54:32 +01003523
3524 if (i >= max_count) {
3525 drmDevicePtr *temp;
3526
3527 max_count += 16;
3528 temp = realloc(local_devices, max_count * sizeof(drmDevicePtr));
3529 if (!temp)
3530 goto free_devices;
3531 local_devices = temp;
3532 }
3533
3534 local_devices[i] = device;
Emil Velikovb556ea12015-08-17 11:09:06 +08003535 i++;
3536 }
Emil Velikovb556ea12015-08-17 11:09:06 +08003537 node_count = i;
3538
Emil Velikovfae59d72015-09-09 17:54:34 +01003539 drmFoldDuplicatedDevices(local_devices, node_count);
Emil Velikovb556ea12015-08-17 11:09:06 +08003540
Emil Velikov5f68d312015-09-07 13:54:32 +01003541 device_count = 0;
Qiang Yu70b64072016-06-06 12:29:16 -04003542 for (i = 0; i < node_count; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -04003543 if (!local_devices[i])
3544 continue;
Qiang Yu70b64072016-06-06 12:29:16 -04003545
Emil Velikov5f68d312015-09-07 13:54:32 +01003546 if ((devices != NULL) && (device_count < max_devices))
3547 devices[device_count] = local_devices[i];
3548 else
3549 drmFreeDevice(&local_devices[i]);
3550
3551 device_count++;
Emil Velikovb556ea12015-08-17 11:09:06 +08003552 }
3553
Emil Velikovb556ea12015-08-17 11:09:06 +08003554 closedir(sysdir);
Matt Roper8c4a1cb2015-09-30 09:30:51 -07003555 free(local_devices);
Emil Velikov5f68d312015-09-07 13:54:32 +01003556 return device_count;
Emil Velikovb556ea12015-08-17 11:09:06 +08003557
Emil Velikov5f68d312015-09-07 13:54:32 +01003558free_devices:
3559 drmFreeDevices(local_devices, i);
Emil Velikovb556ea12015-08-17 11:09:06 +08003560 closedir(sysdir);
Matt Roper8c4a1cb2015-09-30 09:30:51 -07003561
3562free_locals:
3563 free(local_devices);
Emil Velikovb556ea12015-08-17 11:09:06 +08003564 return ret;
3565}
Emil Velikov37d790f2016-11-10 17:26:50 +00003566
Emil Velikov11687bf2016-11-30 17:24:21 +00003567/**
3568 * Get drm devices on the system
3569 *
3570 * \param devices the array of devices with drmDevicePtr elements
3571 * can be NULL to get the device number first
3572 * \param max_devices the maximum number of devices for the array
3573 *
3574 * \return on error - negative error code,
3575 * if devices is NULL - total number of devices available on the system,
3576 * alternatively the number of devices stored in devices[], which is
3577 * capped by the max_devices.
3578 */
3579int drmGetDevices(drmDevicePtr devices[], int max_devices)
3580{
3581 return drmGetDevices2(DRM_DEVICE_GET_PCI_REVISION, devices, max_devices);
3582}
3583
Emil Velikov37d790f2016-11-10 17:26:50 +00003584char *drmGetDeviceNameFromFd2(int fd)
3585{
3586#ifdef __linux__
3587 struct stat sbuf;
3588 char *device_name = NULL;
3589 unsigned int maj, min;
3590 FILE *f;
3591 char buf[512];
3592 static const char match[9] = "\nDEVNAME=";
Grazvydas Ignotas3bc14c82016-12-11 20:03:56 +02003593 size_t expected = 1;
Emil Velikov37d790f2016-11-10 17:26:50 +00003594
3595
3596 if (fstat(fd, &sbuf))
3597 return NULL;
3598
3599 maj = major(sbuf.st_rdev);
3600 min = minor(sbuf.st_rdev);
3601
3602 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
3603 return NULL;
3604
3605 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/uevent", maj, min);
3606 if (!(f = fopen(buf, "r")))
3607 return NULL;
3608
3609 while (expected < sizeof(match)) {
3610 int c = getc(f);
3611
3612 if (c == EOF) {
3613 fclose(f);
3614 return NULL;
3615 } else if (c == match[expected] )
3616 expected++;
3617 else
3618 expected = 0;
3619 }
3620
3621 strcpy(buf, "/dev/");
3622 if (fgets(buf + 5, sizeof(buf) - 5, f)) {
3623 buf[strcspn(buf, "\n")] = '\0';
3624 device_name = strdup(buf);
3625 }
3626
3627 fclose(f);
3628 return device_name;
3629#else
3630#warning "Missing implementation of drmGetDeviceNameFromFd2"
3631 return NULL;
3632#endif
3633}