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