xref: /freebsd/sys/vm/device_pager.c (revision e4542174b02a9ad9a5c3e802f97df69a3ee781c1)
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  * $Id: device_pager.c,v 1.38 1999/01/21 08:29:09 dillon Exp $
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/conf.h>
45 #include <sys/mman.h>
46 #include <sys/malloc.h>
47 
48 #include <vm/vm.h>
49 #include <vm/vm_prot.h>
50 #include <vm/vm_object.h>
51 #include <vm/vm_page.h>
52 #include <vm/vm_pager.h>
53 #include <vm/vm_zone.h>
54 
55 static void dev_pager_init __P((void));
56 static vm_object_t dev_pager_alloc __P((void *, vm_ooffset_t, vm_prot_t,
57 		vm_ooffset_t));
58 static void dev_pager_dealloc __P((vm_object_t));
59 static int dev_pager_getpages __P((vm_object_t, vm_page_t *, int, int));
60 static void dev_pager_putpages __P((vm_object_t, vm_page_t *, int,
61 		boolean_t, int *));
62 static boolean_t dev_pager_haspage __P((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 
68 static vm_zone_t fakepg_zone;
69 static struct vm_zone fakepg_zone_store;
70 
71 static vm_page_t dev_pager_getfake __P((vm_offset_t));
72 static void dev_pager_putfake __P((vm_page_t));
73 
74 static int dev_pager_alloc_lock, dev_pager_alloc_lock_want;
75 
76 struct pagerops devicepagerops = {
77 	dev_pager_init,
78 	dev_pager_alloc,
79 	dev_pager_dealloc,
80 	dev_pager_getpages,
81 	dev_pager_putpages,
82 	dev_pager_haspage,
83 	NULL
84 };
85 
86 static void
87 dev_pager_init()
88 {
89 	TAILQ_INIT(&dev_pager_object_list);
90 	fakepg_zone = &fakepg_zone_store;
91 	zinitna(fakepg_zone, NULL, "DP fakepg", sizeof(struct vm_page), 0, 0, 2);
92 }
93 
94 static vm_object_t
95 dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff)
96 {
97 	dev_t dev;
98 	d_mmap_t *mapfunc;
99 	vm_object_t object;
100 	unsigned int npages;
101 	vm_offset_t off;
102 
103 	/*
104 	 * Make sure this device can be mapped.
105 	 */
106 	dev = (dev_t) (uintptr_t) handle;
107 	mapfunc = cdevsw[major(dev)]->d_mmap;
108 	if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop) {
109 		printf("obsolete map function %p\n", (void *)mapfunc);
110 		return (NULL);
111 	}
112 
113 	/*
114 	 * Offset should be page aligned.
115 	 */
116 	if (foff & PAGE_MASK)
117 		return (NULL);
118 
119 	size = round_page(size);
120 
121 	/*
122 	 * Check that the specified range of the device allows the desired
123 	 * protection.
124 	 *
125 	 * XXX assumes VM_PROT_* == PROT_*
126 	 */
127 	npages = OFF_TO_IDX(size);
128 	for (off = foff; npages--; off += PAGE_SIZE)
129 		if ((*mapfunc) (dev, off, (int) prot) == -1)
130 			return (NULL);
131 
132 	/*
133 	 * Lock to prevent object creation race contion.
134 	 */
135 	while (dev_pager_alloc_lock) {
136 		dev_pager_alloc_lock_want++;
137 		tsleep(&dev_pager_alloc_lock, PVM, "dvpall", 0);
138 		dev_pager_alloc_lock_want--;
139 	}
140 	dev_pager_alloc_lock = 1;
141 
142 	/*
143 	 * Look up pager, creating as necessary.
144 	 */
145 	object = vm_pager_object_lookup(&dev_pager_object_list, handle);
146 	if (object == NULL) {
147 		/*
148 		 * Allocate object and associate it with the pager.
149 		 */
150 		object = vm_object_allocate(OBJT_DEVICE,
151 			OFF_TO_IDX(foff + size));
152 		object->handle = handle;
153 		TAILQ_INIT(&object->un_pager.devp.devp_pglist);
154 		TAILQ_INSERT_TAIL(&dev_pager_object_list, object, pager_object_list);
155 	} else {
156 		/*
157 		 * Gain a reference to the object.
158 		 */
159 		vm_object_reference(object);
160 		if (OFF_TO_IDX(foff + size) > object->size)
161 			object->size = OFF_TO_IDX(foff + size);
162 	}
163 
164 	dev_pager_alloc_lock = 0;
165 	if (dev_pager_alloc_lock_want)
166 		wakeup(&dev_pager_alloc_lock);
167 
168 	return (object);
169 }
170 
171 static void
172 dev_pager_dealloc(object)
173 	vm_object_t object;
174 {
175 	vm_page_t m;
176 
177 	TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
178 	/*
179 	 * Free up our fake pages.
180 	 */
181 	while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != 0) {
182 		TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq);
183 		dev_pager_putfake(m);
184 	}
185 }
186 
187 static int
188 dev_pager_getpages(object, m, count, reqpage)
189 	vm_object_t object;
190 	vm_page_t *m;
191 	int count;
192 	int reqpage;
193 {
194 	vm_offset_t offset;
195 	vm_offset_t paddr;
196 	vm_page_t page;
197 	dev_t dev;
198 	int i, s;
199 	d_mmap_t *mapfunc;
200 	int prot;
201 
202 	dev = (dev_t) (uintptr_t) object->handle;
203 	offset = m[reqpage]->pindex;
204 	prot = PROT_READ;	/* XXX should pass in? */
205 	mapfunc = cdevsw[major(dev)]->d_mmap;
206 
207 	if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop)
208 		panic("dev_pager_getpage: no map function");
209 
210 	paddr = pmap_phys_address((*mapfunc) ((dev_t) dev, (vm_offset_t) offset << PAGE_SHIFT, prot));
211 	KASSERT(paddr != -1,("dev_pager_getpage: map function returns error"));
212 	/*
213 	 * Replace the passed in reqpage page with our own fake page and free up the
214 	 * all of the original pages.
215 	 */
216 	page = dev_pager_getfake(paddr);
217 	TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq);
218 	for (i = 0; i < count; i++) {
219 		vm_page_free(m[i]);
220 	}
221 	s = splhigh();
222 	vm_page_insert(page, object, offset);
223 	splx(s);
224 
225 	return (VM_PAGER_OK);
226 }
227 
228 static void
229 dev_pager_putpages(object, m, count, sync, rtvals)
230 	vm_object_t object;
231 	vm_page_t *m;
232 	int count;
233 	boolean_t sync;
234 	int *rtvals;
235 {
236 	panic("dev_pager_putpage called");
237 }
238 
239 static boolean_t
240 dev_pager_haspage(object, pindex, before, after)
241 	vm_object_t object;
242 	vm_pindex_t pindex;
243 	int *before;
244 	int *after;
245 {
246 	if (before != NULL)
247 		*before = 0;
248 	if (after != NULL)
249 		*after = 0;
250 	return (TRUE);
251 }
252 
253 static vm_page_t
254 dev_pager_getfake(paddr)
255 	vm_offset_t paddr;
256 {
257 	vm_page_t m;
258 
259 	m = zalloc(fakepg_zone);
260 
261 	m->flags = PG_BUSY | PG_FICTITIOUS;
262 	m->valid = VM_PAGE_BITS_ALL;
263 	m->dirty = 0;
264 	m->busy = 0;
265 	m->queue = PQ_NONE;
266 	m->object = NULL;
267 
268 	m->wire_count = 1;
269 	m->hold_count = 0;
270 	m->phys_addr = paddr;
271 
272 	return (m);
273 }
274 
275 static void
276 dev_pager_putfake(m)
277 	vm_page_t m;
278 {
279 	if (!(m->flags & PG_FICTITIOUS))
280 		panic("dev_pager_putfake: bad page");
281 	zfree(fakepg_zone, m);
282 }
283