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