]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/vm/device_pager.c
MFC r235366:
[FreeBSD/stable/9.git] / sys / vm / device_pager.c
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 struct pagerops devicepagerops = {
71         .pgo_init =     dev_pager_init,
72         .pgo_alloc =    dev_pager_alloc,
73         .pgo_dealloc =  dev_pager_dealloc,
74         .pgo_getpages = dev_pager_getpages,
75         .pgo_putpages = dev_pager_putpages,
76         .pgo_haspage =  dev_pager_haspage,
77 };
78
79 static int old_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
80     vm_ooffset_t foff, struct ucred *cred, u_short *color);
81 static void old_dev_pager_dtor(void *handle);
82 static int old_dev_pager_fault(vm_object_t object, vm_ooffset_t offset,
83     int prot, vm_page_t *mres);
84
85 static struct cdev_pager_ops old_dev_pager_ops = {
86         .cdev_pg_ctor = old_dev_pager_ctor,
87         .cdev_pg_dtor = old_dev_pager_dtor,
88         .cdev_pg_fault = old_dev_pager_fault
89 };
90
91 static void
92 dev_pager_init()
93 {
94         TAILQ_INIT(&dev_pager_object_list);
95         mtx_init(&dev_pager_mtx, "dev_pager list", NULL, MTX_DEF);
96 }
97
98 vm_object_t
99 cdev_pager_lookup(void *handle)
100 {
101         vm_object_t object;
102
103         mtx_lock(&dev_pager_mtx);
104         object = vm_pager_object_lookup(&dev_pager_object_list, handle);
105         mtx_unlock(&dev_pager_mtx);
106         return (object);
107 }
108
109 vm_object_t
110 cdev_pager_allocate(void *handle, enum obj_type tp, struct cdev_pager_ops *ops,
111     vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff, struct ucred *cred)
112 {
113         vm_object_t object, object1;
114         vm_pindex_t pindex;
115         u_short color;
116
117         if (tp != OBJT_DEVICE)
118                 return (NULL);
119
120         /*
121          * Offset should be page aligned.
122          */
123         if (foff & PAGE_MASK)
124                 return (NULL);
125
126         size = round_page(size);
127         pindex = OFF_TO_IDX(foff + size);
128
129         if (ops->cdev_pg_ctor(handle, size, prot, foff, cred, &color) != 0)
130                 return (NULL);
131         mtx_lock(&dev_pager_mtx);
132
133         /*
134          * Look up pager, creating as necessary.
135          */
136         object1 = NULL;
137         object = vm_pager_object_lookup(&dev_pager_object_list, handle);
138         if (object == NULL) {
139                 /*
140                  * Allocate object and associate it with the pager.  Initialize
141                  * the object's pg_color based upon the physical address of the
142                  * device's memory.
143                  */
144                 mtx_unlock(&dev_pager_mtx);
145                 object1 = vm_object_allocate(tp, pindex);
146                 object1->flags |= OBJ_COLORED;
147                 object1->pg_color = color;
148                 object1->handle = handle;
149                 object1->un_pager.devp.ops = ops;
150                 TAILQ_INIT(&object1->un_pager.devp.devp_pglist);
151                 mtx_lock(&dev_pager_mtx);
152                 object = vm_pager_object_lookup(&dev_pager_object_list, handle);
153                 if (object != NULL) {
154                         /*
155                          * We raced with other thread while allocating object.
156                          */
157                         if (pindex > object->size)
158                                 object->size = pindex;
159                 } else {
160                         object = object1;
161                         object1 = NULL;
162                         object->handle = handle;
163                         TAILQ_INSERT_TAIL(&dev_pager_object_list, object,
164                             pager_object_list);
165                         KASSERT(object->type == tp,
166                 ("Inconsistent device pager type %p %d", object, tp));
167                 }
168         } else {
169                 if (pindex > object->size)
170                         object->size = pindex;
171         }
172         mtx_unlock(&dev_pager_mtx);
173         if (object1 != NULL) {
174                 object1->handle = object1;
175                 mtx_lock(&dev_pager_mtx);
176                 TAILQ_INSERT_TAIL(&dev_pager_object_list, object1,
177                     pager_object_list);
178                 mtx_unlock(&dev_pager_mtx);
179                 vm_object_deallocate(object1);
180         }
181         return (object);
182 }
183
184 static vm_object_t
185 dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
186     vm_ooffset_t foff, struct ucred *cred)
187 {
188
189         return (cdev_pager_allocate(handle, OBJT_DEVICE, &old_dev_pager_ops,
190             size, prot, foff, cred));
191 }
192
193 void
194 cdev_pager_free_page(vm_object_t object, vm_page_t m)
195 {
196
197         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
198         TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq);
199         vm_page_putfake(m);
200 }
201
202 static void
203 dev_pager_dealloc(object)
204         vm_object_t object;
205 {
206         vm_page_t m;
207
208         VM_OBJECT_UNLOCK(object);
209         object->un_pager.devp.ops->cdev_pg_dtor(object->handle);
210
211         mtx_lock(&dev_pager_mtx);
212         TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
213         mtx_unlock(&dev_pager_mtx);
214         VM_OBJECT_LOCK(object);
215         /*
216          * Free up our fake pages.
217          */
218         while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != NULL)
219                 cdev_pager_free_page(object, m);
220 }
221
222 static int
223 dev_pager_getpages(vm_object_t object, vm_page_t *ma, int count, int reqpage)
224 {
225         int error, i;
226
227         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
228         error = object->un_pager.devp.ops->cdev_pg_fault(object,
229             IDX_TO_OFF(ma[reqpage]->pindex), PROT_READ, &ma[reqpage]);
230
231         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
232
233         for (i = 0; i < count; i++) {
234                 if (i != reqpage) {
235                         vm_page_lock(ma[i]);
236                         vm_page_free(ma[i]);
237                         vm_page_unlock(ma[i]);
238                 }
239         }
240
241         if (error == VM_PAGER_OK) {
242                 TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist,
243                     ma[reqpage], pageq);
244         }
245
246         return (error);
247 }
248
249 static int
250 old_dev_pager_fault(vm_object_t object, vm_ooffset_t offset, int prot,
251     vm_page_t *mres)
252 {
253         vm_pindex_t pidx;
254         vm_paddr_t paddr;
255         vm_page_t m_paddr, page;
256         struct cdev *dev;
257         struct cdevsw *csw;
258         struct file *fpop;
259         struct thread *td;
260         vm_memattr_t memattr;
261         int ref, ret;
262
263         pidx = OFF_TO_IDX(offset);
264         memattr = object->memattr;
265
266         VM_OBJECT_UNLOCK(object);
267
268         dev = object->handle;
269         csw = dev_refthread(dev, &ref);
270         if (csw == NULL) {
271                 VM_OBJECT_LOCK(object);
272                 return (VM_PAGER_FAIL);
273         }
274         td = curthread;
275         fpop = td->td_fpop;
276         td->td_fpop = NULL;
277         ret = csw->d_mmap(dev, offset, &paddr, prot, &memattr);
278         td->td_fpop = fpop;
279         dev_relthread(dev, ref);
280         if (ret != 0) {
281                 printf(
282             "WARNING: dev_pager_getpage: map function returns error %d", ret);
283                 VM_OBJECT_LOCK(object);
284                 return (VM_PAGER_FAIL);
285         }
286
287         /* If "paddr" is a real page, perform a sanity check on "memattr". */
288         if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL &&
289             pmap_page_get_memattr(m_paddr) != memattr) {
290                 memattr = pmap_page_get_memattr(m_paddr);
291                 printf(
292             "WARNING: A device driver has set \"memattr\" inconsistently.\n");
293         }
294         if (((*mres)->flags & PG_FICTITIOUS) != 0) {
295                 /*
296                  * If the passed in result page is a fake page, update it with
297                  * the new physical address.
298                  */
299                 page = *mres;
300                 VM_OBJECT_LOCK(object);
301                 vm_page_updatefake(page, paddr, memattr);
302         } else {
303                 /*
304                  * Replace the passed in reqpage page with our own fake page and
305                  * free up the all of the original pages.
306                  */
307                 page = vm_page_getfake(paddr, memattr);
308                 VM_OBJECT_LOCK(object);
309                 vm_page_lock(*mres);
310                 vm_page_free(*mres);
311                 vm_page_unlock(*mres);
312                 *mres = page;
313                 vm_page_insert(page, object, pidx);
314         }
315         page->valid = VM_PAGE_BITS_ALL;
316         return (VM_PAGER_OK);
317 }
318
319 static void
320 dev_pager_putpages(object, m, count, sync, rtvals)
321         vm_object_t object;
322         vm_page_t *m;
323         int count;
324         boolean_t sync;
325         int *rtvals;
326 {
327
328         panic("dev_pager_putpage called");
329 }
330
331 static boolean_t
332 dev_pager_haspage(object, pindex, before, after)
333         vm_object_t object;
334         vm_pindex_t pindex;
335         int *before;
336         int *after;
337 {
338         if (before != NULL)
339                 *before = 0;
340         if (after != NULL)
341                 *after = 0;
342         return (TRUE);
343 }
344
345 static int
346 old_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
347     vm_ooffset_t foff, struct ucred *cred, u_short *color)
348 {
349         struct cdev *dev;
350         struct cdevsw *csw;
351         vm_memattr_t dummy;
352         vm_ooffset_t off;
353         vm_paddr_t paddr;
354         unsigned int npages;
355         int ref;
356
357         /*
358          * Make sure this device can be mapped.
359          */
360         dev = handle;
361         csw = dev_refthread(dev, &ref);
362         if (csw == NULL)
363                 return (ENXIO);
364
365         /*
366          * Check that the specified range of the device allows the desired
367          * protection.
368          *
369          * XXX assumes VM_PROT_* == PROT_*
370          */
371         npages = OFF_TO_IDX(size);
372         for (off = foff; npages--; off += PAGE_SIZE) {
373                 if (csw->d_mmap(dev, off, &paddr, (int)prot, &dummy) != 0) {
374                         dev_relthread(dev, ref);
375                         return (EINVAL);
376                 }
377         }
378
379         dev_ref(dev);
380         dev_relthread(dev, ref);
381         *color = atop(paddr) - OFF_TO_IDX(off - PAGE_SIZE);
382         return (0);
383 }
384
385 static void
386 old_dev_pager_dtor(void *handle)
387 {
388
389         dev_rel(handle);
390 }