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