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