1 1.8 riastrad /* $NetBSD: nouveau_usif.c,v 1.8 2021/12/18 23:45:32 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright 2014 Red Hat Inc. 5 1.1 riastrad * 6 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 7 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 8 1.1 riastrad * to deal in the Software without restriction, including without limitation 9 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 11 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 12 1.1 riastrad * 13 1.1 riastrad * The above copyright notice and this permission notice shall be included in 14 1.1 riastrad * all copies or substantial portions of the Software. 15 1.1 riastrad * 16 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 1.1 riastrad * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE. 23 1.1 riastrad * 24 1.1 riastrad * Authors: Ben Skeggs <bskeggs (at) redhat.com> 25 1.1 riastrad */ 26 1.1 riastrad 27 1.1 riastrad #include <sys/cdefs.h> 28 1.8 riastrad __KERNEL_RCSID(0, "$NetBSD: nouveau_usif.c,v 1.8 2021/12/18 23:45:32 riastradh Exp $"); 29 1.1 riastrad 30 1.8 riastrad #include "nouveau_drv.h" 31 1.1 riastrad #include "nouveau_usif.h" 32 1.1 riastrad #include "nouveau_abi16.h" 33 1.1 riastrad 34 1.1 riastrad #include <nvif/notify.h> 35 1.1 riastrad #include <nvif/unpack.h> 36 1.1 riastrad #include <nvif/client.h> 37 1.1 riastrad #include <nvif/event.h> 38 1.1 riastrad #include <nvif/ioctl.h> 39 1.1 riastrad 40 1.1 riastrad struct usif_notify_p { 41 1.1 riastrad struct drm_pending_event base; 42 1.1 riastrad struct { 43 1.1 riastrad struct drm_event base; 44 1.1 riastrad u8 data[]; 45 1.1 riastrad } e; 46 1.1 riastrad }; 47 1.1 riastrad 48 1.1 riastrad struct usif_notify { 49 1.1 riastrad struct list_head head; 50 1.1 riastrad atomic_t enabled; 51 1.1 riastrad u32 handle; 52 1.1 riastrad u16 reply; 53 1.1 riastrad u8 route; 54 1.1 riastrad u64 token; 55 1.1 riastrad struct usif_notify_p *p; 56 1.1 riastrad }; 57 1.1 riastrad 58 1.1 riastrad static inline struct usif_notify * 59 1.1 riastrad usif_notify_find(struct drm_file *filp, u32 handle) 60 1.1 riastrad { 61 1.1 riastrad struct nouveau_cli *cli = nouveau_cli(filp); 62 1.1 riastrad struct usif_notify *ntfy; 63 1.1 riastrad list_for_each_entry(ntfy, &cli->notifys, head) { 64 1.1 riastrad if (ntfy->handle == handle) 65 1.1 riastrad return ntfy; 66 1.1 riastrad } 67 1.1 riastrad return NULL; 68 1.1 riastrad } 69 1.1 riastrad 70 1.1 riastrad static inline void 71 1.1 riastrad usif_notify_dtor(struct usif_notify *ntfy) 72 1.1 riastrad { 73 1.1 riastrad list_del(&ntfy->head); 74 1.1 riastrad kfree(ntfy); 75 1.1 riastrad } 76 1.1 riastrad 77 1.1 riastrad int 78 1.1 riastrad usif_notify(const void *header, u32 length, const void *data, u32 size) 79 1.1 riastrad { 80 1.1 riastrad struct usif_notify *ntfy = NULL; 81 1.1 riastrad const union { 82 1.1 riastrad struct nvif_notify_rep_v0 v0; 83 1.1 riastrad } *rep = header; 84 1.1 riastrad struct drm_device *dev; 85 1.1 riastrad struct drm_file *filp; 86 1.1 riastrad unsigned long flags; 87 1.1 riastrad 88 1.1 riastrad if (length == sizeof(rep->v0) && rep->v0.version == 0) { 89 1.1 riastrad if (WARN_ON(!(ntfy = (void *)(unsigned long)rep->v0.token))) 90 1.1 riastrad return NVIF_NOTIFY_DROP; 91 1.1 riastrad BUG_ON(rep->v0.route != NVDRM_NOTIFY_USIF); 92 1.1 riastrad } else 93 1.1 riastrad if (WARN_ON(1)) 94 1.1 riastrad return NVIF_NOTIFY_DROP; 95 1.1 riastrad 96 1.1 riastrad if (WARN_ON(!ntfy->p || ntfy->reply != (length + size))) 97 1.1 riastrad return NVIF_NOTIFY_DROP; 98 1.1 riastrad filp = ntfy->p->base.file_priv; 99 1.1 riastrad dev = filp->minor->dev; 100 1.1 riastrad 101 1.1 riastrad memcpy(&ntfy->p->e.data[0], header, length); 102 1.1 riastrad memcpy(&ntfy->p->e.data[length], data, size); 103 1.1 riastrad switch (rep->v0.version) { 104 1.1 riastrad case 0: { 105 1.1 riastrad struct nvif_notify_rep_v0 *rep = (void *)ntfy->p->e.data; 106 1.1 riastrad rep->route = ntfy->route; 107 1.1 riastrad rep->token = ntfy->token; 108 1.1 riastrad } 109 1.1 riastrad break; 110 1.1 riastrad default: 111 1.8 riastrad BUG(); 112 1.1 riastrad break; 113 1.1 riastrad } 114 1.1 riastrad 115 1.1 riastrad spin_lock_irqsave(&dev->event_lock, flags); 116 1.1 riastrad if (!WARN_ON(filp->event_space < ntfy->p->e.base.length)) { 117 1.1 riastrad list_add_tail(&ntfy->p->base.link, &filp->event_list); 118 1.1 riastrad filp->event_space -= ntfy->p->e.base.length; 119 1.1 riastrad } 120 1.5 riastrad #ifdef __NetBSD__ 121 1.5 riastrad DRM_SPIN_WAKEUP_ONE(&filp->event_wait, &dev->event_lock); 122 1.5 riastrad #else 123 1.1 riastrad wake_up_interruptible(&filp->event_wait); 124 1.5 riastrad #endif 125 1.1 riastrad spin_unlock_irqrestore(&dev->event_lock, flags); 126 1.1 riastrad atomic_set(&ntfy->enabled, 0); 127 1.1 riastrad return NVIF_NOTIFY_DROP; 128 1.1 riastrad } 129 1.1 riastrad 130 1.1 riastrad static int 131 1.1 riastrad usif_notify_new(struct drm_file *f, void *data, u32 size, void *argv, u32 argc) 132 1.1 riastrad { 133 1.1 riastrad struct nouveau_cli *cli = nouveau_cli(f); 134 1.1 riastrad struct nvif_client *client = &cli->base; 135 1.1 riastrad union { 136 1.1 riastrad struct nvif_ioctl_ntfy_new_v0 v0; 137 1.1 riastrad } *args = data; 138 1.1 riastrad union { 139 1.1 riastrad struct nvif_notify_req_v0 v0; 140 1.1 riastrad } *req; 141 1.1 riastrad struct usif_notify *ntfy; 142 1.8 riastrad int ret = -ENOSYS; 143 1.1 riastrad 144 1.8 riastrad if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) { 145 1.1 riastrad if (usif_notify_find(f, args->v0.index)) 146 1.1 riastrad return -EEXIST; 147 1.1 riastrad } else 148 1.1 riastrad return ret; 149 1.1 riastrad req = data; 150 1.8 riastrad ret = -ENOSYS; 151 1.1 riastrad 152 1.1 riastrad if (!(ntfy = kmalloc(sizeof(*ntfy), GFP_KERNEL))) 153 1.1 riastrad return -ENOMEM; 154 1.1 riastrad atomic_set(&ntfy->enabled, 0); 155 1.1 riastrad 156 1.8 riastrad if (!(ret = nvif_unpack(ret, &data, &size, req->v0, 0, 0, true))) { 157 1.1 riastrad ntfy->reply = sizeof(struct nvif_notify_rep_v0) + req->v0.reply; 158 1.1 riastrad ntfy->route = req->v0.route; 159 1.1 riastrad ntfy->token = req->v0.token; 160 1.1 riastrad req->v0.route = NVDRM_NOTIFY_USIF; 161 1.1 riastrad req->v0.token = (unsigned long)(void *)ntfy; 162 1.1 riastrad ret = nvif_client_ioctl(client, argv, argc); 163 1.1 riastrad req->v0.token = ntfy->token; 164 1.1 riastrad req->v0.route = ntfy->route; 165 1.1 riastrad ntfy->handle = args->v0.index; 166 1.1 riastrad } 167 1.1 riastrad 168 1.1 riastrad if (ret == 0) 169 1.1 riastrad list_add(&ntfy->head, &cli->notifys); 170 1.1 riastrad if (ret) 171 1.1 riastrad kfree(ntfy); 172 1.1 riastrad return ret; 173 1.1 riastrad } 174 1.1 riastrad 175 1.1 riastrad static int 176 1.1 riastrad usif_notify_del(struct drm_file *f, void *data, u32 size, void *argv, u32 argc) 177 1.1 riastrad { 178 1.1 riastrad struct nouveau_cli *cli = nouveau_cli(f); 179 1.1 riastrad struct nvif_client *client = &cli->base; 180 1.1 riastrad union { 181 1.1 riastrad struct nvif_ioctl_ntfy_del_v0 v0; 182 1.1 riastrad } *args = data; 183 1.1 riastrad struct usif_notify *ntfy; 184 1.8 riastrad int ret = -ENOSYS; 185 1.1 riastrad 186 1.8 riastrad if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) { 187 1.1 riastrad if (!(ntfy = usif_notify_find(f, args->v0.index))) 188 1.1 riastrad return -ENOENT; 189 1.1 riastrad } else 190 1.1 riastrad return ret; 191 1.1 riastrad 192 1.1 riastrad ret = nvif_client_ioctl(client, argv, argc); 193 1.1 riastrad if (ret == 0) 194 1.1 riastrad usif_notify_dtor(ntfy); 195 1.1 riastrad return ret; 196 1.1 riastrad } 197 1.1 riastrad 198 1.1 riastrad static int 199 1.1 riastrad usif_notify_get(struct drm_file *f, void *data, u32 size, void *argv, u32 argc) 200 1.1 riastrad { 201 1.1 riastrad struct nouveau_cli *cli = nouveau_cli(f); 202 1.1 riastrad struct nvif_client *client = &cli->base; 203 1.1 riastrad union { 204 1.1 riastrad struct nvif_ioctl_ntfy_del_v0 v0; 205 1.1 riastrad } *args = data; 206 1.1 riastrad struct usif_notify *ntfy; 207 1.8 riastrad int ret = -ENOSYS; 208 1.1 riastrad 209 1.8 riastrad if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) { 210 1.1 riastrad if (!(ntfy = usif_notify_find(f, args->v0.index))) 211 1.1 riastrad return -ENOENT; 212 1.1 riastrad } else 213 1.1 riastrad return ret; 214 1.1 riastrad 215 1.1 riastrad if (atomic_xchg(&ntfy->enabled, 1)) 216 1.1 riastrad return 0; 217 1.1 riastrad 218 1.1 riastrad ntfy->p = kmalloc(sizeof(*ntfy->p) + ntfy->reply, GFP_KERNEL); 219 1.1 riastrad if (ret = -ENOMEM, !ntfy->p) 220 1.1 riastrad goto done; 221 1.1 riastrad ntfy->p->base.event = &ntfy->p->e.base; 222 1.1 riastrad ntfy->p->base.file_priv = f; 223 1.1 riastrad ntfy->p->e.base.type = DRM_NOUVEAU_EVENT_NVIF; 224 1.1 riastrad ntfy->p->e.base.length = sizeof(ntfy->p->e.base) + ntfy->reply; 225 1.1 riastrad 226 1.1 riastrad ret = nvif_client_ioctl(client, argv, argc); 227 1.1 riastrad done: 228 1.1 riastrad if (ret) { 229 1.1 riastrad atomic_set(&ntfy->enabled, 0); 230 1.1 riastrad kfree(ntfy->p); 231 1.1 riastrad } 232 1.1 riastrad return ret; 233 1.1 riastrad } 234 1.1 riastrad 235 1.1 riastrad static int 236 1.1 riastrad usif_notify_put(struct drm_file *f, void *data, u32 size, void *argv, u32 argc) 237 1.1 riastrad { 238 1.1 riastrad struct nouveau_cli *cli = nouveau_cli(f); 239 1.1 riastrad struct nvif_client *client = &cli->base; 240 1.1 riastrad union { 241 1.1 riastrad struct nvif_ioctl_ntfy_put_v0 v0; 242 1.1 riastrad } *args = data; 243 1.1 riastrad struct usif_notify *ntfy; 244 1.8 riastrad int ret = -ENOSYS; 245 1.1 riastrad 246 1.8 riastrad if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) { 247 1.1 riastrad if (!(ntfy = usif_notify_find(f, args->v0.index))) 248 1.1 riastrad return -ENOENT; 249 1.1 riastrad } else 250 1.1 riastrad return ret; 251 1.1 riastrad 252 1.1 riastrad ret = nvif_client_ioctl(client, argv, argc); 253 1.1 riastrad if (ret == 0 && atomic_xchg(&ntfy->enabled, 0)) 254 1.1 riastrad kfree(ntfy->p); 255 1.1 riastrad return ret; 256 1.1 riastrad } 257 1.1 riastrad 258 1.1 riastrad struct usif_object { 259 1.1 riastrad struct list_head head; 260 1.1 riastrad struct list_head ntfy; 261 1.1 riastrad u8 route; 262 1.1 riastrad u64 token; 263 1.1 riastrad }; 264 1.1 riastrad 265 1.1 riastrad static void 266 1.1 riastrad usif_object_dtor(struct usif_object *object) 267 1.1 riastrad { 268 1.1 riastrad list_del(&object->head); 269 1.1 riastrad kfree(object); 270 1.1 riastrad } 271 1.1 riastrad 272 1.1 riastrad static int 273 1.1 riastrad usif_object_new(struct drm_file *f, void *data, u32 size, void *argv, u32 argc) 274 1.1 riastrad { 275 1.1 riastrad struct nouveau_cli *cli = nouveau_cli(f); 276 1.1 riastrad struct nvif_client *client = &cli->base; 277 1.1 riastrad union { 278 1.1 riastrad struct nvif_ioctl_new_v0 v0; 279 1.1 riastrad } *args = data; 280 1.1 riastrad struct usif_object *object; 281 1.8 riastrad int ret = -ENOSYS; 282 1.1 riastrad 283 1.1 riastrad if (!(object = kmalloc(sizeof(*object), GFP_KERNEL))) 284 1.1 riastrad return -ENOMEM; 285 1.1 riastrad list_add(&object->head, &cli->objects); 286 1.1 riastrad 287 1.8 riastrad if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) { 288 1.1 riastrad object->route = args->v0.route; 289 1.1 riastrad object->token = args->v0.token; 290 1.1 riastrad args->v0.route = NVDRM_OBJECT_USIF; 291 1.1 riastrad args->v0.token = (unsigned long)(void *)object; 292 1.1 riastrad ret = nvif_client_ioctl(client, argv, argc); 293 1.1 riastrad args->v0.token = object->token; 294 1.1 riastrad args->v0.route = object->route; 295 1.1 riastrad } 296 1.1 riastrad 297 1.1 riastrad if (ret) 298 1.1 riastrad usif_object_dtor(object); 299 1.1 riastrad return ret; 300 1.1 riastrad } 301 1.1 riastrad 302 1.1 riastrad int 303 1.6 maya #ifdef __NetBSD__ 304 1.6 maya usif_ioctl(struct drm_file *filp, void *data, u32 argc) 305 1.6 maya #else 306 1.1 riastrad usif_ioctl(struct drm_file *filp, void __user *user, u32 argc) 307 1.6 maya #endif 308 1.1 riastrad { 309 1.1 riastrad struct nouveau_cli *cli = nouveau_cli(filp); 310 1.1 riastrad struct nvif_client *client = &cli->base; 311 1.6 maya #ifndef __NetBSD__ 312 1.1 riastrad void *data = kmalloc(argc, GFP_KERNEL); 313 1.6 maya #endif 314 1.1 riastrad u32 size = argc; 315 1.1 riastrad union { 316 1.1 riastrad struct nvif_ioctl_v0 v0; 317 1.1 riastrad } *argv = data; 318 1.1 riastrad struct usif_object *object; 319 1.1 riastrad u8 owner; 320 1.1 riastrad int ret; 321 1.1 riastrad 322 1.6 maya #ifndef __NetBSD__ 323 1.1 riastrad if (ret = -ENOMEM, !argv) 324 1.1 riastrad goto done; 325 1.1 riastrad if (ret = -EFAULT, copy_from_user(argv, user, size)) 326 1.1 riastrad goto done; 327 1.6 maya #endif 328 1.1 riastrad 329 1.8 riastrad if (!(ret = nvif_unpack(-ENOSYS, &data, &size, argv->v0, 0, 0, true))) { 330 1.1 riastrad /* block access to objects not created via this interface */ 331 1.1 riastrad owner = argv->v0.owner; 332 1.1 riastrad if (argv->v0.object == 0ULL && 333 1.1 riastrad argv->v0.type != NVIF_IOCTL_V0_DEL) 334 1.1 riastrad argv->v0.owner = NVDRM_OBJECT_ANY; /* except client */ 335 1.1 riastrad else 336 1.1 riastrad argv->v0.owner = NVDRM_OBJECT_USIF; 337 1.1 riastrad } else 338 1.1 riastrad goto done; 339 1.1 riastrad 340 1.1 riastrad /* USIF slightly abuses some return-only ioctl members in order 341 1.1 riastrad * to provide interoperability with the older ABI16 objects 342 1.1 riastrad */ 343 1.1 riastrad mutex_lock(&cli->mutex); 344 1.1 riastrad if (argv->v0.route) { 345 1.1 riastrad if (ret = -EINVAL, argv->v0.route == 0xff) 346 1.1 riastrad ret = nouveau_abi16_usif(filp, argv, argc); 347 1.1 riastrad if (ret) { 348 1.1 riastrad mutex_unlock(&cli->mutex); 349 1.1 riastrad goto done; 350 1.1 riastrad } 351 1.1 riastrad } 352 1.1 riastrad 353 1.1 riastrad switch (argv->v0.type) { 354 1.1 riastrad case NVIF_IOCTL_V0_NEW: 355 1.1 riastrad ret = usif_object_new(filp, data, size, argv, argc); 356 1.1 riastrad break; 357 1.1 riastrad case NVIF_IOCTL_V0_NTFY_NEW: 358 1.1 riastrad ret = usif_notify_new(filp, data, size, argv, argc); 359 1.1 riastrad break; 360 1.1 riastrad case NVIF_IOCTL_V0_NTFY_DEL: 361 1.1 riastrad ret = usif_notify_del(filp, data, size, argv, argc); 362 1.1 riastrad break; 363 1.1 riastrad case NVIF_IOCTL_V0_NTFY_GET: 364 1.1 riastrad ret = usif_notify_get(filp, data, size, argv, argc); 365 1.1 riastrad break; 366 1.1 riastrad case NVIF_IOCTL_V0_NTFY_PUT: 367 1.1 riastrad ret = usif_notify_put(filp, data, size, argv, argc); 368 1.1 riastrad break; 369 1.3 riastrad case NVIF_IOCTL_V0_MAP_NETBSD: 370 1.3 riastrad /* Kernel-only kludge. */ 371 1.3 riastrad ret = -EINVAL; 372 1.7 christos break; 373 1.1 riastrad default: 374 1.1 riastrad ret = nvif_client_ioctl(client, argv, argc); 375 1.1 riastrad break; 376 1.1 riastrad } 377 1.1 riastrad if (argv->v0.route == NVDRM_OBJECT_USIF) { 378 1.1 riastrad object = (void *)(unsigned long)argv->v0.token; 379 1.1 riastrad argv->v0.route = object->route; 380 1.1 riastrad argv->v0.token = object->token; 381 1.1 riastrad if (ret == 0 && argv->v0.type == NVIF_IOCTL_V0_DEL) { 382 1.1 riastrad list_del(&object->head); 383 1.1 riastrad kfree(object); 384 1.1 riastrad } 385 1.1 riastrad } else { 386 1.1 riastrad argv->v0.route = NVIF_IOCTL_V0_ROUTE_HIDDEN; 387 1.1 riastrad argv->v0.token = 0; 388 1.1 riastrad } 389 1.1 riastrad argv->v0.owner = owner; 390 1.1 riastrad mutex_unlock(&cli->mutex); 391 1.1 riastrad 392 1.6 maya #ifndef __NetBSD__ 393 1.1 riastrad if (copy_to_user(user, argv, argc)) 394 1.1 riastrad ret = -EFAULT; 395 1.6 maya #endif 396 1.1 riastrad done: 397 1.6 maya #ifndef __NetBSD__ 398 1.1 riastrad kfree(argv); 399 1.6 maya #endif 400 1.1 riastrad return ret; 401 1.1 riastrad } 402 1.1 riastrad 403 1.1 riastrad void 404 1.1 riastrad usif_client_fini(struct nouveau_cli *cli) 405 1.1 riastrad { 406 1.1 riastrad struct usif_object *object, *otemp; 407 1.1 riastrad struct usif_notify *notify, *ntemp; 408 1.1 riastrad 409 1.1 riastrad list_for_each_entry_safe(notify, ntemp, &cli->notifys, head) { 410 1.1 riastrad usif_notify_dtor(notify); 411 1.1 riastrad } 412 1.1 riastrad 413 1.1 riastrad list_for_each_entry_safe(object, otemp, &cli->objects, head) { 414 1.1 riastrad usif_object_dtor(object); 415 1.1 riastrad } 416 1.1 riastrad } 417 1.1 riastrad 418 1.1 riastrad void 419 1.1 riastrad usif_client_init(struct nouveau_cli *cli) 420 1.1 riastrad { 421 1.1 riastrad INIT_LIST_HEAD(&cli->objects); 422 1.1 riastrad INIT_LIST_HEAD(&cli->notifys); 423 1.1 riastrad } 424