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