xref: /freebsd/sys/vm/device_pager.c (revision 670d17b5c0824768cc5f70d8aff4b245cca0ed2e)
1 /*
2  * Copyright (c) 1990 University of Utah.
3  * Copyright (c) 1991, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * the Systems Programming Group of the University of Utah Computer
8  * Science Department.
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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)device_pager.c	8.1 (Berkeley) 6/11/93
39  * $FreeBSD$
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/conf.h>
45 #include <sys/lock.h>
46 #include <sys/proc.h>
47 #include <sys/mutex.h>
48 #include <sys/mman.h>
49 #include <sys/sx.h>
50 
51 #include <vm/vm.h>
52 #include <vm/vm_object.h>
53 #include <vm/vm_page.h>
54 #include <vm/vm_pager.h>
55 #include <vm/uma.h>
56 
57 static void dev_pager_init(void);
58 static vm_object_t dev_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
59 		vm_ooffset_t);
60 static void dev_pager_dealloc(vm_object_t);
61 static int dev_pager_getpages(vm_object_t, vm_page_t *, int, int);
62 static void dev_pager_putpages(vm_object_t, vm_page_t *, int,
63 		boolean_t, int *);
64 static boolean_t dev_pager_haspage(vm_object_t, vm_pindex_t, int *,
65 		int *);
66 
67 /* list of device pager objects */
68 static struct pagerlst dev_pager_object_list;
69 /* protect against object creation */
70 static struct sx dev_pager_sx;
71 /* protect list manipulation */
72 static struct mtx dev_pager_mtx;
73 
74 
75 static uma_zone_t fakepg_zone;
76 
77 static vm_page_t dev_pager_getfake(vm_offset_t);
78 static void dev_pager_putfake(vm_page_t);
79 
80 struct pagerops devicepagerops = {
81 	dev_pager_init,
82 	dev_pager_alloc,
83 	dev_pager_dealloc,
84 	dev_pager_getpages,
85 	dev_pager_putpages,
86 	dev_pager_haspage,
87 	NULL
88 };
89 
90 static void
91 dev_pager_init()
92 {
93 	TAILQ_INIT(&dev_pager_object_list);
94 	sx_init(&dev_pager_sx, "dev_pager create");
95 	mtx_init(&dev_pager_mtx, "dev_pager list", MTX_DEF);
96 	fakepg_zone = uma_zcreate("DP fakepg", sizeof(struct vm_page),
97 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
98 }
99 
100 static vm_object_t
101 dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff)
102 {
103 	dev_t dev;
104 	d_mmap_t *mapfunc;
105 	vm_object_t object;
106 	unsigned int npages;
107 	vm_offset_t off;
108 
109 	mtx_assert(&Giant, MA_OWNED);
110 	/*
111 	 * Make sure this device can be mapped.
112 	 */
113 	dev = handle;
114 	mapfunc = devsw(dev)->d_mmap;
115 	if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop) {
116 		printf("obsolete map function %p\n", (void *)mapfunc);
117 		return (NULL);
118 	}
119 
120 	/*
121 	 * Offset should be page aligned.
122 	 */
123 	if (foff & PAGE_MASK)
124 		return (NULL);
125 
126 	size = round_page(size);
127 
128 	/*
129 	 * Check that the specified range of the device allows the desired
130 	 * protection.
131 	 *
132 	 * XXX assumes VM_PROT_* == PROT_*
133 	 */
134 	npages = OFF_TO_IDX(size);
135 	for (off = foff; npages--; off += PAGE_SIZE)
136 		if ((*mapfunc) (dev, off, (int) prot) == -1)
137 			return (NULL);
138 
139 	/*
140 	 * Lock to prevent object creation race condition.
141 	 */
142 	sx_xlock(&dev_pager_sx);
143 
144 	/*
145 	 * Look up pager, creating as necessary.
146 	 */
147 	object = vm_pager_object_lookup(&dev_pager_object_list, handle);
148 	if (object == NULL) {
149 		/*
150 		 * Allocate object and associate it with the pager.
151 		 */
152 		object = vm_object_allocate(OBJT_DEVICE,
153 			OFF_TO_IDX(foff + size));
154 		object->handle = handle;
155 		TAILQ_INIT(&object->un_pager.devp.devp_pglist);
156 		mtx_lock(&dev_pager_mtx);
157 		TAILQ_INSERT_TAIL(&dev_pager_object_list, object, pager_object_list);
158 		mtx_unlock(&dev_pager_mtx);
159 	} else {
160 		/*
161 		 * Gain a reference to the object.
162 		 */
163 		vm_object_reference(object);
164 		if (OFF_TO_IDX(foff + size) > object->size)
165 			object->size = OFF_TO_IDX(foff + size);
166 	}
167 
168 	sx_xunlock(&dev_pager_sx);
169 
170 	return (object);
171 }
172 
173 static void
174 dev_pager_dealloc(object)
175 	vm_object_t object;
176 {
177 	vm_page_t m;
178 
179 	mtx_lock(&dev_pager_mtx);
180 	TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
181 	mtx_unlock(&dev_pager_mtx);
182 	/*
183 	 * Free up our fake pages.
184 	 */
185 	while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != 0) {
186 		TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq);
187 		dev_pager_putfake(m);
188 	}
189 }
190 
191 static int
192 dev_pager_getpages(object, m, count, reqpage)
193 	vm_object_t object;
194 	vm_page_t *m;
195 	int count;
196 	int reqpage;
197 {
198 	vm_offset_t offset;
199 	vm_offset_t paddr;
200 	vm_page_t page;
201 	dev_t dev;
202 	int i;
203 	d_mmap_t *mapfunc;
204 	int prot;
205 
206 	mtx_assert(&Giant, MA_OWNED);
207 	dev = object->handle;
208 	offset = m[reqpage]->pindex;
209 	prot = PROT_READ;	/* XXX should pass in? */
210 	mapfunc = devsw(dev)->d_mmap;
211 
212 	if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop)
213 		panic("dev_pager_getpage: no map function");
214 
215 	paddr = pmap_phys_address((*mapfunc) (dev, (vm_offset_t) offset << PAGE_SHIFT, prot));
216 	KASSERT(paddr != -1,("dev_pager_getpage: map function returns error"));
217 	/*
218 	 * Replace the passed in reqpage page with our own fake page and free up the
219 	 * all of the original pages.
220 	 */
221 	page = dev_pager_getfake(paddr);
222 	TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq);
223 	for (i = 0; i < count; i++) {
224 		vm_page_free(m[i]);
225 	}
226 	vm_page_insert(page, object, offset);
227 
228 	return (VM_PAGER_OK);
229 }
230 
231 static void
232 dev_pager_putpages(object, m, count, sync, rtvals)
233 	vm_object_t object;
234 	vm_page_t *m;
235 	int count;
236 	boolean_t sync;
237 	int *rtvals;
238 {
239 	panic("dev_pager_putpage called");
240 }
241 
242 static boolean_t
243 dev_pager_haspage(object, pindex, before, after)
244 	vm_object_t object;
245 	vm_pindex_t pindex;
246 	int *before;
247 	int *after;
248 {
249 	if (before != NULL)
250 		*before = 0;
251 	if (after != NULL)
252 		*after = 0;
253 	return (TRUE);
254 }
255 
256 static vm_page_t
257 dev_pager_getfake(paddr)
258 	vm_offset_t paddr;
259 {
260 	vm_page_t m;
261 
262 	m = uma_zalloc(fakepg_zone, M_WAITOK);
263 
264 	m->flags = PG_BUSY | PG_FICTITIOUS;
265 	m->valid = VM_PAGE_BITS_ALL;
266 	m->dirty = 0;
267 	m->busy = 0;
268 	m->queue = PQ_NONE;
269 	m->object = NULL;
270 
271 	m->wire_count = 1;
272 	m->hold_count = 0;
273 	m->phys_addr = paddr;
274 
275 	return (m);
276 }
277 
278 static void
279 dev_pager_putfake(m)
280 	vm_page_t m;
281 {
282 	if (!(m->flags & PG_FICTITIOUS))
283 		panic("dev_pager_putfake: bad page");
284 	uma_zfree(fakepg_zone, m);
285 }
286