drm_vm.c revision 1.1.2.1 1 /* $NetBSD: drm_vm.c,v 1.1.2.1 2013/07/24 02:53:31 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: drm_vm.c,v 1.1.2.1 2013/07/24 02:53:31 riastradh Exp $");
34
35 #include <sys/types.h>
36
37 #include <uvm/uvm_extern.h>
38
39 #include <drm/drmP.h>
40
41 static paddr_t drm_mmap_dma_paddr(struct drm_device *, off_t, int);
42 static paddr_t drm_mmap_map_paddr(struct drm_device *, struct drm_local_map *,
43 off_t, int);
44
45 paddr_t
46 drm_mmap_paddr(struct drm_device *dev, off_t byte_offset, int prot)
47 {
48 const off_t page_offset = (byte_offset >> PAGE_SHIFT);
49 struct drm_hash_item *hash;
50 paddr_t paddr = (paddr_t)-1;
51 int error;
52
53 if (byte_offset != trunc_page(byte_offset))
54 return (paddr_t)-1;
55
56 mutex_lock(&dev->struct_mutex);
57
58 if ((dev->dma != NULL) &&
59 (0 <= byte_offset) &&
60 (byte_offset <= (dev->dma->page_count << PAGE_SHIFT))) {
61 paddr = drm_mmap_dma_paddr(dev, byte_offset, prot);
62 goto out;
63 }
64
65 if (drm_ht_find_item(&dev->map_hash, page_offset, &hash))
66 goto out;
67
68 struct drm_local_map *const map = drm_hash_entry(hash,
69 struct drm_map_list, hash)->map;
70 if (map == NULL)
71 goto out;
72
73 /*
74 * XXX FreeBSD drops the mutex at this point, which would be
75 * nice, to allow sleeping in bus_dma cruft, but I don't know
76 * what guarantees the map will continue to exist.
77 */
78
79 if (ISSET(map->flags, _DRM_RESTRICTED) && !DRM_SUSER())
80 goto out;
81
82 if (byte_offset < map->offset)
83 goto out;
84
85 paddr = drm_mmap_map_paddr(dev, map, (map->offset - byte_offset),
86 prot);
87
88 out: mutex_unlock(&dev->struct_mutex);
89 return error;
90 }
91
92 static paddr_t
93 drm_mmap_dma_paddr(struct drm_device *dev, off_t byte_offset, int prot)
94 {
95 const off_t page_offset = (byte_offset >> PAGE_SHIFT);
96
97 /* XXX Is this the right mutex? */
98 KASSERT(mutex_is_locked(&dev->struct_mutex));
99
100 if (dev->dma->pagelist == NULL)
101 return (paddr_t)-1;
102
103 if (page_offset >= dev->dma->page_count)
104 return (paddr_t)-1;
105
106 return dev->dma->pagelist[page_offset];
107 }
108
109 static paddr_t
110 drm_mmap_map_paddr(struct drm_device *dev, struct drm_local_map *map,
111 off_t byte_offset, int prot)
112 {
113 int flags = 0;
114
115 switch (map->type) {
116 case _DRM_FRAME_BUFFER:
117 case _DRM_AGP:
118 flags |= BUS_SPACE_MAP_PREFETCHABLE;
119 /* Fall through. */
120
121 case _DRM_REGISTERS:
122 flags |= BUS_SPACE_MAP_LINEAR; /* XXX Why? */
123
124 return bus_space_mmap(dev->bst, map->offset, byte_offset, prot,
125 flags);
126
127 case _DRM_CONSISTENT: {
128 struct drm_dma_handle *const dmah = map->lm_data.dmah;
129
130 return bus_dmamem_mmap(dev->dmat, &dmah->dmah_seg, 1,
131 byte_offset, prot,
132 /* XXX BUS_DMA_WAITOK? We're holding a mutex... */
133 /* XXX What else? BUS_DMA_COHERENT? */
134 (BUS_DMA_WAITOK | BUS_DMA_NOCACHE));
135 }
136
137 case _DRM_SCATTER_GATHER: {
138 struct drm_sg_mem *const sg = dev->sg;
139
140 #if 0 /* XXX */
141 KASSERT(sg == map->lm_data.sg);
142 #endif
143
144 return bus_dmamem_mmap(dev->dmat, sg->sg_segs, sg->sg_nsegs,
145 byte_offset, prot,
146 /* XXX BUS_DMA_WAITOK? We're holding a mutex... */
147 /* XXX What else? BUS_DMA_COHERENT? */
148 (BUS_DMA_WAITOK | BUS_DMA_NOCACHE));
149 }
150
151 case _DRM_SHM:
152 case _DRM_GEM:
153 default:
154 return (paddr_t)-1;
155 }
156 }
157