4 * Copyright (c) 1990 University of Utah.
5 * Copyright (c) 1991, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)device_pager.c 8.1 (Berkeley) 6/11/93
37 * $FreeBSD: src/sys/vm/device_pager.c,v 1.46.2.1 2000/08/02 21:54:37 peter Exp $
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
45 #include <sys/device.h>
46 #include <sys/queue.h>
47 #include <sys/malloc.h>
48 #include <sys/thread2.h>
49 #include <sys/mutex2.h>
52 #include <vm/vm_object.h>
53 #include <vm/vm_page.h>
54 #include <vm/vm_pager.h>
55 #include <vm/vm_zone.h>
56 #include <vm/vm_page2.h>
58 static void dev_pager_dealloc (vm_object_t
);
59 static int dev_pager_getpage (vm_object_t
, vm_page_t
*, int);
60 static void dev_pager_putpages (vm_object_t
, vm_page_t
*, int, int, int *);
61 static boolean_t
dev_pager_haspage (vm_object_t
, vm_pindex_t
);
63 /* list of device pager objects */
64 static TAILQ_HEAD(, vm_page
) dev_freepages_list
=
65 TAILQ_HEAD_INITIALIZER(dev_freepages_list
);
66 static MALLOC_DEFINE(M_FICTITIOUS_PAGES
, "device-mapped pages",
67 "Device mapped pages");
69 static vm_page_t
dev_pager_getfake (vm_paddr_t
, int);
70 static void dev_pager_putfake (vm_page_t
);
72 struct pagerops devicepagerops
= {
79 /* list of device pager objects */
80 static struct pagerlst dev_pager_object_list
=
81 TAILQ_HEAD_INITIALIZER(dev_pager_object_list
);
82 /* protect list manipulation */
83 static struct mtx dev_pager_mtx
= MTX_INITIALIZER("devpgr");
85 static int old_dev_pager_ctor(void *handle
, vm_ooffset_t size
, vm_prot_t prot
,
86 vm_ooffset_t foff
, struct ucred
*cred
, u_short
*pg_color
);
87 static void old_dev_pager_dtor(void *handle
);
88 static int old_dev_pager_fault(vm_object_t object
, vm_ooffset_t offset
,
89 int prot
, vm_page_t
*mres
);
91 static struct cdev_pager_ops old_dev_pager_ops
= {
92 .cdev_pg_ctor
= old_dev_pager_ctor
,
93 .cdev_pg_dtor
= old_dev_pager_dtor
,
94 .cdev_pg_fault
= old_dev_pager_fault
98 cdev_pager_lookup(void *handle
)
102 mtx_lock(&dev_pager_mtx
);
103 object
= vm_pager_object_lookup(&dev_pager_object_list
, handle
);
104 mtx_unlock(&dev_pager_mtx
);
110 cdev_pager_allocate(void *handle
, enum obj_type tp
, struct cdev_pager_ops
*ops
,
111 vm_ooffset_t size
, vm_prot_t prot
, vm_ooffset_t foff
, struct ucred
*cred
)
118 * Offset should be page aligned.
120 if (foff
& PAGE_MASK
)
123 size
= round_page64(size
);
125 if (ops
->cdev_pg_ctor(handle
, size
, prot
, foff
, cred
, &color
) != 0)
129 * Look up pager, creating as necessary.
131 mtx_lock(&dev_pager_mtx
);
132 object
= vm_pager_object_lookup(&dev_pager_object_list
, handle
);
133 if (object
== NULL
) {
135 * Allocate object and associate it with the pager.
137 object
= vm_object_allocate_hold(tp
,
138 OFF_TO_IDX(foff
+ size
));
139 object
->handle
= handle
;
140 object
->un_pager
.devp
.ops
= ops
;
141 object
->un_pager
.devp
.dev
= handle
;
142 TAILQ_INIT(&object
->un_pager
.devp
.devp_pglist
);
145 * handle is only a device for old_dev_pager_ctor.
147 if (ops
->cdev_pg_ctor
== old_dev_pager_ctor
) {
149 dev
->si_object
= object
;
152 TAILQ_INSERT_TAIL(&dev_pager_object_list
, object
,
155 vm_object_drop(object
);
158 * Gain a reference to the object.
160 vm_object_hold(object
);
161 vm_object_reference_locked(object
);
162 if (OFF_TO_IDX(foff
+ size
) > object
->size
)
163 object
->size
= OFF_TO_IDX(foff
+ size
);
164 vm_object_drop(object
);
166 mtx_unlock(&dev_pager_mtx
);
175 dev_pager_alloc(void *handle
, off_t size
, vm_prot_t prot
, off_t foff
)
177 return (cdev_pager_allocate(handle
, OBJT_DEVICE
, &old_dev_pager_ops
,
178 size
, prot
, foff
, NULL
));
182 * Caller must hold object lock.
185 cdev_pager_free_page(vm_object_t object
, vm_page_t m
)
187 if (object
->type
== OBJT_MGTDEVICE
) {
188 KKASSERT((m
->flags
& PG_FICTITIOUS
) != 0);
189 pmap_page_protect(m
, VM_PROT_NONE
);
192 } else if (object
->type
== OBJT_DEVICE
) {
193 TAILQ_REMOVE(&object
->un_pager
.devp
.devp_pglist
, m
, pageq
);
194 dev_pager_putfake(m
);
202 dev_pager_dealloc(vm_object_t object
)
207 * NOTE: Callback may recurse into the device pager so do not
208 * obtain dev_pager_mtx until after it returns.
210 * The mutex should only be needed when manipulating the list.
212 object
->un_pager
.devp
.ops
->cdev_pg_dtor(object
->un_pager
.devp
.dev
);
214 mtx_lock(&dev_pager_mtx
);
215 TAILQ_REMOVE(&dev_pager_object_list
, object
, pager_object_list
);
216 mtx_unlock(&dev_pager_mtx
);
218 if (object
->type
== OBJT_DEVICE
) {
220 * Free up our fake pages.
222 while ((m
= TAILQ_FIRST(&object
->un_pager
.devp
.devp_pglist
)) !=
224 TAILQ_REMOVE(&object
->un_pager
.devp
.devp_pglist
,
226 dev_pager_putfake(m
);
234 * WARNING! Do not obtain dev_pager_mtx here, doing so will cause a
235 * deadlock in DRMs VM paging code.
238 dev_pager_getpage(vm_object_t object
, vm_page_t
*mpp
, int seqaccess
)
245 error
= object
->un_pager
.devp
.ops
->cdev_pg_fault(
246 object
, IDX_TO_OFF(page
->pindex
),
256 dev_pager_putpages(vm_object_t object
, vm_page_t
*m
,
257 int count
, int sync
, int *rtvals
)
259 panic("dev_pager_putpage called");
266 dev_pager_haspage(vm_object_t object
, vm_pindex_t pindex
)
272 * The caller does not need to hold dev_pager_mtx() but caller must ensure
273 * no page-use collision.
276 dev_pager_getfake(vm_paddr_t paddr
, int pat_mode
)
280 m
= kmalloc(sizeof(*m
), M_FICTITIOUS_PAGES
, M_WAITOK
|M_ZERO
);
284 m
->flags
= PG_BUSY
| PG_FICTITIOUS
;
285 m
->valid
= VM_PAGE_BITS_ALL
;
293 m
->phys_addr
= paddr
;
294 m
->pat_mode
= pat_mode
;
296 spin_init(&m
->spin
, "dev_page");
302 * The caller does not need to hold dev_pager_mtx() but caller must ensure
303 * no page-use collision within the object.
306 dev_pager_putfake(vm_page_t m
)
308 if (!(m
->flags
& PG_FICTITIOUS
))
309 panic("dev_pager_putfake: bad page");
310 KKASSERT(m
->object
== NULL
);
311 KKASSERT(m
->hold_count
== 0);
312 kfree(m
, M_FICTITIOUS_PAGES
);
316 old_dev_pager_ctor(void *handle
, vm_ooffset_t size
, vm_prot_t prot
,
317 vm_ooffset_t foff
, struct ucred
*cred
, u_short
*color
)
326 * Check that the specified range of the device allows the desired
329 * XXX assumes VM_PROT_* == PROT_*
331 npages
= OFF_TO_IDX(size
);
332 for (off
= foff
; npages
--; off
+= PAGE_SIZE
) {
333 if (dev_dmmap(dev
, off
, (int)prot
, NULL
) == -1)
340 static void old_dev_pager_dtor(void *handle
)
346 KKASSERT(dev
->si_object
);
347 dev
->si_object
= NULL
;
351 static int old_dev_pager_fault(vm_object_t object
, vm_ooffset_t offset
,
352 int prot
, vm_page_t
*mres
)
356 vm_offset_t pidx
= OFF_TO_IDX(offset
);
360 dev
= object
->handle
;
362 paddr
= pmap_phys_address(
363 dev_dmmap(dev
, offset
, prot
, NULL
));
364 KASSERT(paddr
!= -1,("dev_pager_getpage: map function returns error"));
365 KKASSERT(object
->type
== OBJT_DEVICE
);
367 if (page
->flags
& PG_FICTITIOUS
) {
369 * If the passed in reqpage page is already a fake page,
370 * update it with the new physical address.
372 page
->phys_addr
= paddr
;
373 page
->valid
= VM_PAGE_BITS_ALL
;
376 * Replace the passed in reqpage page with our own fake page
377 * and free up all the original pages. Object lock must be
378 * held when manipulating devp_pglist and inserting the
381 page
= dev_pager_getfake(paddr
, object
->memattr
);
382 vm_object_hold(object
);
383 TAILQ_INSERT_TAIL(&object
->un_pager
.devp
.devp_pglist
,
386 if (vm_page_insert(page
, object
, pidx
) == FALSE
) {
387 panic("dev_pager_getpage: page (%p,%016jx) exists",
388 object
, (uintmax_t)pidx
);
390 vm_object_drop(object
);
393 return (VM_PAGER_OK
);