1592ffb21SWarner Losh /**
2592ffb21SWarner Losh * \file drm_vm.c
3592ffb21SWarner Losh * Memory mapping for DRM
4592ffb21SWarner Losh *
5592ffb21SWarner Losh * \author Rickard E. (Rik) Faith <faith@valinux.com>
6592ffb21SWarner Losh * \author Gareth Hughes <gareth@valinux.com>
7592ffb21SWarner Losh */
8592ffb21SWarner Losh
9592ffb21SWarner Losh /*
10592ffb21SWarner Losh * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
11592ffb21SWarner Losh *
12592ffb21SWarner Losh * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
13592ffb21SWarner Losh * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14592ffb21SWarner Losh * All Rights Reserved.
15592ffb21SWarner Losh *
16592ffb21SWarner Losh * Permission is hereby granted, free of charge, to any person obtaining a
17592ffb21SWarner Losh * copy of this software and associated documentation files (the "Software"),
18592ffb21SWarner Losh * to deal in the Software without restriction, including without limitation
19592ffb21SWarner Losh * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20592ffb21SWarner Losh * and/or sell copies of the Software, and to permit persons to whom the
21592ffb21SWarner Losh * Software is furnished to do so, subject to the following conditions:
22592ffb21SWarner Losh *
23592ffb21SWarner Losh * The above copyright notice and this permission notice (including the next
24592ffb21SWarner Losh * paragraph) shall be included in all copies or substantial portions of the
25592ffb21SWarner Losh * Software.
26592ffb21SWarner Losh *
27592ffb21SWarner Losh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28592ffb21SWarner Losh * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29592ffb21SWarner Losh * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30592ffb21SWarner Losh * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31592ffb21SWarner Losh * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32592ffb21SWarner Losh * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33592ffb21SWarner Losh * OTHER DEALINGS IN THE SOFTWARE.
34592ffb21SWarner Losh */
35592ffb21SWarner Losh
36592ffb21SWarner Losh #include <sys/cdefs.h>
37592ffb21SWarner Losh /** @file drm_vm.c
38*7fa5cd38SGordon Bergling * Support code for mmaping of DRM maps.
39592ffb21SWarner Losh */
40592ffb21SWarner Losh
41592ffb21SWarner Losh #include <dev/drm2/drmP.h>
42592ffb21SWarner Losh #include <dev/drm2/drm.h>
43592ffb21SWarner Losh
44592ffb21SWarner Losh #ifdef FREEBSD_NOTYET
45592ffb21SWarner Losh int
drm_mmap(struct cdev * kdev,vm_ooffset_t offset,vm_paddr_t * paddr,int prot,vm_memattr_t * memattr)46592ffb21SWarner Losh drm_mmap(struct cdev *kdev, vm_ooffset_t offset, vm_paddr_t *paddr,
47592ffb21SWarner Losh int prot, vm_memattr_t *memattr)
48592ffb21SWarner Losh {
49592ffb21SWarner Losh struct drm_device *dev = drm_get_device_from_kdev(kdev);
50592ffb21SWarner Losh struct drm_file *file_priv = NULL;
51592ffb21SWarner Losh drm_local_map_t *map;
52592ffb21SWarner Losh enum drm_map_type type;
53592ffb21SWarner Losh vm_paddr_t phys;
54592ffb21SWarner Losh int error;
55592ffb21SWarner Losh
56592ffb21SWarner Losh /* d_mmap gets called twice, we can only reference file_priv during
57592ffb21SWarner Losh * the first call. We need to assume that if error is EBADF the
58592ffb21SWarner Losh * call was successful and the client is authenticated.
59592ffb21SWarner Losh */
60592ffb21SWarner Losh error = devfs_get_cdevpriv((void **)&file_priv);
61592ffb21SWarner Losh if (error == ENOENT) {
62592ffb21SWarner Losh DRM_ERROR("Could not find authenticator!\n");
63592ffb21SWarner Losh return EINVAL;
64592ffb21SWarner Losh }
65592ffb21SWarner Losh
66592ffb21SWarner Losh if (file_priv && !file_priv->authenticated)
67592ffb21SWarner Losh return EACCES;
68592ffb21SWarner Losh
69592ffb21SWarner Losh DRM_DEBUG("called with offset %016jx\n", offset);
70592ffb21SWarner Losh if (dev->dma && offset < ptoa(dev->dma->page_count)) {
71592ffb21SWarner Losh drm_device_dma_t *dma = dev->dma;
72592ffb21SWarner Losh
73592ffb21SWarner Losh DRM_SPINLOCK(&dev->dma_lock);
74592ffb21SWarner Losh
75592ffb21SWarner Losh if (dma->pagelist != NULL) {
76592ffb21SWarner Losh unsigned long page = offset >> PAGE_SHIFT;
77592ffb21SWarner Losh unsigned long phys = dma->pagelist[page];
78592ffb21SWarner Losh
79592ffb21SWarner Losh DRM_SPINUNLOCK(&dev->dma_lock);
80592ffb21SWarner Losh *paddr = phys;
81592ffb21SWarner Losh return 0;
82592ffb21SWarner Losh } else {
83592ffb21SWarner Losh DRM_SPINUNLOCK(&dev->dma_lock);
84592ffb21SWarner Losh return -1;
85592ffb21SWarner Losh }
86592ffb21SWarner Losh }
87592ffb21SWarner Losh
88592ffb21SWarner Losh /* A sequential search of a linked list is
89592ffb21SWarner Losh fine here because: 1) there will only be
90592ffb21SWarner Losh about 5-10 entries in the list and, 2) a
91592ffb21SWarner Losh DRI client only has to do this mapping
92592ffb21SWarner Losh once, so it doesn't have to be optimized
93592ffb21SWarner Losh for performance, even if the list was a
94592ffb21SWarner Losh bit longer.
95592ffb21SWarner Losh */
96592ffb21SWarner Losh DRM_LOCK(dev);
97592ffb21SWarner Losh TAILQ_FOREACH(map, &dev->maplist, link) {
98592ffb21SWarner Losh if (offset >> DRM_MAP_HANDLE_SHIFT ==
99592ffb21SWarner Losh (unsigned long)map->handle >> DRM_MAP_HANDLE_SHIFT)
100592ffb21SWarner Losh break;
101592ffb21SWarner Losh }
102592ffb21SWarner Losh
103592ffb21SWarner Losh if (map == NULL) {
104592ffb21SWarner Losh DRM_DEBUG("Can't find map, request offset = %016jx\n", offset);
105592ffb21SWarner Losh TAILQ_FOREACH(map, &dev->maplist, link) {
106592ffb21SWarner Losh DRM_DEBUG("map offset = %016lx, handle = %016lx\n",
107592ffb21SWarner Losh map->offset, (unsigned long)map->handle);
108592ffb21SWarner Losh }
109592ffb21SWarner Losh DRM_UNLOCK(dev);
110592ffb21SWarner Losh return -1;
111592ffb21SWarner Losh }
112592ffb21SWarner Losh if (((map->flags & _DRM_RESTRICTED) && !DRM_SUSER(DRM_CURPROC))) {
113592ffb21SWarner Losh DRM_UNLOCK(dev);
114592ffb21SWarner Losh DRM_DEBUG("restricted map\n");
115592ffb21SWarner Losh return -1;
116592ffb21SWarner Losh }
117592ffb21SWarner Losh type = map->type;
118592ffb21SWarner Losh DRM_UNLOCK(dev);
119592ffb21SWarner Losh
120592ffb21SWarner Losh offset = offset & ((1ULL << DRM_MAP_HANDLE_SHIFT) - 1);
121592ffb21SWarner Losh
122592ffb21SWarner Losh switch (type) {
123592ffb21SWarner Losh case _DRM_FRAME_BUFFER:
124592ffb21SWarner Losh case _DRM_AGP:
125592ffb21SWarner Losh *memattr = VM_MEMATTR_WRITE_COMBINING;
126592ffb21SWarner Losh /* FALLTHROUGH */
127592ffb21SWarner Losh case _DRM_REGISTERS:
128592ffb21SWarner Losh phys = map->offset + offset;
129592ffb21SWarner Losh break;
130592ffb21SWarner Losh case _DRM_SCATTER_GATHER:
131592ffb21SWarner Losh *memattr = VM_MEMATTR_WRITE_COMBINING;
132592ffb21SWarner Losh /* FALLTHROUGH */
133592ffb21SWarner Losh case _DRM_CONSISTENT:
134592ffb21SWarner Losh case _DRM_SHM:
135592ffb21SWarner Losh phys = vtophys((char *)map->virtual + offset);
136592ffb21SWarner Losh break;
137592ffb21SWarner Losh default:
138592ffb21SWarner Losh DRM_ERROR("bad map type %d\n", type);
139592ffb21SWarner Losh return -1; /* This should never happen. */
140592ffb21SWarner Losh }
141592ffb21SWarner Losh
142592ffb21SWarner Losh *paddr = phys;
143592ffb21SWarner Losh return 0;
144592ffb21SWarner Losh }
145592ffb21SWarner Losh #endif /* FREEBSD_NOTYET */
146