xref: /dragonfly/sys/vm/device_pager.c (revision 4e7eb5cc)
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: src/sys/vm/device_pager.c,v 1.46.2.1 2000/08/02 21:54:37 peter Exp $
40  * $DragonFly: src/sys/vm/device_pager.c,v 1.6 2003/11/03 17:11:23 dillon Exp $
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/conf.h>
46 #include <sys/mman.h>
47 #include <sys/device.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/vm_zone.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 
68 static vm_zone_t fakepg_zone;
69 static struct vm_zone fakepg_zone_store;
70 
71 static vm_page_t dev_pager_getfake (vm_paddr_t);
72 static void dev_pager_putfake (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 	vm_object_t object;
99 	unsigned int npages;
100 	vm_offset_t off;
101 
102 	/*
103 	 * Make sure this device can be mapped.
104 	 */
105 	dev = handle;
106 
107 	/*
108 	 * Offset should be page aligned.
109 	 */
110 	if (foff & PAGE_MASK)
111 		return (NULL);
112 
113 	size = round_page(size);
114 
115 	/*
116 	 * Check that the specified range of the device allows the desired
117 	 * protection.
118 	 *
119 	 * XXX assumes VM_PROT_* == PROT_*
120 	 */
121 	npages = OFF_TO_IDX(size);
122 	for (off = foff; npages--; off += PAGE_SIZE) {
123 		if (dev_dmmap(dev, off, (int)prot) == -1)
124 			return (NULL);
125 	}
126 
127 	/*
128 	 * Lock to prevent object creation race condition.
129 	 */
130 	while (dev_pager_alloc_lock) {
131 		dev_pager_alloc_lock_want++;
132 		tsleep(&dev_pager_alloc_lock, 0, "dvpall", 0);
133 		dev_pager_alloc_lock_want--;
134 	}
135 	dev_pager_alloc_lock = 1;
136 
137 	/*
138 	 * Look up pager, creating as necessary.
139 	 */
140 	object = vm_pager_object_lookup(&dev_pager_object_list, handle);
141 	if (object == NULL) {
142 		/*
143 		 * Allocate object and associate it with the pager.
144 		 */
145 		object = vm_object_allocate(OBJT_DEVICE,
146 			OFF_TO_IDX(foff + size));
147 		object->handle = handle;
148 		TAILQ_INIT(&object->un_pager.devp.devp_pglist);
149 		TAILQ_INSERT_TAIL(&dev_pager_object_list, object, pager_object_list);
150 	} else {
151 		/*
152 		 * Gain a reference to the object.
153 		 */
154 		vm_object_reference(object);
155 		if (OFF_TO_IDX(foff + size) > object->size)
156 			object->size = OFF_TO_IDX(foff + size);
157 	}
158 
159 	dev_pager_alloc_lock = 0;
160 	if (dev_pager_alloc_lock_want)
161 		wakeup(&dev_pager_alloc_lock);
162 
163 	return (object);
164 }
165 
166 static void
167 dev_pager_dealloc(object)
168 	vm_object_t object;
169 {
170 	vm_page_t m;
171 
172 	TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
173 	/*
174 	 * Free up our fake pages.
175 	 */
176 	while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != 0) {
177 		TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq);
178 		dev_pager_putfake(m);
179 	}
180 }
181 
182 static int
183 dev_pager_getpages(object, m, count, reqpage)
184 	vm_object_t object;
185 	vm_page_t *m;
186 	int count;
187 	int reqpage;
188 {
189 	vm_offset_t offset;
190 	vm_paddr_t paddr;
191 	vm_page_t page;
192 	dev_t dev;
193 	int i, s;
194 	int prot;
195 
196 	dev = object->handle;
197 	offset = m[reqpage]->pindex;
198 	prot = PROT_READ;	/* XXX should pass in? */
199 
200 	paddr = pmap_phys_address(dev_dmmap(dev, (vm_offset_t) offset << PAGE_SHIFT, prot));
201 	KASSERT(paddr != -1,("dev_pager_getpage: map function returns error"));
202 	/*
203 	 * Replace the passed in reqpage page with our own fake page and free up the
204 	 * all of the original pages.
205 	 */
206 	page = dev_pager_getfake(paddr);
207 	TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq);
208 	for (i = 0; i < count; i++) {
209 		vm_page_free(m[i]);
210 	}
211 	s = splhigh();
212 	vm_page_insert(page, object, offset);
213 	splx(s);
214 
215 	return (VM_PAGER_OK);
216 }
217 
218 static void
219 dev_pager_putpages(object, m, count, sync, rtvals)
220 	vm_object_t object;
221 	vm_page_t *m;
222 	int count;
223 	boolean_t sync;
224 	int *rtvals;
225 {
226 	panic("dev_pager_putpage called");
227 }
228 
229 static boolean_t
230 dev_pager_haspage(object, pindex, before, after)
231 	vm_object_t object;
232 	vm_pindex_t pindex;
233 	int *before;
234 	int *after;
235 {
236 	if (before != NULL)
237 		*before = 0;
238 	if (after != NULL)
239 		*after = 0;
240 	return (TRUE);
241 }
242 
243 static vm_page_t
244 dev_pager_getfake(paddr)
245 	vm_paddr_t paddr;
246 {
247 	vm_page_t m;
248 
249 	m = zalloc(fakepg_zone);
250 
251 	m->flags = PG_BUSY | PG_FICTITIOUS;
252 	m->valid = VM_PAGE_BITS_ALL;
253 	m->dirty = 0;
254 	m->busy = 0;
255 	m->queue = PQ_NONE;
256 	m->object = NULL;
257 
258 	m->wire_count = 1;
259 	m->hold_count = 0;
260 	m->phys_addr = paddr;
261 
262 	return (m);
263 }
264 
265 static void
266 dev_pager_putfake(m)
267 	vm_page_t m;
268 {
269 	if (!(m->flags & PG_FICTITIOUS))
270 		panic("dev_pager_putfake: bad page");
271 	zfree(fakepg_zone, m);
272 }
273