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