kernel - SWAP CACHE part 3/many - Rearrange VM pagerops
[dragonfly.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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      @(#)device_pager.c      8.1 (Berkeley) 6/11/93
39  * $FreeBSD: src/sys/vm/device_pager.c,v 1.46.2.1 2000/08/02 21:54:37 peter Exp $
40  * $DragonFly: src/sys/vm/device_pager.c,v 1.13 2008/01/21 10:25:18 corecode Exp $
41  */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/conf.h>
47 #include <sys/mman.h>
48 #include <sys/device.h>
49 #include <sys/queue.h>
50 #include <sys/malloc.h>
51 #include <sys/thread2.h>
52
53 #include <vm/vm.h>
54 #include <vm/vm_object.h>
55 #include <vm/vm_page.h>
56 #include <vm/vm_pager.h>
57 #include <vm/vm_zone.h>
58
59 static vm_object_t dev_pager_alloc (void *, off_t, vm_prot_t, off_t);
60 static void dev_pager_dealloc (vm_object_t);
61 static int dev_pager_getpage (vm_object_t, vm_page_t *, int);
62 static void dev_pager_putpages (vm_object_t, vm_page_t *, int, 
63                 boolean_t, int *);
64 static boolean_t dev_pager_haspage (vm_object_t, vm_pindex_t);
65
66 /* list of device pager objects */
67 static TAILQ_HEAD(, vm_page) dev_freepages_list =
68                 TAILQ_HEAD_INITIALIZER(dev_freepages_list);
69 static MALLOC_DEFINE(M_FICTITIOUS_PAGES, "device-mapped pages", "Device mapped pages");
70
71 static vm_page_t dev_pager_getfake (vm_paddr_t);
72 static void dev_pager_putfake (vm_page_t);
73
74 static int dev_pager_alloc_lock, dev_pager_alloc_lock_want;
75
76 struct pagerops devicepagerops = {
77         dev_pager_alloc,
78         dev_pager_dealloc,
79         dev_pager_getpage,
80         dev_pager_putpages,
81         dev_pager_haspage
82 };
83
84 static vm_object_t
85 dev_pager_alloc(void *handle, off_t size, vm_prot_t prot, off_t foff)
86 {
87         cdev_t dev;
88         vm_object_t object;
89         unsigned int npages;
90         vm_offset_t off;
91
92         /*
93          * Make sure this device can be mapped.
94          */
95         dev = handle;
96
97         /*
98          * Offset should be page aligned.
99          */
100         if (foff & PAGE_MASK)
101                 return (NULL);
102
103         size = round_page(size);
104
105         /*
106          * Check that the specified range of the device allows the desired
107          * protection.
108          *
109          * XXX assumes VM_PROT_* == PROT_*
110          */
111         npages = OFF_TO_IDX(size);
112         for (off = foff; npages--; off += PAGE_SIZE) {
113                 if (dev_dmmap(dev, off, (int)prot) == -1)
114                         return (NULL);
115         }
116
117         /*
118          * Lock to prevent object creation race condition.
119          */
120         while (dev_pager_alloc_lock) {
121                 dev_pager_alloc_lock_want++;
122                 tsleep(&dev_pager_alloc_lock, 0, "dvpall", 0);
123                 dev_pager_alloc_lock_want--;
124         }
125         dev_pager_alloc_lock = 1;
126
127         /*
128          * Look up pager, creating as necessary.
129          */
130         object = dev->si_object;
131         if (object == NULL) {
132                 /*
133                  * Allocate object and associate it with the pager.
134                  */
135                 object = vm_object_allocate(OBJT_DEVICE,
136                         OFF_TO_IDX(foff + size));
137                 object->handle = handle;
138                 TAILQ_INIT(&object->un_pager.devp.devp_pglist);
139                 dev->si_object = object;
140         } else {
141                 /*
142                  * Gain a reference to the object.
143                  */
144                 vm_object_reference(object);
145                 if (OFF_TO_IDX(foff + size) > object->size)
146                         object->size = OFF_TO_IDX(foff + size);
147         }
148
149         dev_pager_alloc_lock = 0;
150         if (dev_pager_alloc_lock_want)
151                 wakeup(&dev_pager_alloc_lock);
152
153         return (object);
154 }
155
156 static void
157 dev_pager_dealloc(vm_object_t object)
158 {
159         vm_page_t m;
160         cdev_t dev;
161
162         if ((dev = object->handle) != NULL) {
163                 KKASSERT(dev->si_object);
164                 dev->si_object = NULL;
165         }
166
167         /*
168          * Free up our fake pages.
169          */
170         while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != 0) {
171                 TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq);
172                 dev_pager_putfake(m);
173         }
174 }
175
176 static int
177 dev_pager_getpage(vm_object_t object, vm_page_t *mpp, int seqaccess)
178 {
179         vm_offset_t offset;
180         vm_paddr_t paddr;
181         vm_page_t page;
182         cdev_t dev;
183         int prot;
184
185         page = *mpp;
186         dev = object->handle;
187         offset = page->pindex;
188         prot = PROT_READ;       /* XXX should pass in? */
189
190         paddr = pmap_phys_address(
191                     dev_dmmap(dev, (vm_offset_t)offset << PAGE_SHIFT, prot));
192         KASSERT(paddr != -1,("dev_pager_getpage: map function returns error"));
193
194         if (page->flags & PG_FICTITIOUS) {
195                 /*
196                  * If the passed in reqpage page is a fake page, update it
197                  * with the new physical address.
198                  */
199                 page->phys_addr = paddr;
200                 page->valid = VM_PAGE_BITS_ALL;
201         } else {
202                 /*
203                  * Replace the passed in reqpage page with our own fake page
204                  * and free up all the original pages.
205                  */
206                 page = dev_pager_getfake(paddr);
207                 TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq);
208                 crit_enter();
209                 vm_page_free(*mpp);
210                 vm_page_insert(page, object, offset);
211                 crit_exit();
212         }
213         return (VM_PAGER_OK);
214 }
215
216 static void
217 dev_pager_putpages(vm_object_t object, vm_page_t *m,
218                    int count, boolean_t sync, int *rtvals)
219 {
220         panic("dev_pager_putpage called");
221 }
222
223 static boolean_t
224 dev_pager_haspage(vm_object_t object, vm_pindex_t pindex)
225 {
226         return (TRUE);
227 }
228
229 static vm_page_t
230 dev_pager_getfake(vm_paddr_t paddr)
231 {
232         vm_page_t m;
233
234         if ((m = TAILQ_FIRST(&dev_freepages_list)) != NULL) {
235                 TAILQ_REMOVE(&dev_freepages_list, m, pageq);
236         } else {
237                 m = kmalloc(sizeof(*m), M_FICTITIOUS_PAGES, M_WAITOK);
238         }
239         bzero(m, sizeof(*m));
240
241         m->flags = PG_BUSY | PG_FICTITIOUS;
242         m->valid = VM_PAGE_BITS_ALL;
243         m->dirty = 0;
244         m->busy = 0;
245         m->queue = PQ_NONE;
246         m->object = NULL;
247
248         m->wire_count = 1;
249         m->hold_count = 0;
250         m->phys_addr = paddr;
251
252         return (m);
253 }
254
255 /*
256  * Synthesized VM pages must be structurally stable for lockless lookups to
257  * work properly.
258  */
259 static void
260 dev_pager_putfake(vm_page_t m)
261 {
262         if (!(m->flags & PG_FICTITIOUS))
263                 panic("dev_pager_putfake: bad page");
264         KKASSERT(m->object == NULL);
265         TAILQ_INSERT_HEAD(&dev_freepages_list, m, pageq);
266 }
267