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