xref: /freebsd/sys/vm/device_pager.c (revision 66e576525d35c68fcb86f142ebaa5a448555c0c7)
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, struct ucred *);
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 list manipulation */
68 static struct mtx dev_pager_mtx;
69 
70 
71 static uma_zone_t fakepg_zone;
72 
73 static vm_page_t dev_pager_getfake(vm_paddr_t, vm_memattr_t);
74 static void dev_pager_putfake(vm_page_t);
75 static void dev_pager_updatefake(vm_page_t, vm_paddr_t, vm_memattr_t);
76 
77 struct pagerops devicepagerops = {
78 	.pgo_init =	dev_pager_init,
79 	.pgo_alloc =	dev_pager_alloc,
80 	.pgo_dealloc =	dev_pager_dealloc,
81 	.pgo_getpages =	dev_pager_getpages,
82 	.pgo_putpages =	dev_pager_putpages,
83 	.pgo_haspage =	dev_pager_haspage,
84 };
85 
86 static void
87 dev_pager_init()
88 {
89 	TAILQ_INIT(&dev_pager_object_list);
90 	mtx_init(&dev_pager_mtx, "dev_pager list", NULL, MTX_DEF);
91 	fakepg_zone = uma_zcreate("DP fakepg", sizeof(struct vm_page),
92 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
93 	    UMA_ZONE_NOFREE|UMA_ZONE_VM);
94 }
95 
96 /*
97  * MPSAFE
98  */
99 static vm_object_t
100 dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
101     vm_ooffset_t foff, struct ucred *cred)
102 {
103 	struct cdev *dev;
104 	vm_object_t object, object1;
105 	vm_pindex_t pindex;
106 	unsigned int npages;
107 	vm_paddr_t paddr;
108 	vm_ooffset_t off;
109 	vm_memattr_t dummy;
110 	struct cdevsw *csw;
111 	int ref;
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 	csw = dev_refthread(dev, &ref);
127 	if (csw == NULL)
128 		return (NULL);
129 
130 	/*
131 	 * Check that the specified range of the device allows the desired
132 	 * protection.
133 	 *
134 	 * XXX assumes VM_PROT_* == PROT_*
135 	 */
136 	npages = OFF_TO_IDX(size);
137 	for (off = foff; npages--; off += PAGE_SIZE)
138 		if (csw->d_mmap(dev, off, &paddr, (int)prot, &dummy) != 0) {
139 			dev_relthread(dev, ref);
140 			return (NULL);
141 		}
142 
143 	mtx_lock(&dev_pager_mtx);
144 
145 	/*
146 	 * Look up pager, creating as necessary.
147 	 */
148 	object1 = NULL;
149 	object = vm_pager_object_lookup(&dev_pager_object_list, handle);
150 	if (object == NULL) {
151 		/*
152 		 * Allocate object and associate it with the pager.  Initialize
153 		 * the object's pg_color based upon the physical address of the
154 		 * device's memory.
155 		 */
156 		mtx_unlock(&dev_pager_mtx);
157 		object1 = vm_object_allocate(OBJT_DEVICE, pindex);
158 		object1->flags |= OBJ_COLORED;
159 		object1->pg_color = atop(paddr) - OFF_TO_IDX(off - PAGE_SIZE);
160 		mtx_lock(&dev_pager_mtx);
161 		object = vm_pager_object_lookup(&dev_pager_object_list, handle);
162 		if (object != NULL) {
163 			/*
164 			 * We raced with other thread while allocating object.
165 			 */
166 			if (pindex > object->size)
167 				object->size = pindex;
168 		} else {
169 			object = object1;
170 			object1 = NULL;
171 			object->handle = handle;
172 			TAILQ_INIT(&object->un_pager.devp.devp_pglist);
173 			TAILQ_INSERT_TAIL(&dev_pager_object_list, object,
174 			    pager_object_list);
175 		}
176 	} else {
177 		if (pindex > object->size)
178 			object->size = pindex;
179 	}
180 	mtx_unlock(&dev_pager_mtx);
181 	dev_relthread(dev, ref);
182 	vm_object_deallocate(object1);
183 	return (object);
184 }
185 
186 static void
187 dev_pager_dealloc(object)
188 	vm_object_t object;
189 {
190 	vm_page_t m;
191 
192 	VM_OBJECT_UNLOCK(object);
193 	mtx_lock(&dev_pager_mtx);
194 	TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
195 	mtx_unlock(&dev_pager_mtx);
196 	VM_OBJECT_LOCK(object);
197 	/*
198 	 * Free up our fake pages.
199 	 */
200 	while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != NULL) {
201 		TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq);
202 		dev_pager_putfake(m);
203 	}
204 }
205 
206 static int
207 dev_pager_getpages(object, m, count, reqpage)
208 	vm_object_t object;
209 	vm_page_t *m;
210 	int count;
211 	int reqpage;
212 {
213 	vm_pindex_t offset;
214 	vm_paddr_t paddr;
215 	vm_page_t m_paddr, page;
216 	vm_memattr_t memattr;
217 	struct cdev *dev;
218 	int i, ref, ret;
219 	struct cdevsw *csw;
220 	struct thread *td;
221 	struct file *fpop;
222 
223 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
224 	dev = object->handle;
225 	page = m[reqpage];
226 	offset = page->pindex;
227 	memattr = object->memattr;
228 	VM_OBJECT_UNLOCK(object);
229 	csw = dev_refthread(dev, &ref);
230 	if (csw == NULL)
231 		panic("dev_pager_getpage: no cdevsw");
232 	td = curthread;
233 	fpop = td->td_fpop;
234 	td->td_fpop = NULL;
235 	ret = csw->d_mmap(dev, (vm_ooffset_t)offset << PAGE_SHIFT, &paddr,
236 	    PROT_READ, &memattr);
237 	KASSERT(ret == 0, ("dev_pager_getpage: map function returns error"));
238 	td->td_fpop = fpop;
239 	dev_relthread(dev, ref);
240 	/* If "paddr" is a real page, perform a sanity check on "memattr". */
241 	if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL &&
242 	    pmap_page_get_memattr(m_paddr) != memattr) {
243 		memattr = pmap_page_get_memattr(m_paddr);
244 		printf(
245 	    "WARNING: A device driver has set \"memattr\" inconsistently.\n");
246 	}
247 	if ((page->flags & PG_FICTITIOUS) != 0) {
248 		/*
249 		 * If the passed in reqpage page is a fake page, update it with
250 		 * the new physical address.
251 		 */
252 		VM_OBJECT_LOCK(object);
253 		dev_pager_updatefake(page, paddr, memattr);
254 		if (count > 1) {
255 
256 			for (i = 0; i < count; i++) {
257 				if (i != reqpage) {
258 					vm_page_lock(m[i]);
259 					vm_page_free(m[i]);
260 					vm_page_unlock(m[i]);
261 				}
262 			}
263 		}
264 	} else {
265 		/*
266 		 * Replace the passed in reqpage page with our own fake page and
267 		 * free up the all of the original pages.
268 		 */
269 		page = dev_pager_getfake(paddr, memattr);
270 		VM_OBJECT_LOCK(object);
271 		TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq);
272 		for (i = 0; i < count; i++) {
273 			vm_page_lock(m[i]);
274 			vm_page_free(m[i]);
275 			vm_page_unlock(m[i]);
276 		}
277 		vm_page_insert(page, object, offset);
278 		m[reqpage] = page;
279 	}
280 	page->valid = VM_PAGE_BITS_ALL;
281 	return (VM_PAGER_OK);
282 }
283 
284 static void
285 dev_pager_putpages(object, m, count, sync, rtvals)
286 	vm_object_t object;
287 	vm_page_t *m;
288 	int count;
289 	boolean_t sync;
290 	int *rtvals;
291 {
292 	panic("dev_pager_putpage called");
293 }
294 
295 static boolean_t
296 dev_pager_haspage(object, pindex, before, after)
297 	vm_object_t object;
298 	vm_pindex_t pindex;
299 	int *before;
300 	int *after;
301 {
302 	if (before != NULL)
303 		*before = 0;
304 	if (after != NULL)
305 		*after = 0;
306 	return (TRUE);
307 }
308 
309 /*
310  * Create a fictitious page with the specified physical address and memory
311  * attribute.  The memory attribute is the only the machine-dependent aspect
312  * of a fictitious page that must be initialized.
313  */
314 static vm_page_t
315 dev_pager_getfake(vm_paddr_t paddr, vm_memattr_t memattr)
316 {
317 	vm_page_t m;
318 
319 	m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO);
320 	m->phys_addr = paddr;
321 	/* Fictitious pages don't use "segind". */
322 	m->flags = PG_FICTITIOUS;
323 	/* Fictitious pages don't use "order" or "pool". */
324 	m->oflags = VPO_BUSY;
325 	m->wire_count = 1;
326 	pmap_page_set_memattr(m, memattr);
327 	return (m);
328 }
329 
330 /*
331  * Release a fictitious page.
332  */
333 static void
334 dev_pager_putfake(vm_page_t m)
335 {
336 
337 	if (!(m->flags & PG_FICTITIOUS))
338 		panic("dev_pager_putfake: bad page");
339 	uma_zfree(fakepg_zone, m);
340 }
341 
342 /*
343  * Update the given fictitious page to the specified physical address and
344  * memory attribute.
345  */
346 static void
347 dev_pager_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
348 {
349 
350 	if (!(m->flags & PG_FICTITIOUS))
351 		panic("dev_pager_updatefake: bad page");
352 	m->phys_addr = paddr;
353 	pmap_page_set_memattr(m, memattr);
354 }
355