drm: Handle drm masters and minors like Linux
[dragonfly.git] / sys / dev / drm / drm_fops.c
1 /**
2  * \file drm_fops.c
3  * File operations for DRM
4  *
5  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6  * \author Daryll Strauss <daryll@valinux.com>
7  * \author Gareth Hughes <gareth@valinux.com>
8  */
9
10 /*
11  * Created: Mon Jan  4 08:58:31 1999 by faith@valinux.com
12  *
13  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
14  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
15  * All Rights Reserved.
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining a
18  * copy of this software and associated documentation files (the "Software"),
19  * to deal in the Software without restriction, including without limitation
20  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
21  * and/or sell copies of the Software, and to permit persons to whom the
22  * Software is furnished to do so, subject to the following conditions:
23  *
24  * The above copyright notice and this permission notice (including the next
25  * paragraph) shall be included in all copies or substantial portions of the
26  * Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
31  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
32  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
33  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34  * OTHER DEALINGS IN THE SOFTWARE.
35  */
36
37 #include <sys/devfs.h>
38 #include <sys/file.h>
39
40 #include <drm/drmP.h>
41 #include <linux/module.h>
42
43 static int drm_open_helper(struct cdev *kdev, int flags, int fmt, DRM_STRUCTPROC *p,
44                     struct drm_device *dev, struct file *fp);
45
46 static int drm_setup(struct drm_device *dev)
47 {
48         drm_local_map_t *map;
49         int i;
50         int ret;
51
52         /* prebuild the SAREA */
53         i = drm_addmap(dev, 0, SAREA_MAX, _DRM_SHM,
54             _DRM_CONTAINS_LOCK, &map);
55         if (i != 0)
56                 return i;
57
58         if (dev->driver->firstopen) {
59                 ret = dev->driver->firstopen(dev);
60                 if (ret != 0)
61                         return ret;
62         }
63
64         if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
65             !drm_core_check_feature(dev, DRIVER_MODESET)) {
66                 dev->buf_use = 0;
67                 atomic_set(&dev->buf_alloc, 0);
68
69                 i = drm_dma_setup(dev);
70                 if (i != 0)
71                         return i;
72         }
73
74         for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
75                 atomic_set(&dev->counts[i], 0);
76
77         dev->sigdata.lock = NULL;
78
79         dev->context_flag = 0;
80         dev->interrupt_flag = 0;
81         dev->dma_flag = 0;
82         dev->last_context = 0;
83         dev->last_switch = 0;
84         dev->last_checked = 0;
85         init_waitqueue_head(&dev->context_wait);
86         dev->if_version = 0;
87
88         dev->ctx_start = 0;
89         dev->lck_start = 0;
90
91         dev->buf_async = NULL;
92         init_waitqueue_head(&dev->buf_readers);
93         init_waitqueue_head(&dev->buf_writers);
94
95         DRM_DEBUG("\n");
96
97         /*
98          * The kernel's context could be created here, but is now created
99          * in drm_dma_enqueue.  This is more resource-efficient for
100          * hardware that does not do DMA, but may mean that
101          * drm_select_queue fails between the time the interrupt is
102          * initialized and the time the queues are initialized.
103          */
104
105         return 0;
106 }
107
108 #define DRIVER_SOFTC(unit) \
109         ((struct drm_device *)devclass_get_softc(drm_devclass, unit))
110
111 /**
112  * Open file.
113  *
114  * \param inode device inode
115  * \param filp file pointer.
116  * \return zero on success or a negative number on failure.
117  *
118  * Searches the DRM device with the same minor number, calls open_helper(), and
119  * increments the device open count. If the open count was previous at zero,
120  * i.e., it's the first that the device is open, then calls setup().
121  */
122 int drm_open(struct dev_open_args *ap)
123 {
124         struct cdev *kdev = ap->a_head.a_dev;
125         struct file *filp = ap->a_fp;
126         struct drm_device *dev = NULL;
127         int minor_id = minor(kdev);
128         struct drm_minor *minor;
129         int retcode = 0;
130         int need_setup = 0;
131 /* old dfly variables below */
132         int flags = ap->a_oflags;
133         int fmt = 0;
134         struct thread *p = curthread;
135
136         minor = idr_find(&drm_minors_idr, minor_id);
137         if (!minor)
138                 return -ENODEV;
139
140         if (!(dev = minor->dev))
141                 return -ENODEV;
142
143         if (!dev->open_count++)
144                 need_setup = 1;
145
146         retcode = drm_open_helper(kdev, flags, fmt, p, dev, filp);
147         if (retcode)
148                 goto err_undo;
149         atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
150         if (need_setup) {
151                 retcode = drm_setup(dev);
152                 if (retcode)
153                         goto err_undo;
154         }
155         return 0;
156
157 err_undo:
158         dev->open_count--;
159         return retcode;
160 }
161 EXPORT_SYMBOL(drm_open);
162
163 /**
164  * Called whenever a process opens /dev/drm.
165  *
166  * \param inode device inode.
167  * \param filp file pointer.
168  * \param dev device.
169  * \return zero on success or a negative number on failure.
170  *
171  * Creates and initializes a drm_file structure for the file private data in \p
172  * filp and add it into the double linked list in \p dev.
173  */
174 static int drm_open_helper(struct cdev *kdev, int flags, int fmt, DRM_STRUCTPROC *p,
175                     struct drm_device *dev, struct file *filp)
176 {
177         int minor_id = 0;
178         struct drm_file *priv;
179         int ret;
180
181         if (flags & O_EXCL)
182                 return EBUSY; /* No exclusive opens */
183         dev->flags = flags;
184
185         DRM_DEBUG("pid = %d, device = %s\n", DRM_CURRENTPID, devtoname(kdev));
186
187         priv = kmalloc(sizeof(*priv), M_DRM, M_WAITOK | M_NULLOK | M_ZERO);
188         if (!priv)
189                 return -ENOMEM;
190
191         filp->private_data = priv;
192         priv->filp = filp;
193         priv->uid               = p->td_proc->p_ucred->cr_svuid;
194         priv->pid               = p->td_proc->p_pid;
195         priv->minor = idr_find(&drm_minors_idr, minor_id);
196         priv->ioctl_count = 0;
197         /* for compatibility root is always authenticated */
198         priv->authenticated     = DRM_SUSER(p);
199         priv->lock_count = 0;
200
201         INIT_LIST_HEAD(&priv->lhead);
202         INIT_LIST_HEAD(&priv->fbs);
203         lockinit(&priv->fbs_lock, "dfbsl", 0, LK_CANRECURSE);
204         INIT_LIST_HEAD(&priv->event_list);
205         init_waitqueue_head(&priv->event_wait);
206         priv->event_space = 4096; /* set aside 4k for event buffer */
207
208         if (dev->driver->driver_features & DRIVER_GEM)
209                 drm_gem_open(dev, priv);
210
211         if (dev->driver->open) {
212                 ret = dev->driver->open(dev, priv);
213                 if (ret != 0)
214                         goto out_free;
215         }
216
217         /* if there is no current master make this fd it */
218         mutex_lock(&dev->struct_mutex);
219         if (!priv->minor->master) {
220                 /* create a new master */
221                 priv->minor->master = drm_master_create(priv->minor);
222                 if (!priv->minor->master) {
223                         mutex_unlock(&dev->struct_mutex);
224                         ret = -ENOMEM;
225                         goto out_free;
226                 }
227
228                 priv->is_master = 1;
229                 /* take another reference for the copy in the local file priv */
230                 priv->master = drm_master_get(priv->minor->master);
231
232                 priv->authenticated = 1;
233
234                 mutex_unlock(&dev->struct_mutex);
235                 if (dev->driver->master_create) {
236                         ret = dev->driver->master_create(dev, priv->master);
237                         if (ret) {
238                                 mutex_lock(&dev->struct_mutex);
239                                 /* drop both references if this fails */
240                                 drm_master_put(&priv->minor->master);
241                                 drm_master_put(&priv->master);
242                                 mutex_unlock(&dev->struct_mutex);
243                                 goto out_free;
244                         }
245                 }
246                 mutex_lock(&dev->struct_mutex);
247                 if (dev->driver->master_set) {
248                         ret = dev->driver->master_set(dev, priv, true);
249                         if (ret) {
250                                 /* drop both references if this fails */
251                                 drm_master_put(&priv->minor->master);
252                                 drm_master_put(&priv->master);
253                                 mutex_unlock(&dev->struct_mutex);
254                                 goto out_free;
255                         }
256                 }
257                 mutex_unlock(&dev->struct_mutex);
258         } else {
259                 /* get a reference to the master */
260                 priv->master = drm_master_get(priv->minor->master);
261                 mutex_unlock(&dev->struct_mutex);
262         }
263
264         mutex_lock(&dev->struct_mutex);
265         list_add(&priv->lhead, &dev->filelist);
266         mutex_unlock(&dev->struct_mutex);
267
268         return 0;
269       out_free:
270         kfree(priv, M_DRM);
271         filp->private_data = NULL;
272         return ret;
273 }
274
275 /**
276  * Release file.
277  *
278  * \param inode device inode
279  * \param file_priv DRM file private.
280  * \return zero on success or a negative number on failure.
281  *
282  * If the hardware lock is held then free it, and take it again for the kernel
283  * context since it's necessary to reclaim buffers. Unlink the file private
284  * data from its list and free it. Decreases the open count and if it reaches
285  * zero calls drm_lastclose().
286  */
287
288 #if 0 /* old drm_release equivalent from DragonFly */
289 void drm_cdevpriv_dtor(void *cd)
290 {
291         struct drm_file *file_priv = cd;
292         struct drm_device *dev = file_priv->dev;
293         int retcode = 0;
294
295         DRM_DEBUG("open_count = %d\n", dev->open_count);
296
297         DRM_LOCK(dev);
298
299         if (dev->driver->preclose != NULL)
300                 dev->driver->preclose(dev, file_priv);
301
302         /* ========================================================
303          * Begin inline drm_release
304          */
305
306         DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
307             DRM_CURRENTPID, (long)dev->dev, dev->open_count);
308
309         if (dev->driver->driver_features & DRIVER_GEM)
310                 drm_gem_release(dev, file_priv);
311
312         if (dev->primary->master->lock.hw_lock
313             && _DRM_LOCK_IS_HELD(dev->primary->master->lock.hw_lock->lock)
314             && dev->primary->master->lock.file_priv == file_priv) {
315                 DRM_DEBUG("Process %d dead, freeing lock for context %d\n",
316                           DRM_CURRENTPID,
317                           _DRM_LOCKING_CONTEXT(dev->primary->master->lock.hw_lock->lock));
318                 if (dev->driver->reclaim_buffers_locked != NULL)
319                         dev->driver->reclaim_buffers_locked(dev, file_priv);
320
321                 drm_lock_free(&dev->primary->master->lock,
322                     _DRM_LOCKING_CONTEXT(dev->primary->master->lock.hw_lock->lock));
323
324                                 /* FIXME: may require heavy-handed reset of
325                                    hardware at this point, possibly
326                                    processed via a callback to the X
327                                    server. */
328         } else if (dev->driver->reclaim_buffers_locked != NULL &&
329             dev->primary->master->lock.hw_lock != NULL) {
330                 /* The lock is required to reclaim buffers */
331                 for (;;) {
332                         if (!dev->primary->master->lock.hw_lock) {
333                                 /* Device has been unregistered */
334                                 retcode = EINTR;
335                                 break;
336                         }
337                         if (drm_lock_take(&dev->primary->master->lock, DRM_KERNEL_CONTEXT)) {
338                                 dev->primary->master->lock.file_priv = file_priv;
339                                 dev->primary->master->lock.lock_time = jiffies;
340                                 atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
341                                 break;  /* Got lock */
342                         }
343                         /* Contention */
344                         retcode = DRM_LOCK_SLEEP(dev, &dev->primary->master->lock.lock_queue,
345                             PCATCH, "drmlk2", 0);
346                         if (retcode)
347                                 break;
348                 }
349                 if (retcode == 0) {
350                         dev->driver->reclaim_buffers_locked(dev, file_priv);
351                         drm_lock_free(&dev->primary->master->lock, DRM_KERNEL_CONTEXT);
352                 }
353         }
354
355         if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
356             !dev->driver->reclaim_buffers_locked)
357                 drm_reclaim_buffers(dev, file_priv);
358
359         funsetown(&dev->buf_sigio);
360
361         if (dev->driver->postclose != NULL)
362                 dev->driver->postclose(dev, file_priv);
363         list_del(&file_priv->lhead);
364
365
366         /* ========================================================
367          * End inline drm_release
368          */
369
370         atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
371         device_unbusy(dev->dev);
372         if (--dev->open_count == 0) {
373                 retcode = drm_lastclose(dev);
374         }
375
376         DRM_UNLOCK(dev);
377 }
378 #endif
379
380 static void drm_unload(struct drm_device *dev)
381 {
382         int i;
383
384         DRM_DEBUG("\n");
385
386         drm_sysctl_cleanup(dev);
387         if (dev->devnode != NULL)
388                 destroy_dev(dev->devnode);
389
390         drm_ctxbitmap_cleanup(dev);
391
392         if (dev->driver->driver_features & DRIVER_GEM)
393                 drm_gem_destroy(dev);
394
395         if (dev->agp && dev->agp->agp_mtrr) {
396                 int __unused retcode;
397
398                 retcode = drm_mtrr_del(0, dev->agp->agp_info.ai_aperture_base,
399                     dev->agp->agp_info.ai_aperture_size, DRM_MTRR_WC);
400                 DRM_DEBUG("mtrr_del = %d", retcode);
401         }
402
403         drm_vblank_cleanup(dev);
404
405         DRM_LOCK(dev);
406         drm_lastclose(dev);
407         DRM_UNLOCK(dev);
408
409         /* Clean up PCI resources allocated by drm_bufs.c.  We're not really
410          * worried about resource consumption while the DRM is inactive (between
411          * lastclose and firstopen or unload) because these aren't actually
412          * taking up KVA, just keeping the PCI resource allocated.
413          */
414         for (i = 0; i < DRM_MAX_PCI_RESOURCE; i++) {
415                 if (dev->pcir[i] == NULL)
416                         continue;
417                 bus_release_resource(dev->dev, SYS_RES_MEMORY,
418                     dev->pcirid[i], dev->pcir[i]);
419                 dev->pcir[i] = NULL;
420         }
421
422         if (dev->agp) {
423                 drm_free(dev->agp, M_DRM);
424                 dev->agp = NULL;
425         }
426
427         if (dev->driver->unload != NULL) {
428                 DRM_LOCK(dev);
429                 dev->driver->unload(dev);
430                 DRM_UNLOCK(dev);
431         }
432
433         drm_mem_uninit();
434
435         if (pci_disable_busmaster(dev->dev))
436                 DRM_ERROR("Request to disable bus-master failed.\n");
437
438         lockuninit(&dev->vbl_lock);
439         lockuninit(&dev->dev_lock);
440         lockuninit(&dev->event_lock);
441         lockuninit(&dev->struct_mutex);
442 }
443
444 int drm_release(device_t kdev)
445 {
446         struct drm_device *dev;
447
448         dev = device_get_softc(kdev);
449         drm_unload(dev);
450         if (dev->irqr) {
451                 bus_release_resource(dev->dev, SYS_RES_IRQ, dev->irqrid,
452                     dev->irqr);
453                 if (dev->msi_enabled) {
454                         pci_release_msi(dev->dev);
455                         DRM_INFO("MSI released\n");
456                 }
457         }
458         return (0);
459 }
460
461 static bool
462 drm_dequeue_event(struct drm_device *dev, struct drm_file *file_priv,
463     struct uio *uio, struct drm_pending_event **out)
464 {
465         struct drm_pending_event *e;
466
467         if (list_empty(&file_priv->event_list))
468                 return (false);
469         e = list_first_entry(&file_priv->event_list,
470             struct drm_pending_event, link);
471         if (e->event->length > uio->uio_resid)
472                 return (false);
473
474         file_priv->event_space += e->event->length;
475         list_del(&e->link);
476         *out = e;
477         return (true);
478 }
479
480 int
481 drm_read(struct dev_read_args *ap)
482 {
483         struct file *filp = ap->a_fp;
484         struct drm_file *file_priv = filp->private_data;
485         struct cdev *kdev = ap->a_head.a_dev;
486         struct uio *uio = ap->a_uio;
487         int ioflag = ap->a_ioflag;
488         struct drm_device *dev;
489         struct drm_pending_event *e;
490         int error;
491
492         dev = drm_get_device_from_kdev(kdev);
493         lockmgr(&dev->event_lock, LK_EXCLUSIVE);
494         while (list_empty(&file_priv->event_list)) {
495                 if ((ioflag & O_NONBLOCK) != 0) {
496                         error = EAGAIN;
497                         goto out;
498                 }
499                 error = lksleep(&file_priv->event_space, &dev->event_lock,
500                    PCATCH, "drmrea", 0);
501                if (error != 0)
502                        goto out;
503         }
504         while (drm_dequeue_event(dev, file_priv, uio, &e)) {
505                 lockmgr(&dev->event_lock, LK_RELEASE);
506                 error = uiomove((caddr_t)e->event, e->event->length, uio);
507                 e->destroy(e);
508                 if (error != 0)
509                         return (error);
510                 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
511         }
512 out:
513         lockmgr(&dev->event_lock, LK_RELEASE);
514         return (error);
515 }
516
517 void
518 drm_event_wakeup(struct drm_pending_event *e)
519 {
520         struct drm_file *file_priv = e->file_priv;
521         struct drm_device *dev = file_priv->minor->dev;
522
523         KKASSERT(lockstatus(&dev->event_lock, curthread) != 0);
524
525         wakeup(&file_priv->event_space);
526         KNOTE(&file_priv->dkq.ki_note, 0);
527 }
528
529 static int
530 drmfilt(struct knote *kn, long hint)
531 {
532         struct drm_file *file_priv = (struct drm_file *)kn->kn_hook;
533         struct drm_device *dev = file_priv->minor->dev;
534         int ready = 0;
535
536         lockmgr(&dev->event_lock, LK_EXCLUSIVE);
537         if (!list_empty(&file_priv->event_list))
538                 ready = 1;
539         lockmgr(&dev->event_lock, LK_RELEASE);
540
541         return (ready);
542 }
543
544 static void
545 drmfilt_detach(struct knote *kn)
546 {
547         struct drm_file *file_priv = (struct drm_file *)kn->kn_hook;
548         struct drm_device *dev = file_priv->minor->dev;
549         struct klist *klist;
550
551         lockmgr(&dev->event_lock, LK_EXCLUSIVE);
552         klist = &file_priv->dkq.ki_note;
553         knote_remove(klist, kn);
554         lockmgr(&dev->event_lock, LK_RELEASE);
555 }
556
557 static struct filterops drmfiltops =
558         { FILTEROP_ISFD, NULL, drmfilt_detach, drmfilt };
559
560 int
561 drm_kqfilter(struct dev_kqfilter_args *ap)
562 {
563         struct file *filp = ap->a_fp;
564         struct drm_file *file_priv = filp->private_data;
565         struct drm_device *dev = file_priv->minor->dev;
566         struct knote *kn = ap->a_kn;
567         struct klist *klist;
568
569         ap->a_result = 0;
570
571         switch (kn->kn_filter) {
572         case EVFILT_READ:
573                 kn->kn_fop = &drmfiltops;
574                 kn->kn_hook = (caddr_t)file_priv;
575                 break;
576         default:
577                 ap->a_result = EOPNOTSUPP;
578                 return (0);
579         }
580
581         lockmgr(&dev->event_lock, LK_EXCLUSIVE);
582         klist = &file_priv->dkq.ki_note;
583         knote_insert(klist, kn);
584         lockmgr(&dev->event_lock, LK_RELEASE);
585
586         return (0);
587 }