drm_cdevsw.c revision 1.1 1 1.1 riastrad /* $NetBSD: drm_cdevsw.c,v 1.1 2018/08/27 06:31:41 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 riastrad * by Taylor R. Campbell.
9 1.1 riastrad *
10 1.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1 riastrad * modification, are permitted provided that the following conditions
12 1.1 riastrad * are met:
13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1 riastrad *
19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 riastrad */
31 1.1 riastrad
32 1.1 riastrad #include <sys/cdefs.h>
33 1.1 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_cdevsw.c,v 1.1 2018/08/27 06:31:41 riastradh Exp $");
34 1.1 riastrad
35 1.1 riastrad #include <sys/param.h>
36 1.1 riastrad #include <sys/types.h>
37 1.1 riastrad #include <sys/conf.h>
38 1.1 riastrad #include <sys/device.h>
39 1.1 riastrad #include <sys/file.h>
40 1.1 riastrad #include <sys/filedesc.h>
41 1.1 riastrad #include <sys/ioccom.h>
42 1.1 riastrad #include <sys/kauth.h>
43 1.1 riastrad #ifndef _MODULE
44 1.1 riastrad /* XXX Mega-kludge because modules are broken. */
45 1.1 riastrad #include <sys/once.h>
46 1.1 riastrad #endif
47 1.1 riastrad #include <sys/pmf.h>
48 1.1 riastrad #include <sys/poll.h>
49 1.1 riastrad #ifndef _MODULE
50 1.1 riastrad #include <sys/reboot.h> /* XXX drm_init kludge */
51 1.1 riastrad #endif
52 1.1 riastrad #include <sys/select.h>
53 1.1 riastrad
54 1.1 riastrad #include <uvm/uvm_extern.h>
55 1.1 riastrad
56 1.1 riastrad #include <linux/err.h>
57 1.1 riastrad
58 1.1 riastrad #include <linux/pm.h>
59 1.1 riastrad
60 1.1 riastrad #include <drm/drmP.h>
61 1.1 riastrad #include "../dist/drm/drm_legacy.h"
62 1.1 riastrad
63 1.1 riastrad static dev_type_open(drm_open);
64 1.1 riastrad
65 1.1 riastrad static int drm_firstopen(struct drm_device *);
66 1.1 riastrad
67 1.1 riastrad static int drm_close(struct file *);
68 1.1 riastrad static int drm_read(struct file *, off_t *, struct uio *, kauth_cred_t,
69 1.1 riastrad int);
70 1.1 riastrad static int drm_dequeue_event(struct drm_file *, size_t,
71 1.1 riastrad struct drm_pending_event **, int);
72 1.1 riastrad static int drm_poll(struct file *, int);
73 1.1 riastrad static int drm_kqfilter(struct file *, struct knote *);
74 1.1 riastrad static int drm_stat(struct file *, struct stat *);
75 1.1 riastrad static int drm_ioctl(struct file *, unsigned long, void *);
76 1.1 riastrad static int drm_fop_mmap(struct file *, off_t *, size_t, int, int *, int *,
77 1.1 riastrad struct uvm_object **, int *);
78 1.1 riastrad static int drm_version_string(char *, size_t *, const char *);
79 1.1 riastrad static paddr_t drm_mmap(dev_t, off_t, int);
80 1.1 riastrad
81 1.1 riastrad static drm_ioctl_t drm_version;
82 1.1 riastrad
83 1.1 riastrad #define DRM_IOCTL_DEF(IOCTL, FUNC, FLAGS) \
84 1.1 riastrad [DRM_IOCTL_NR(IOCTL)] = { \
85 1.1 riastrad .cmd = (IOCTL), \
86 1.1 riastrad .flags = (FLAGS), \
87 1.1 riastrad .func = (FUNC), \
88 1.1 riastrad .cmd_drv = 0, \
89 1.1 riastrad }
90 1.1 riastrad
91 1.1 riastrad #if __OS_HAS_AGP
92 1.1 riastrad /* XXX Kludge for AGP. */
93 1.1 riastrad static drm_ioctl_t drm_agp_acquire_hook_ioctl;
94 1.1 riastrad static drm_ioctl_t drm_agp_release_hook_ioctl;
95 1.1 riastrad static drm_ioctl_t drm_agp_enable_hook_ioctl;
96 1.1 riastrad static drm_ioctl_t drm_agp_info_hook_ioctl;
97 1.1 riastrad static drm_ioctl_t drm_agp_alloc_hook_ioctl;
98 1.1 riastrad static drm_ioctl_t drm_agp_free_hook_ioctl;
99 1.1 riastrad static drm_ioctl_t drm_agp_bind_hook_ioctl;
100 1.1 riastrad static drm_ioctl_t drm_agp_unbind_hook_ioctl;
101 1.1 riastrad
102 1.1 riastrad #define drm_agp_acquire_ioctl drm_agp_acquire_hook_ioctl
103 1.1 riastrad #define drm_agp_release_ioctl drm_agp_release_hook_ioctl
104 1.1 riastrad #define drm_agp_enable_ioctl drm_agp_enable_hook_ioctl
105 1.1 riastrad #define drm_agp_info_ioctl drm_agp_info_hook_ioctl
106 1.1 riastrad #define drm_agp_alloc_ioctl drm_agp_alloc_hook_ioctl
107 1.1 riastrad #define drm_agp_free_ioctl drm_agp_free_hook_ioctl
108 1.1 riastrad #define drm_agp_bind_ioctl drm_agp_bind_hook_ioctl
109 1.1 riastrad #define drm_agp_unbind_ioctl drm_agp_unbind_hook_ioctl
110 1.1 riastrad #endif
111 1.1 riastrad
112 1.1 riastrad /* Table copied verbatim from dist/drm/drm_drv.c. */
113 1.1 riastrad static const struct drm_ioctl_desc drm_ioctls[] = {
114 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_VERSION, drm_version, DRM_UNLOCKED|DRM_RENDER_ALLOW),
115 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_UNIQUE, drm_getunique, 0),
116 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_MAGIC, drm_getmagic, 0),
117 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_IRQ_BUSID, drm_irq_by_busid, DRM_MASTER|DRM_ROOT_ONLY),
118 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_MAP, drm_getmap, DRM_UNLOCKED),
119 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_CLIENT, drm_getclient, DRM_UNLOCKED),
120 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_STATS, drm_getstats, DRM_UNLOCKED),
121 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_CAP, drm_getcap, DRM_UNLOCKED|DRM_RENDER_ALLOW),
122 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SET_CLIENT_CAP, drm_setclientcap, 0),
123 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SET_VERSION, drm_setversion, DRM_MASTER),
124 1.1 riastrad
125 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SET_UNIQUE, drm_setunique, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
126 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_BLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
127 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_UNBLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
128 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AUTH_MAGIC, drm_authmagic, DRM_AUTH|DRM_MASTER),
129 1.1 riastrad
130 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_ADD_MAP, drm_addmap_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
131 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_RM_MAP, drm_rmmap_ioctl, DRM_AUTH),
132 1.1 riastrad
133 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SET_SAREA_CTX, drm_setsareactx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
134 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_SAREA_CTX, drm_getsareactx, DRM_AUTH),
135 1.1 riastrad
136 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SET_MASTER, drm_setmaster_ioctl, DRM_ROOT_ONLY),
137 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_DROP_MASTER, drm_dropmaster_ioctl, DRM_ROOT_ONLY),
138 1.1 riastrad
139 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_ADD_CTX, drm_addctx, DRM_AUTH|DRM_ROOT_ONLY),
140 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_RM_CTX, drm_rmctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
141 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MOD_CTX, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
142 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_CTX, drm_getctx, DRM_AUTH),
143 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SWITCH_CTX, drm_switchctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
144 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_NEW_CTX, drm_newctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
145 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_RES_CTX, drm_resctx, DRM_AUTH),
146 1.1 riastrad
147 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_ADD_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
148 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_RM_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
149 1.1 riastrad
150 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_LOCK, drm_lock, DRM_AUTH),
151 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_UNLOCK, drm_unlock, DRM_AUTH),
152 1.1 riastrad
153 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_FINISH, drm_noop, DRM_AUTH),
154 1.1 riastrad
155 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_ADD_BUFS, drm_addbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
156 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MARK_BUFS, drm_markbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
157 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_INFO_BUFS, drm_infobufs, DRM_AUTH),
158 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MAP_BUFS, drm_mapbufs, DRM_AUTH),
159 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_FREE_BUFS, drm_freebufs, DRM_AUTH),
160 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_DMA, drm_dma_ioctl, DRM_AUTH),
161 1.1 riastrad
162 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_CONTROL, drm_control, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
163 1.1 riastrad
164 1.1 riastrad #if __OS_HAS_AGP
165 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_ACQUIRE, drm_agp_acquire_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
166 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_RELEASE, drm_agp_release_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
167 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_ENABLE, drm_agp_enable_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
168 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_INFO, drm_agp_info_ioctl, DRM_AUTH),
169 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_ALLOC, drm_agp_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
170 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_FREE, drm_agp_free_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
171 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_BIND, drm_agp_bind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
172 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_UNBIND, drm_agp_unbind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
173 1.1 riastrad #endif
174 1.1 riastrad
175 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SG_ALLOC, drm_sg_alloc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
176 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SG_FREE, drm_sg_free, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
177 1.1 riastrad
178 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_WAIT_VBLANK, drm_wait_vblank, DRM_UNLOCKED),
179 1.1 riastrad
180 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODESET_CTL, drm_modeset_ctl, 0),
181 1.1 riastrad
182 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_UPDATE_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
183 1.1 riastrad
184 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GEM_CLOSE, drm_gem_close_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
185 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GEM_FLINK, drm_gem_flink_ioctl, DRM_AUTH|DRM_UNLOCKED),
186 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GEM_OPEN, drm_gem_open_ioctl, DRM_AUTH|DRM_UNLOCKED),
187 1.1 riastrad
188 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETRESOURCES, drm_mode_getresources, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
189 1.1 riastrad
190 1.1 riastrad #ifndef __NetBSD__ /* XXX drm prime */
191 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_PRIME_HANDLE_TO_FD, drm_prime_handle_to_fd_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
192 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_PRIME_FD_TO_HANDLE, drm_prime_fd_to_handle_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
193 1.1 riastrad #endif
194 1.1 riastrad
195 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANERESOURCES, drm_mode_getplane_res, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
196 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCRTC, drm_mode_getcrtc, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
197 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETCRTC, drm_mode_setcrtc, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
198 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANE, drm_mode_getplane, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
199 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPLANE, drm_mode_setplane, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
200 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR, drm_mode_cursor_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
201 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETGAMMA, drm_mode_gamma_get_ioctl, DRM_UNLOCKED),
202 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETGAMMA, drm_mode_gamma_set_ioctl, DRM_MASTER|DRM_UNLOCKED),
203 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETENCODER, drm_mode_getencoder, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
204 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCONNECTOR, drm_mode_getconnector, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
205 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATTACHMODE, drm_noop, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
206 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_DETACHMODE, drm_noop, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
207 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPERTY, drm_mode_getproperty_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
208 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPROPERTY, drm_mode_connector_property_set_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
209 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPBLOB, drm_mode_getblob_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
210 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETFB, drm_mode_getfb, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
211 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB, drm_mode_addfb, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
212 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB2, drm_mode_addfb2, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
213 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_RMFB, drm_mode_rmfb, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
214 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_PAGE_FLIP, drm_mode_page_flip_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
215 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_DIRTYFB, drm_mode_dirtyfb_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
216 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATE_DUMB, drm_mode_create_dumb_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
217 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_MAP_DUMB, drm_mode_mmap_dumb_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
218 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROY_DUMB, drm_mode_destroy_dumb_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
219 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_OBJ_GETPROPERTIES, drm_mode_obj_get_properties_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
220 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_OBJ_SETPROPERTY, drm_mode_obj_set_property_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
221 1.1 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR2, drm_mode_cursor2_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
222 1.1 riastrad };
223 1.1 riastrad
224 1.1 riastrad const struct cdevsw drm_cdevsw = {
225 1.1 riastrad .d_open = drm_open,
226 1.1 riastrad .d_close = noclose,
227 1.1 riastrad .d_read = noread,
228 1.1 riastrad .d_write = nowrite,
229 1.1 riastrad .d_ioctl = noioctl,
230 1.1 riastrad .d_stop = nostop,
231 1.1 riastrad .d_tty = notty,
232 1.1 riastrad .d_poll = nopoll,
233 1.1 riastrad .d_mmap = drm_mmap,
234 1.1 riastrad .d_kqfilter = nokqfilter,
235 1.1 riastrad .d_discard = nodiscard,
236 1.1 riastrad /* XXX was D_TTY | D_NEGOFFSAFE */
237 1.1 riastrad /* XXX Add D_MPSAFE some day... */
238 1.1 riastrad .d_flag = D_NEGOFFSAFE,
239 1.1 riastrad };
240 1.1 riastrad
241 1.1 riastrad static const struct fileops drm_fileops = {
242 1.1 riastrad .fo_name = "drm",
243 1.1 riastrad .fo_read = drm_read,
244 1.1 riastrad .fo_write = fbadop_write,
245 1.1 riastrad .fo_ioctl = drm_ioctl,
246 1.1 riastrad .fo_fcntl = fnullop_fcntl,
247 1.1 riastrad .fo_poll = drm_poll,
248 1.1 riastrad .fo_stat = drm_stat,
249 1.1 riastrad .fo_close = drm_close,
250 1.1 riastrad .fo_kqfilter = drm_kqfilter,
251 1.1 riastrad .fo_restart = fnullop_restart,
252 1.1 riastrad .fo_mmap = drm_fop_mmap,
253 1.1 riastrad };
254 1.1 riastrad
255 1.1 riastrad static int
256 1.1 riastrad drm_open(dev_t d, int flags, int fmt, struct lwp *l)
257 1.1 riastrad {
258 1.1 riastrad struct drm_minor *dminor;
259 1.1 riastrad struct drm_device *dev;
260 1.1 riastrad bool firstopen, lastclose;
261 1.1 riastrad int fd;
262 1.1 riastrad struct file *fp;
263 1.1 riastrad int error;
264 1.1 riastrad extern int drm_guarantee_initialized(void);
265 1.1 riastrad
266 1.1 riastrad error = drm_guarantee_initialized();
267 1.1 riastrad if (error)
268 1.1 riastrad goto fail0;
269 1.1 riastrad
270 1.1 riastrad if (flags & O_EXCL) {
271 1.1 riastrad error = EBUSY;
272 1.1 riastrad goto fail0;
273 1.1 riastrad }
274 1.1 riastrad
275 1.1 riastrad dminor = drm_minor_acquire(minor(d));
276 1.1 riastrad if (IS_ERR(dminor)) {
277 1.1 riastrad /* XXX errno Linux->NetBSD */
278 1.1 riastrad error = -PTR_ERR(dminor);
279 1.1 riastrad goto fail0;
280 1.1 riastrad }
281 1.1 riastrad dev = dminor->dev;
282 1.1 riastrad if (dev->switch_power_state != DRM_SWITCH_POWER_ON) {
283 1.1 riastrad error = EINVAL;
284 1.1 riastrad goto fail1;
285 1.1 riastrad }
286 1.1 riastrad
287 1.1 riastrad spin_lock(&dev->count_lock);
288 1.1 riastrad if (dev->open_count == INT_MAX) {
289 1.1 riastrad spin_unlock(&dev->count_lock);
290 1.1 riastrad error = EBUSY;
291 1.1 riastrad goto fail1;
292 1.1 riastrad }
293 1.1 riastrad firstopen = (dev->open_count == 0);
294 1.1 riastrad dev->open_count++;
295 1.1 riastrad spin_unlock(&dev->count_lock);
296 1.1 riastrad
297 1.1 riastrad if (firstopen) {
298 1.1 riastrad /* XXX errno Linux->NetBSD */
299 1.1 riastrad error = drm_firstopen(dev);
300 1.1 riastrad if (error)
301 1.1 riastrad goto fail2;
302 1.1 riastrad }
303 1.1 riastrad
304 1.1 riastrad error = fd_allocfile(&fp, &fd);
305 1.1 riastrad if (error)
306 1.1 riastrad goto fail2;
307 1.1 riastrad
308 1.1 riastrad struct drm_file *const file = kmem_zalloc(sizeof(*file), KM_SLEEP);
309 1.1 riastrad /* XXX errno Linux->NetBSD */
310 1.1 riastrad error = -drm_open_file(file, fp, dminor);
311 1.1 riastrad if (error)
312 1.1 riastrad goto fail3;
313 1.1 riastrad
314 1.1 riastrad error = fd_clone(fp, fd, flags, &drm_fileops, file);
315 1.1 riastrad KASSERT(error == EMOVEFD); /* XXX */
316 1.1 riastrad
317 1.1 riastrad /* Success! (But error has to be EMOVEFD, not 0.) */
318 1.1 riastrad return error;
319 1.1 riastrad
320 1.1 riastrad fail3: kmem_free(file, sizeof(*file));
321 1.1 riastrad fd_abort(curproc, fp, fd);
322 1.1 riastrad fail2: spin_lock(&dev->count_lock);
323 1.1 riastrad KASSERT(0 < dev->open_count);
324 1.1 riastrad --dev->open_count;
325 1.1 riastrad lastclose = (dev->open_count == 0);
326 1.1 riastrad spin_unlock(&dev->count_lock);
327 1.1 riastrad if (lastclose)
328 1.1 riastrad (void)drm_lastclose(dev);
329 1.1 riastrad fail1: drm_minor_release(dminor);
330 1.1 riastrad fail0: KASSERT(error);
331 1.1 riastrad return error;
332 1.1 riastrad }
333 1.1 riastrad
334 1.1 riastrad static int
335 1.1 riastrad drm_close(struct file *fp)
336 1.1 riastrad {
337 1.1 riastrad struct drm_file *const file = fp->f_data;
338 1.1 riastrad struct drm_minor *const dminor = file->minor;
339 1.1 riastrad struct drm_device *const dev = dminor->dev;
340 1.1 riastrad bool lastclose;
341 1.1 riastrad
342 1.1 riastrad drm_close_file(file);
343 1.1 riastrad kmem_free(file, sizeof(*file));
344 1.1 riastrad
345 1.1 riastrad spin_lock(&dev->count_lock);
346 1.1 riastrad KASSERT(0 < dev->open_count);
347 1.1 riastrad --dev->open_count;
348 1.1 riastrad lastclose = (dev->open_count == 0);
349 1.1 riastrad spin_unlock(&dev->count_lock);
350 1.1 riastrad
351 1.1 riastrad if (lastclose)
352 1.1 riastrad (void)drm_lastclose(dev);
353 1.1 riastrad
354 1.1 riastrad drm_minor_release(dminor);
355 1.1 riastrad
356 1.1 riastrad return 0;
357 1.1 riastrad }
358 1.1 riastrad
359 1.1 riastrad static int
360 1.1 riastrad drm_firstopen(struct drm_device *dev)
361 1.1 riastrad {
362 1.1 riastrad int ret;
363 1.1 riastrad
364 1.1 riastrad if (drm_core_check_feature(dev, DRIVER_MODESET))
365 1.1 riastrad return 0;
366 1.1 riastrad
367 1.1 riastrad if (dev->driver->firstopen) {
368 1.1 riastrad ret = (*dev->driver->firstopen)(dev);
369 1.1 riastrad if (ret)
370 1.1 riastrad goto fail0;
371 1.1 riastrad }
372 1.1 riastrad
373 1.1 riastrad ret = drm_legacy_dma_setup(dev);
374 1.1 riastrad if (ret)
375 1.1 riastrad goto fail1;
376 1.1 riastrad
377 1.1 riastrad return 0;
378 1.1 riastrad
379 1.1 riastrad fail2: __unused
380 1.1 riastrad drm_legacy_dma_takedown(dev);
381 1.1 riastrad fail1: if (dev->driver->lastclose)
382 1.1 riastrad (*dev->driver->lastclose)(dev);
383 1.1 riastrad fail0: KASSERT(ret);
384 1.1 riastrad return ret;
385 1.1 riastrad }
386 1.1 riastrad
387 1.1 riastrad int
388 1.1 riastrad drm_lastclose(struct drm_device *dev)
389 1.1 riastrad {
390 1.1 riastrad struct drm_vma_entry *vma, *vma_temp;
391 1.1 riastrad
392 1.1 riastrad /* XXX Order is sketchy here... */
393 1.1 riastrad if (dev->driver->lastclose)
394 1.1 riastrad (*dev->driver->lastclose)(dev);
395 1.1 riastrad if (dev->irq_enabled && !drm_core_check_feature(dev, DRIVER_MODESET))
396 1.1 riastrad drm_irq_uninstall(dev);
397 1.1 riastrad
398 1.1 riastrad mutex_lock(&dev->struct_mutex);
399 1.1 riastrad if (dev->agp)
400 1.1 riastrad drm_agp_clear_hook(dev);
401 1.1 riastrad drm_legacy_sg_cleanup(dev);
402 1.1 riastrad list_for_each_entry_safe(vma, vma_temp, &dev->vmalist, head) {
403 1.1 riastrad list_del(&vma->head);
404 1.1 riastrad kfree(vma);
405 1.1 riastrad }
406 1.1 riastrad drm_legacy_dma_takedown(dev);
407 1.1 riastrad mutex_unlock(&dev->struct_mutex);
408 1.1 riastrad
409 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
410 1.1 riastrad dev->sigdata.lock = NULL;
411 1.1 riastrad dev->context_flag = 0;
412 1.1 riastrad dev->last_context = 0;
413 1.1 riastrad dev->if_version = 0;
414 1.1 riastrad }
415 1.1 riastrad
416 1.1 riastrad return 0;
417 1.1 riastrad }
418 1.1 riastrad
419 1.1 riastrad static int
420 1.1 riastrad drm_read(struct file *fp, off_t *off, struct uio *uio, kauth_cred_t cred,
421 1.1 riastrad int flags)
422 1.1 riastrad {
423 1.1 riastrad struct drm_file *const file = fp->f_data;
424 1.1 riastrad struct drm_pending_event *event;
425 1.1 riastrad bool first;
426 1.1 riastrad int error = 0;
427 1.1 riastrad
428 1.1 riastrad for (first = true; ; first = false) {
429 1.1 riastrad int f = 0;
430 1.1 riastrad
431 1.1 riastrad if (!first || ISSET(fp->f_flag, FNONBLOCK))
432 1.1 riastrad f |= FNONBLOCK;
433 1.1 riastrad
434 1.1 riastrad /* XXX errno Linux->NetBSD */
435 1.1 riastrad error = -drm_dequeue_event(file, uio->uio_resid, &event, f);
436 1.1 riastrad if (error) {
437 1.1 riastrad if ((error == EWOULDBLOCK) && !first)
438 1.1 riastrad error = 0;
439 1.1 riastrad break;
440 1.1 riastrad }
441 1.1 riastrad if (event == NULL)
442 1.1 riastrad break;
443 1.1 riastrad error = uiomove(event->event, event->event->length, uio);
444 1.1 riastrad if (error) /* XXX Requeue the event? */
445 1.1 riastrad break;
446 1.1 riastrad (*event->destroy)(event);
447 1.1 riastrad }
448 1.1 riastrad
449 1.1 riastrad /* Success! */
450 1.1 riastrad return error;
451 1.1 riastrad }
452 1.1 riastrad
453 1.1 riastrad static int
454 1.1 riastrad drm_dequeue_event(struct drm_file *file, size_t max_length,
455 1.1 riastrad struct drm_pending_event **eventp, int flags)
456 1.1 riastrad {
457 1.1 riastrad struct drm_device *const dev = file->minor->dev;
458 1.1 riastrad struct drm_pending_event *event = NULL;
459 1.1 riastrad unsigned long irqflags;
460 1.1 riastrad int ret = 0;
461 1.1 riastrad
462 1.1 riastrad spin_lock_irqsave(&dev->event_lock, irqflags);
463 1.1 riastrad
464 1.1 riastrad if (ISSET(flags, FNONBLOCK)) {
465 1.1 riastrad if (list_empty(&file->event_list))
466 1.1 riastrad ret = -EWOULDBLOCK;
467 1.1 riastrad } else {
468 1.1 riastrad DRM_SPIN_WAIT_UNTIL(ret, &file->event_wait, &dev->event_lock,
469 1.1 riastrad !list_empty(&file->event_list));
470 1.1 riastrad }
471 1.1 riastrad if (ret)
472 1.1 riastrad goto out;
473 1.1 riastrad
474 1.1 riastrad event = list_first_entry(&file->event_list, struct drm_pending_event,
475 1.1 riastrad link);
476 1.1 riastrad if (event->event->length > max_length) {
477 1.1 riastrad /* Event is too large, can't return it. */
478 1.1 riastrad event = NULL;
479 1.1 riastrad ret = 0;
480 1.1 riastrad goto out;
481 1.1 riastrad }
482 1.1 riastrad
483 1.1 riastrad file->event_space += event->event->length;
484 1.1 riastrad list_del(&event->link);
485 1.1 riastrad
486 1.1 riastrad out: spin_unlock_irqrestore(&dev->event_lock, irqflags);
487 1.1 riastrad *eventp = event;
488 1.1 riastrad return ret;
489 1.1 riastrad }
490 1.1 riastrad
491 1.1 riastrad static int
492 1.1 riastrad drm_poll(struct file *fp __unused, int events __unused)
493 1.1 riastrad {
494 1.1 riastrad struct drm_file *const file = fp->f_data;
495 1.1 riastrad struct drm_device *const dev = file->minor->dev;
496 1.1 riastrad int revents = 0;
497 1.1 riastrad unsigned long irqflags;
498 1.1 riastrad
499 1.1 riastrad if (!ISSET(events, (POLLIN | POLLRDNORM)))
500 1.1 riastrad return 0;
501 1.1 riastrad
502 1.1 riastrad spin_lock_irqsave(&dev->event_lock, irqflags);
503 1.1 riastrad if (list_empty(&file->event_list))
504 1.1 riastrad selrecord(curlwp, &file->event_selq);
505 1.1 riastrad else
506 1.1 riastrad revents |= (events & (POLLIN | POLLRDNORM));
507 1.1 riastrad spin_unlock_irqrestore(&dev->event_lock, irqflags);
508 1.1 riastrad
509 1.1 riastrad return revents;
510 1.1 riastrad }
511 1.1 riastrad
512 1.1 riastrad static void filt_drm_detach(struct knote *);
513 1.1 riastrad static int filt_drm_event(struct knote *, long);
514 1.1 riastrad
515 1.1 riastrad static const struct filterops drm_filtops = {
516 1.1 riastrad .f_isfd = 1,
517 1.1 riastrad .f_attach = NULL,
518 1.1 riastrad .f_detach = filt_drm_detach,
519 1.1 riastrad .f_event = filt_drm_event,
520 1.1 riastrad };
521 1.1 riastrad
522 1.1 riastrad static int
523 1.1 riastrad drm_kqfilter(struct file *fp, struct knote *kn)
524 1.1 riastrad {
525 1.1 riastrad struct drm_file *const file = fp->f_data;
526 1.1 riastrad struct drm_device *const dev = file->minor->dev;
527 1.1 riastrad unsigned long irqflags;
528 1.1 riastrad
529 1.1 riastrad switch (kn->kn_filter) {
530 1.1 riastrad case EVFILT_READ:
531 1.1 riastrad kn->kn_fop = &drm_filtops;
532 1.1 riastrad kn->kn_hook = file;
533 1.1 riastrad spin_lock_irqsave(&dev->event_lock, irqflags);
534 1.1 riastrad SLIST_INSERT_HEAD(&file->event_selq.sel_klist, kn, kn_selnext);
535 1.1 riastrad spin_unlock_irqrestore(&dev->event_lock, irqflags);
536 1.1 riastrad return 0;
537 1.1 riastrad case EVFILT_WRITE:
538 1.1 riastrad default:
539 1.1 riastrad return EINVAL;
540 1.1 riastrad }
541 1.1 riastrad }
542 1.1 riastrad
543 1.1 riastrad static void
544 1.1 riastrad filt_drm_detach(struct knote *kn)
545 1.1 riastrad {
546 1.1 riastrad struct drm_file *const file = kn->kn_hook;
547 1.1 riastrad struct drm_device *const dev = file->minor->dev;
548 1.1 riastrad unsigned long irqflags;
549 1.1 riastrad
550 1.1 riastrad spin_lock_irqsave(&dev->event_lock, irqflags);
551 1.1 riastrad SLIST_REMOVE(&file->event_selq.sel_klist, kn, knote, kn_selnext);
552 1.1 riastrad spin_unlock_irqrestore(&dev->event_lock, irqflags);
553 1.1 riastrad }
554 1.1 riastrad
555 1.1 riastrad static int
556 1.1 riastrad filt_drm_event(struct knote *kn, long hint)
557 1.1 riastrad {
558 1.1 riastrad struct drm_file *const file = kn->kn_hook;
559 1.1 riastrad struct drm_device *const dev = file->minor->dev;
560 1.1 riastrad unsigned long irqflags;
561 1.1 riastrad int ret;
562 1.1 riastrad
563 1.1 riastrad if (hint == NOTE_SUBMIT)
564 1.1 riastrad KASSERT(spin_is_locked(&dev->event_lock));
565 1.1 riastrad else
566 1.1 riastrad spin_lock_irqsave(&dev->event_lock, irqflags);
567 1.1 riastrad if (list_empty(&file->event_list)) {
568 1.1 riastrad ret = 0;
569 1.1 riastrad } else {
570 1.1 riastrad struct drm_pending_event *const event =
571 1.1 riastrad list_first_entry(&file->event_list,
572 1.1 riastrad struct drm_pending_event, link);
573 1.1 riastrad kn->kn_data = event->event->length;
574 1.1 riastrad ret = 1;
575 1.1 riastrad }
576 1.1 riastrad if (hint == NOTE_SUBMIT)
577 1.1 riastrad KASSERT(spin_is_locked(&dev->event_lock));
578 1.1 riastrad else
579 1.1 riastrad spin_unlock_irqrestore(&dev->event_lock, irqflags);
580 1.1 riastrad
581 1.1 riastrad return ret;
582 1.1 riastrad }
583 1.1 riastrad
584 1.1 riastrad static int
585 1.1 riastrad drm_stat(struct file *fp, struct stat *st)
586 1.1 riastrad {
587 1.1 riastrad struct drm_file *const file = fp->f_data;
588 1.1 riastrad struct drm_minor *const dminor = file->minor;
589 1.1 riastrad const dev_t devno = makedev(cdevsw_lookup_major(&drm_cdevsw),
590 1.1 riastrad 64*dminor->type + dminor->index);
591 1.1 riastrad
592 1.1 riastrad (void)memset(st, 0, sizeof(*st));
593 1.1 riastrad
594 1.1 riastrad st->st_dev = devno;
595 1.1 riastrad st->st_ino = 0; /* XXX (dev,ino) uniqueness bleh */
596 1.1 riastrad st->st_uid = kauth_cred_geteuid(fp->f_cred);
597 1.1 riastrad st->st_gid = kauth_cred_getegid(fp->f_cred);
598 1.1 riastrad st->st_mode = S_IFCHR; /* XXX what? */
599 1.1 riastrad st->st_rdev = devno;
600 1.1 riastrad /* XXX what else? */
601 1.1 riastrad
602 1.1 riastrad return 0;
603 1.1 riastrad }
604 1.1 riastrad
605 1.1 riastrad static int
606 1.1 riastrad drm_ioctl(struct file *fp, unsigned long cmd, void *data)
607 1.1 riastrad {
608 1.1 riastrad struct drm_file *const file = fp->f_data;
609 1.1 riastrad const unsigned int nr = DRM_IOCTL_NR(cmd);
610 1.1 riastrad int error;
611 1.1 riastrad
612 1.1 riastrad switch (cmd) {
613 1.1 riastrad case FIONBIO:
614 1.1 riastrad case FIOASYNC:
615 1.1 riastrad return 0;
616 1.1 riastrad
617 1.1 riastrad #if 0 /* XXX why? */
618 1.1 riastrad case SIOCSPGRP:
619 1.1 riastrad case TIOCSPGRP:
620 1.1 riastrad case FIOSETOWN:
621 1.1 riastrad return fsetown(&dev->buf_pgid, cmd, data);
622 1.1 riastrad
623 1.1 riastrad case SIOCGPGRP:
624 1.1 riastrad case TIOCGPGRP:
625 1.1 riastrad case FIOGETOWN:
626 1.1 riastrad return fgetown(&dev->buf_pgid, cmd, data);
627 1.1 riastrad #endif
628 1.1 riastrad
629 1.1 riastrad default:
630 1.1 riastrad break;
631 1.1 riastrad }
632 1.1 riastrad
633 1.1 riastrad if (IOCGROUP(cmd) != DRM_IOCTL_BASE)
634 1.1 riastrad return EINVAL;
635 1.1 riastrad
636 1.1 riastrad KASSERT(file != NULL);
637 1.1 riastrad KASSERT(file->minor != NULL);
638 1.1 riastrad KASSERT(file->minor->dev != NULL);
639 1.1 riastrad struct drm_device *const dev = file->minor->dev;
640 1.1 riastrad const struct drm_ioctl_desc *ioctl;
641 1.1 riastrad
642 1.1 riastrad if ((DRM_COMMAND_BASE <= nr) && (nr < DRM_COMMAND_END)) {
643 1.1 riastrad const unsigned int driver_nr = nr - DRM_COMMAND_BASE;
644 1.1 riastrad if (driver_nr >= dev->driver->num_ioctls)
645 1.1 riastrad return EINVAL;
646 1.1 riastrad ioctl = &dev->driver->ioctls[driver_nr];
647 1.1 riastrad } else if (nr < __arraycount(drm_ioctls)) {
648 1.1 riastrad ioctl = &drm_ioctls[nr];
649 1.1 riastrad } else {
650 1.1 riastrad ioctl = NULL;
651 1.1 riastrad }
652 1.1 riastrad
653 1.1 riastrad if ((ioctl == NULL) || (ioctl->func == NULL))
654 1.1 riastrad return EINVAL;
655 1.1 riastrad
656 1.1 riastrad /* XXX Synchronize with drm_ioctl_permit in upstream drm_drv.c. */
657 1.1 riastrad if (ISSET(ioctl->flags, DRM_ROOT_ONLY) && !DRM_SUSER())
658 1.1 riastrad return EACCES;
659 1.1 riastrad
660 1.1 riastrad if (ISSET(ioctl->flags, DRM_AUTH) &&
661 1.1 riastrad (file->minor->type != DRM_MINOR_RENDER) &&
662 1.1 riastrad !file->authenticated)
663 1.1 riastrad return EACCES;
664 1.1 riastrad
665 1.1 riastrad if (ISSET(ioctl->flags, DRM_MASTER) &&
666 1.1 riastrad (file->master == NULL) &&
667 1.1 riastrad (file->minor->type != DRM_MINOR_CONTROL))
668 1.1 riastrad return EACCES;
669 1.1 riastrad
670 1.1 riastrad if (!ISSET(ioctl->flags, DRM_CONTROL_ALLOW) &&
671 1.1 riastrad (file->minor->type == DRM_MINOR_CONTROL))
672 1.1 riastrad return EACCES;
673 1.1 riastrad
674 1.1 riastrad if (!ISSET(ioctl->flags, DRM_RENDER_ALLOW) &&
675 1.1 riastrad (file->minor->type == DRM_MINOR_RENDER))
676 1.1 riastrad return EACCES;
677 1.1 riastrad
678 1.1 riastrad if (!ISSET(ioctl->flags, DRM_UNLOCKED))
679 1.1 riastrad mutex_lock(&drm_global_mutex);
680 1.1 riastrad
681 1.1 riastrad /* XXX errno Linux->NetBSD */
682 1.1 riastrad error = -(*ioctl->func)(dev, data, file);
683 1.1 riastrad
684 1.1 riastrad if (!ISSET(ioctl->flags, DRM_UNLOCKED))
685 1.1 riastrad mutex_unlock(&drm_global_mutex);
686 1.1 riastrad
687 1.1 riastrad return error;
688 1.1 riastrad }
689 1.1 riastrad
690 1.1 riastrad static int
691 1.1 riastrad drm_fop_mmap(struct file *fp, off_t *offp, size_t len, int prot, int *flagsp,
692 1.1 riastrad int *advicep, struct uvm_object **uobjp, int *maxprotp)
693 1.1 riastrad {
694 1.1 riastrad struct drm_file *const file = fp->f_data;
695 1.1 riastrad struct drm_device *const dev = file->minor->dev;
696 1.1 riastrad int error;
697 1.1 riastrad
698 1.1 riastrad KASSERT(fp == file->filp);
699 1.1 riastrad error = (*dev->driver->mmap_object)(dev, *offp, len, prot, uobjp,
700 1.1 riastrad offp, file->filp);
701 1.1 riastrad *maxprotp = prot;
702 1.1 riastrad *advicep = UVM_ADV_RANDOM;
703 1.1 riastrad return -error;
704 1.1 riastrad }
705 1.1 riastrad
706 1.1 riastrad static int
707 1.1 riastrad drm_version_string(char *target, size_t *lenp, const char *source)
708 1.1 riastrad {
709 1.1 riastrad const size_t len = strlen(source);
710 1.1 riastrad const size_t trunc_len = MIN(len, *lenp);
711 1.1 riastrad
712 1.1 riastrad *lenp = len;
713 1.1 riastrad if ((trunc_len > 0) && (target != NULL))
714 1.1 riastrad /* copyoutstr takes a buffer size, not a string length. */
715 1.1 riastrad /* XXX errno NetBSD->Linux */
716 1.1 riastrad return -copyoutstr(source, target, trunc_len+1, NULL);
717 1.1 riastrad
718 1.1 riastrad return 0;
719 1.1 riastrad }
720 1.1 riastrad
721 1.1 riastrad static int
722 1.1 riastrad drm_version(struct drm_device *dev, void *data, struct drm_file *file)
723 1.1 riastrad {
724 1.1 riastrad struct drm_version *v = data;
725 1.1 riastrad int error;
726 1.1 riastrad
727 1.1 riastrad v->version_major = dev->driver->major;
728 1.1 riastrad v->version_minor = dev->driver->minor;
729 1.1 riastrad v->version_patchlevel = dev->driver->patchlevel;
730 1.1 riastrad
731 1.1 riastrad error = drm_version_string(v->name, &v->name_len, dev->driver->name);
732 1.1 riastrad if (error)
733 1.1 riastrad goto out;
734 1.1 riastrad error = drm_version_string(v->date, &v->date_len, dev->driver->date);
735 1.1 riastrad if (error)
736 1.1 riastrad goto out;
737 1.1 riastrad error = drm_version_string(v->desc, &v->desc_len, dev->driver->desc);
738 1.1 riastrad if (error)
739 1.1 riastrad goto out;
740 1.1 riastrad
741 1.1 riastrad out: return error;
742 1.1 riastrad }
743 1.1 riastrad
744 1.1 riastrad static paddr_t
745 1.1 riastrad drm_mmap(dev_t d, off_t offset, int prot)
746 1.1 riastrad {
747 1.1 riastrad struct drm_minor *dminor;
748 1.1 riastrad paddr_t paddr;
749 1.1 riastrad
750 1.1 riastrad dminor = drm_minor_acquire(minor(d));
751 1.1 riastrad if (IS_ERR(dminor))
752 1.1 riastrad return (paddr_t)-1;
753 1.1 riastrad
754 1.1 riastrad paddr = drm_mmap_paddr(dminor->dev, offset, prot);
755 1.1 riastrad
756 1.1 riastrad drm_minor_release(dminor);
757 1.1 riastrad return paddr;
758 1.1 riastrad }
759 1.1 riastrad
760 1.1 riastrad static const struct drm_agp_hooks *volatile drm_current_agp_hooks;
761 1.1 riastrad
762 1.1 riastrad int
763 1.1 riastrad drm_agp_register(const struct drm_agp_hooks *hooks)
764 1.1 riastrad {
765 1.1 riastrad
766 1.1 riastrad membar_producer();
767 1.1 riastrad if (atomic_cas_ptr(&drm_current_agp_hooks, NULL, __UNCONST(hooks))
768 1.1 riastrad != NULL)
769 1.1 riastrad return EBUSY;
770 1.1 riastrad
771 1.1 riastrad return 0;
772 1.1 riastrad }
773 1.1 riastrad
774 1.1 riastrad void
775 1.1 riastrad drm_agp_deregister(const struct drm_agp_hooks *hooks)
776 1.1 riastrad {
777 1.1 riastrad
778 1.1 riastrad if (atomic_cas_ptr(&drm_current_agp_hooks, __UNCONST(hooks), NULL)
779 1.1 riastrad != hooks)
780 1.1 riastrad panic("%s: wrong hooks: %p != %p", __func__,
781 1.1 riastrad hooks, drm_current_agp_hooks);
782 1.1 riastrad }
783 1.1 riastrad
784 1.1 riastrad static void __dead
785 1.1 riastrad drm_noagp_panic(struct drm_device *dev)
786 1.1 riastrad {
787 1.1 riastrad if ((dev != NULL) &&
788 1.1 riastrad (dev->control != NULL) &&
789 1.1 riastrad (dev->control->kdev != NULL))
790 1.1 riastrad panic("%s: no agp loaded", device_xname(dev->control->kdev));
791 1.1 riastrad else
792 1.1 riastrad panic("drm_device %p: no agp loaded", dev);
793 1.1 riastrad }
794 1.1 riastrad
795 1.1 riastrad int
796 1.1 riastrad drm_agp_release_hook(struct drm_device *dev)
797 1.1 riastrad {
798 1.1 riastrad const struct drm_agp_hooks *const hooks = drm_current_agp_hooks;
799 1.1 riastrad
800 1.1 riastrad if (hooks == NULL)
801 1.1 riastrad drm_noagp_panic(dev);
802 1.1 riastrad membar_consumer();
803 1.1 riastrad return (*hooks->agph_release)(dev);
804 1.1 riastrad }
805 1.1 riastrad
806 1.1 riastrad void
807 1.1 riastrad drm_agp_clear_hook(struct drm_device *dev)
808 1.1 riastrad {
809 1.1 riastrad const struct drm_agp_hooks *const hooks = drm_current_agp_hooks;
810 1.1 riastrad
811 1.1 riastrad if (hooks == NULL)
812 1.1 riastrad drm_noagp_panic(dev);
813 1.1 riastrad membar_consumer();
814 1.1 riastrad (*hooks->agph_clear)(dev);
815 1.1 riastrad }
816 1.1 riastrad
817 1.1 riastrad #if __OS_HAS_AGP
818 1.1 riastrad
819 1.1 riastrad #define DEFINE_AGP_HOOK_IOCTL(NAME, HOOK) \
820 1.1 riastrad static int \
821 1.1 riastrad NAME(struct drm_device *dev, void *data, struct drm_file *file) \
822 1.1 riastrad { \
823 1.1 riastrad const struct drm_agp_hooks *const hooks = drm_current_agp_hooks; \
824 1.1 riastrad \
825 1.1 riastrad if (hooks == NULL) \
826 1.1 riastrad return -ENODEV; \
827 1.1 riastrad membar_consumer(); \
828 1.1 riastrad return (*hooks->HOOK)(dev, data, file); \
829 1.1 riastrad }
830 1.1 riastrad
831 1.1 riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_acquire_hook_ioctl, agph_acquire_ioctl)
832 1.1 riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_release_hook_ioctl, agph_release_ioctl)
833 1.1 riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_enable_hook_ioctl, agph_enable_ioctl)
834 1.1 riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_info_hook_ioctl, agph_info_ioctl)
835 1.1 riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_alloc_hook_ioctl, agph_alloc_ioctl)
836 1.1 riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_free_hook_ioctl, agph_free_ioctl)
837 1.1 riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_bind_hook_ioctl, agph_bind_ioctl)
838 1.1 riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_unbind_hook_ioctl, agph_unbind_ioctl)
839 1.1 riastrad
840 1.1 riastrad #endif
841