]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/vm/device_pager.c
MFC r238980:
[FreeBSD/stable/8.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
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 static __inline int
97 dev_mmap(struct cdevsw *csw, struct cdev *dev, vm_offset_t offset,
98     vm_paddr_t *paddr, int nprot, vm_memattr_t *memattr)
99 {
100
101         if (csw->d_flags & D_MMAP2)
102                 return (csw->d_mmap2(dev, offset, paddr, nprot, memattr));
103         else
104                 return (csw->d_mmap(dev, offset, paddr, nprot));
105 }
106
107 /*
108  * MPSAFE
109  */
110 static vm_object_t
111 dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
112     vm_ooffset_t foff, struct ucred *cred)
113 {
114         struct cdev *dev;
115         vm_object_t object, object1;
116         vm_pindex_t pindex;
117         unsigned int npages;
118         vm_paddr_t paddr;
119         vm_offset_t off;
120         vm_memattr_t dummy;
121         struct cdevsw *csw;
122         int ref;
123
124         /*
125          * Offset should be page aligned.
126          */
127         if (foff & PAGE_MASK)
128                 return (NULL);
129
130         size = round_page(size);
131         pindex = OFF_TO_IDX(foff + size);
132
133         /*
134          * Make sure this device can be mapped.
135          */
136         dev = handle;
137         csw = dev_refthread(dev, &ref);
138         if (csw == NULL)
139                 return (NULL);
140
141         /*
142          * Check that the specified range of the device allows the desired
143          * protection.
144          *
145          * XXX assumes VM_PROT_* == PROT_*
146          */
147         npages = OFF_TO_IDX(size);
148         for (off = foff; npages--; off += PAGE_SIZE)
149                 if (dev_mmap(csw, dev, off, &paddr, (int)prot, &dummy) != 0) {
150                         dev_relthread(dev, ref);
151                         return (NULL);
152                 }
153
154         mtx_lock(&dev_pager_mtx);
155
156         /*
157          * Look up pager, creating as necessary.
158          */
159         object1 = NULL;
160         object = vm_pager_object_lookup(&dev_pager_object_list, handle);
161         if (object == NULL) {
162                 /*
163                  * Allocate object and associate it with the pager.  Initialize
164                  * the object's pg_color based upon the physical address of the
165                  * device's memory.
166                  */
167                 mtx_unlock(&dev_pager_mtx);
168                 object1 = vm_object_allocate(OBJT_DEVICE, pindex);
169                 object1->flags |= OBJ_COLORED;
170                 object1->pg_color = atop(paddr) - OFF_TO_IDX(off - PAGE_SIZE);
171                 TAILQ_INIT(&object1->un_pager.devp.devp_pglist);
172                 mtx_lock(&dev_pager_mtx);
173                 object = vm_pager_object_lookup(&dev_pager_object_list, handle);
174                 if (object != NULL) {
175                         /*
176                          * We raced with other thread while allocating object.
177                          */
178                         if (pindex > object->size)
179                                 object->size = pindex;
180                 } else {
181                         object = object1;
182                         object1 = NULL;
183                         object->handle = handle;
184                         TAILQ_INSERT_TAIL(&dev_pager_object_list, object,
185                             pager_object_list);
186                 }
187         } else {
188                 if (pindex > object->size)
189                         object->size = pindex;
190         }
191         mtx_unlock(&dev_pager_mtx);
192         dev_relthread(dev, ref);
193         if (object1 != NULL) {
194                 object1->handle = object1;
195                 mtx_lock(&dev_pager_mtx);
196                 TAILQ_INSERT_TAIL(&dev_pager_object_list, object1,
197                     pager_object_list);
198                 mtx_unlock(&dev_pager_mtx);
199                 vm_object_deallocate(object1);
200         }
201         return (object);
202 }
203
204 static void
205 dev_pager_dealloc(object)
206         vm_object_t object;
207 {
208         vm_page_t m;
209
210         VM_OBJECT_UNLOCK(object);
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                 TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq);
220                 dev_pager_putfake(m);
221         }
222 }
223
224 static int
225 dev_pager_getpages(object, m, count, reqpage)
226         vm_object_t object;
227         vm_page_t *m;
228         int count;
229         int reqpage;
230 {
231         vm_pindex_t offset;
232         vm_paddr_t paddr;
233         vm_page_t m_paddr, page;
234         vm_memattr_t memattr;
235         struct cdev *dev;
236         int i, ref, ret;
237         struct cdevsw *csw;
238         struct thread *td;
239         struct file *fpop;
240
241         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
242         dev = object->handle;
243         page = m[reqpage];
244         offset = page->pindex;
245         memattr = object->memattr;
246         VM_OBJECT_UNLOCK(object);
247         csw = dev_refthread(dev, &ref);
248         if (csw == NULL)
249                 panic("dev_pager_getpage: no cdevsw");
250         td = curthread;
251         fpop = td->td_fpop;
252         td->td_fpop = NULL;
253         ret = dev_mmap(csw, dev, (vm_offset_t)offset << PAGE_SHIFT, &paddr,
254             PROT_READ, &memattr);
255         KASSERT(ret == 0, ("dev_pager_getpage: map function returns error"));
256         td->td_fpop = fpop;
257         dev_relthread(dev, ref);
258         /* If "paddr" is a real page, perform a sanity check on "memattr". */
259         if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL &&
260             pmap_page_get_memattr(m_paddr) != memattr) {
261                 memattr = pmap_page_get_memattr(m_paddr);
262                 printf(
263             "WARNING: A device driver has set \"memattr\" inconsistently.\n");
264         }
265         if ((page->flags & PG_FICTITIOUS) != 0) {
266                 /*
267                  * If the passed in reqpage page is a fake page, update it with
268                  * the new physical address.
269                  */
270                 VM_OBJECT_LOCK(object);
271                 dev_pager_updatefake(page, paddr, memattr);
272                 if (count > 1) {
273                         vm_page_lock_queues();
274                         for (i = 0; i < count; i++) {
275                                 if (i != reqpage)
276                                         vm_page_free(m[i]);
277                         }
278                         vm_page_unlock_queues();
279                 }
280         } else {
281                 /*
282                  * Replace the passed in reqpage page with our own fake page and
283                  * free up the all of the original pages.
284                  */
285                 page = dev_pager_getfake(paddr, memattr);
286                 VM_OBJECT_LOCK(object);
287                 TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq);
288                 vm_page_lock_queues();
289                 for (i = 0; i < count; i++)
290                         vm_page_free(m[i]);
291                 vm_page_unlock_queues();
292                 vm_page_insert(page, object, offset);
293                 m[reqpage] = page;
294         }
295         page->valid = VM_PAGE_BITS_ALL;
296         return (VM_PAGER_OK);
297 }
298
299 static void
300 dev_pager_putpages(object, m, count, sync, rtvals)
301         vm_object_t object;
302         vm_page_t *m;
303         int count;
304         boolean_t sync;
305         int *rtvals;
306 {
307         panic("dev_pager_putpage called");
308 }
309
310 static boolean_t
311 dev_pager_haspage(object, pindex, before, after)
312         vm_object_t object;
313         vm_pindex_t pindex;
314         int *before;
315         int *after;
316 {
317         if (before != NULL)
318                 *before = 0;
319         if (after != NULL)
320                 *after = 0;
321         return (TRUE);
322 }
323
324 /*
325  * Create a fictitious page with the specified physical address and memory
326  * attribute.  The memory attribute is the only the machine-dependent aspect
327  * of a fictitious page that must be initialized.
328  */
329 static vm_page_t
330 dev_pager_getfake(vm_paddr_t paddr, vm_memattr_t memattr)
331 {
332         vm_page_t m;
333
334         m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO);
335         m->phys_addr = paddr;
336         /* Fictitious pages don't use "segind". */
337         m->flags = PG_FICTITIOUS;
338         /* Fictitious pages don't use "order" or "pool". */
339         m->oflags = VPO_BUSY;
340         m->wire_count = 1;
341         pmap_page_set_memattr(m, memattr);
342         return (m);
343 }
344
345 /*
346  * Release a fictitious page.
347  */
348 static void
349 dev_pager_putfake(vm_page_t m)
350 {
351
352         if (!(m->flags & PG_FICTITIOUS))
353                 panic("dev_pager_putfake: bad page");
354         uma_zfree(fakepg_zone, m);
355 }
356
357 /*
358  * Update the given fictitious page to the specified physical address and
359  * memory attribute.
360  */
361 static void
362 dev_pager_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
363 {
364
365         if (!(m->flags & PG_FICTITIOUS))
366                 panic("dev_pager_updatefake: bad page");
367         m->phys_addr = paddr;
368         pmap_page_set_memattr(m, memattr);
369 }