blob: 9c2cea56a05614166e23d47bb9b6122f58a9cea3 [file] [log] [blame]
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001/* vm.c -- Memory mapping for DRM -*- linux-c -*-
2 * Created: Mon Jan 4 08:58:31 1999 by faith@precisioninsight.com
Daryll Straussb6a28bf1999-12-05 23:10:37 +00003 *
Kevin E Martin99efe3c2000-05-18 06:14:27 +00004 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
Daryll Straussb6a28bf1999-12-05 23:10:37 +00005 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 *
Jeff Hartmann5bd80142000-03-16 03:37:30 +000026 * $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/kernel/vm.c,v 1.5 2000/02/23 04:47:31 martin Exp $
Daryll Straussb6a28bf1999-12-05 23:10:37 +000027 *
28 */
29
30#define __NO_VERSION__
31#include "drmP.h"
32
33struct vm_operations_struct drm_vm_ops = {
34 nopage: drm_vm_nopage,
35 open: drm_vm_open,
36 close: drm_vm_close,
37};
38
39struct vm_operations_struct drm_vm_shm_ops = {
40 nopage: drm_vm_shm_nopage,
41 open: drm_vm_open,
42 close: drm_vm_close,
43};
44
45struct vm_operations_struct drm_vm_dma_ops = {
46 nopage: drm_vm_dma_nopage,
47 open: drm_vm_open,
48 close: drm_vm_close,
49};
50
Daryll Strausse1dba5c1999-12-07 03:37:16 +000051#if LINUX_VERSION_CODE < 0x020317
Daryll Straussb6a28bf1999-12-05 23:10:37 +000052unsigned long drm_vm_nopage(struct vm_area_struct *vma,
53 unsigned long address,
54 int write_access)
Daryll Strausse1dba5c1999-12-07 03:37:16 +000055#else
56 /* Return type changed in 2.3.23 */
57struct page *drm_vm_nopage(struct vm_area_struct *vma,
58 unsigned long address,
59 int write_access)
60#endif
Daryll Straussb6a28bf1999-12-05 23:10:37 +000061{
62 DRM_DEBUG("0x%08lx, %d\n", address, write_access);
63
Daryll Strausse1dba5c1999-12-07 03:37:16 +000064 return NOPAGE_SIGBUS; /* Disallow mremap */
Daryll Straussb6a28bf1999-12-05 23:10:37 +000065}
66
Daryll Strausse1dba5c1999-12-07 03:37:16 +000067#if LINUX_VERSION_CODE < 0x020317
Daryll Straussb6a28bf1999-12-05 23:10:37 +000068unsigned long drm_vm_shm_nopage(struct vm_area_struct *vma,
69 unsigned long address,
70 int write_access)
Daryll Strausse1dba5c1999-12-07 03:37:16 +000071#else
72 /* Return type changed in 2.3.23 */
73struct page *drm_vm_shm_nopage(struct vm_area_struct *vma,
74 unsigned long address,
75 int write_access)
76#endif
Daryll Straussb6a28bf1999-12-05 23:10:37 +000077{
78 drm_file_t *priv = vma->vm_file->private_data;
79 drm_device_t *dev = priv->dev;
80 unsigned long physical;
81 unsigned long offset;
82 unsigned long page;
83
Daryll Strausse1dba5c1999-12-07 03:37:16 +000084 if (address > vma->vm_end) return NOPAGE_SIGBUS; /* Disallow mremap */
85 if (!dev->lock.hw_lock) return NOPAGE_OOM; /* Nothing allocated */
Daryll Straussb6a28bf1999-12-05 23:10:37 +000086
87 offset = address - vma->vm_start;
88 page = offset >> PAGE_SHIFT;
89 physical = (unsigned long)dev->lock.hw_lock + (offset & (~PAGE_MASK));
90 atomic_inc(&mem_map[MAP_NR(physical)].count); /* Dec. by kernel */
91
92 DRM_DEBUG("0x%08lx (page %lu) => 0x%08lx\n", address, page, physical);
Daryll Strausse1dba5c1999-12-07 03:37:16 +000093#if LINUX_VERSION_CODE < 0x020317
Daryll Straussb6a28bf1999-12-05 23:10:37 +000094 return physical;
Daryll Strausse1dba5c1999-12-07 03:37:16 +000095#else
96 return mem_map + MAP_NR(physical);
97#endif
Daryll Straussb6a28bf1999-12-05 23:10:37 +000098}
99
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000100#if LINUX_VERSION_CODE < 0x020317
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000101unsigned long drm_vm_dma_nopage(struct vm_area_struct *vma,
102 unsigned long address,
103 int write_access)
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000104#else
105 /* Return type changed in 2.3.23 */
106struct page *drm_vm_dma_nopage(struct vm_area_struct *vma,
107 unsigned long address,
108 int write_access)
109#endif
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000110{
111 drm_file_t *priv = vma->vm_file->private_data;
112 drm_device_t *dev = priv->dev;
113 drm_device_dma_t *dma = dev->dma;
114 unsigned long physical;
115 unsigned long offset;
116 unsigned long page;
117
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000118 if (!dma) return NOPAGE_SIGBUS; /* Error */
119 if (address > vma->vm_end) return NOPAGE_SIGBUS; /* Disallow mremap */
120 if (!dma->pagelist) return NOPAGE_OOM ; /* Nothing allocated */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000121
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000122 offset = address - vma->vm_start; /* vm_[pg]off[set] should be 0 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000123 page = offset >> PAGE_SHIFT;
124 physical = dma->pagelist[page] + (offset & (~PAGE_MASK));
125 atomic_inc(&mem_map[MAP_NR(physical)].count); /* Dec. by kernel */
126
127 DRM_DEBUG("0x%08lx (page %lu) => 0x%08lx\n", address, page, physical);
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000128#if LINUX_VERSION_CODE < 0x020317
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000129 return physical;
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000130#else
131 return mem_map + MAP_NR(physical);
132#endif
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000133}
134
135void drm_vm_open(struct vm_area_struct *vma)
136{
137 drm_file_t *priv = vma->vm_file->private_data;
138 drm_device_t *dev = priv->dev;
139#if DRM_DEBUG_CODE
140 drm_vma_entry_t *vma_entry;
141#endif
142
143 DRM_DEBUG("0x%08lx,0x%08lx\n",
144 vma->vm_start, vma->vm_end - vma->vm_start);
145 atomic_inc(&dev->vma_count);
146 MOD_INC_USE_COUNT;
147
148#if DRM_DEBUG_CODE
149 vma_entry = drm_alloc(sizeof(*vma_entry), DRM_MEM_VMAS);
150 if (vma_entry) {
151 down(&dev->struct_sem);
152 vma_entry->vma = vma;
153 vma_entry->next = dev->vmalist;
154 vma_entry->pid = current->pid;
155 dev->vmalist = vma_entry;
156 up(&dev->struct_sem);
157 }
158#endif
159}
160
161void drm_vm_close(struct vm_area_struct *vma)
162{
163 drm_file_t *priv = vma->vm_file->private_data;
164 drm_device_t *dev = priv->dev;
165#if DRM_DEBUG_CODE
166 drm_vma_entry_t *pt, *prev;
167#endif
168
169 DRM_DEBUG("0x%08lx,0x%08lx\n",
170 vma->vm_start, vma->vm_end - vma->vm_start);
171 MOD_DEC_USE_COUNT;
172 atomic_dec(&dev->vma_count);
173
174#if DRM_DEBUG_CODE
175 down(&dev->struct_sem);
176 for (pt = dev->vmalist, prev = NULL; pt; prev = pt, pt = pt->next) {
177 if (pt->vma == vma) {
178 if (prev) {
179 prev->next = pt->next;
180 } else {
181 dev->vmalist = pt->next;
182 }
183 drm_free(pt, sizeof(*pt), DRM_MEM_VMAS);
184 break;
185 }
186 }
187 up(&dev->struct_sem);
188#endif
189}
190
191int drm_mmap_dma(struct file *filp, struct vm_area_struct *vma)
192{
193 drm_file_t *priv = filp->private_data;
194 drm_device_t *dev = priv->dev;
195 drm_device_dma_t *dma = dev->dma;
196 unsigned long length = vma->vm_end - vma->vm_start;
197
198 DRM_DEBUG("start = 0x%lx, end = 0x%lx, offset = 0x%lx\n",
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000199 vma->vm_start, vma->vm_end, VM_OFFSET(vma));
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000200
201 /* Length must match exact page count */
202 if ((length >> PAGE_SHIFT) != dma->page_count) return -EINVAL;
203
204 vma->vm_ops = &drm_vm_dma_ops;
205 vma->vm_flags |= VM_LOCKED | VM_SHM; /* Don't swap */
206
207#if LINUX_VERSION_CODE < 0x020203 /* KERNEL_VERSION(2,2,3) */
208 /* In Linux 2.2.3 and above, this is
209 handled in do_mmap() in mm/mmap.c. */
210 ++filp->f_count;
211#endif
212 vma->vm_file = filp; /* Needed for drm_vm_open() */
213 drm_vm_open(vma);
214 return 0;
215}
216
217int drm_mmap(struct file *filp, struct vm_area_struct *vma)
218{
219 drm_file_t *priv = filp->private_data;
220 drm_device_t *dev = priv->dev;
221 drm_map_t *map = NULL;
222 int i;
223
224 DRM_DEBUG("start = 0x%lx, end = 0x%lx, offset = 0x%lx\n",
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000225 vma->vm_start, vma->vm_end, VM_OFFSET(vma));
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000226
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000227 if (!VM_OFFSET(vma)) return drm_mmap_dma(filp, vma);
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000228
229 /* A sequential search of a linked list is
230 fine here because: 1) there will only be
231 about 5-10 entries in the list and, 2) a
232 DRI client only has to do this mapping
233 once, so it doesn't have to be optimized
234 for performance, even if the list was a
235 bit longer. */
236 for (i = 0; i < dev->map_count; i++) {
237 map = dev->maplist[i];
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000238 if (map->offset == VM_OFFSET(vma)) break;
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000239 }
240
241 if (i >= dev->map_count) return -EINVAL;
242 if (!map || ((map->flags&_DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN)))
243 return -EPERM;
244
245 /* Check for valid size. */
246 if (map->size != vma->vm_end - vma->vm_start) return -EINVAL;
247
Kevin E Martin99efe3c2000-05-18 06:14:27 +0000248 if (!capable(CAP_SYS_ADMIN) && (map->flags & _DRM_READ_ONLY)) {
249 vma->vm_flags &= VM_MAYWRITE;
250#if defined(__i386__)
251 pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
252#else
253 /* Ye gads this is ugly. With more thought
254 we could move this up higher and use
255 `protection_map' instead. */
256 vma->vm_page_prot = __pgprot(pte_val(pte_wrprotect(
257 __pte(pgprot_val(vma->vm_page_prot)))));
258#endif
259 }
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000260
261 switch (map->type) {
262 case _DRM_FRAME_BUFFER:
263 case _DRM_REGISTERS:
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +0000264 case _DRM_AGP:
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000265 if (VM_OFFSET(vma) >= __pa(high_memory)) {
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000266#if defined(__i386__)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +0000267 if (boot_cpu_data.x86 > 3 && map->type != _DRM_AGP) {
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000268 pgprot_val(vma->vm_page_prot) |= _PAGE_PCD;
269 pgprot_val(vma->vm_page_prot) &= ~_PAGE_PWT;
270 }
271#endif
272 vma->vm_flags |= VM_IO; /* not in core dump */
273 }
274 if (remap_page_range(vma->vm_start,
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000275 VM_OFFSET(vma),
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000276 vma->vm_end - vma->vm_start,
277 vma->vm_page_prot))
278 return -EAGAIN;
Kevin E Martin99efe3c2000-05-18 06:14:27 +0000279 DRM_DEBUG(" Type = %d; start = 0x%lx, end = 0x%lx,"
280 " offset = 0x%lx\n",
281 map->type,
282 vma->vm_start, vma->vm_end, VM_OFFSET(vma));
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000283 vma->vm_ops = &drm_vm_ops;
284 break;
285 case _DRM_SHM:
286 vma->vm_ops = &drm_vm_shm_ops;
287 /* Don't let this area swap. Change when
288 DRM_KERNEL advisory is supported. */
289 vma->vm_flags |= VM_LOCKED;
290 break;
291 default:
292 return -EINVAL; /* This should never happen. */
293 }
294 vma->vm_flags |= VM_LOCKED | VM_SHM; /* Don't swap */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000295
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000296#if LINUX_VERSION_CODE < 0x020203 /* KERNEL_VERSION(2,2,3) */
297 /* In Linux 2.2.3 and above, this is
298 handled in do_mmap() in mm/mmap.c. */
299 ++filp->f_count;
300#endif
301 vma->vm_file = filp; /* Needed for drm_vm_open() */
302 drm_vm_open(vma);
303 return 0;
304}