Regen.
[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.9 2004/11/12 00:09:56 dillon Exp $
41  */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/conf.h>
46 #include <sys/mman.h>
47 #include <sys/device.h>
48 #include <sys/thread2.h>
49
50 #include <vm/vm.h>
51 #include <vm/vm_object.h>
52 #include <vm/vm_page.h>
53 #include <vm/vm_pager.h>
54 #include <vm/vm_zone.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);
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
66 /* list of device pager objects */
67 static struct pagerlst dev_pager_object_list;
68
69 static vm_zone_t fakepg_zone;
70 static struct vm_zone fakepg_zone_store;
71
72 static vm_page_t dev_pager_getfake (vm_paddr_t);
73 static void dev_pager_putfake (vm_page_t);
74
75 static int dev_pager_alloc_lock, dev_pager_alloc_lock_want;
76
77 struct pagerops devicepagerops = {
78         dev_pager_init,
79         dev_pager_alloc,
80         dev_pager_dealloc,
81         dev_pager_getpages,
82         dev_pager_putpages,
83         dev_pager_haspage,
84         NULL
85 };
86
87 static void
88 dev_pager_init(void)
89 {
90         TAILQ_INIT(&dev_pager_object_list);
91         fakepg_zone = &fakepg_zone_store;
92         zinitna(fakepg_zone, NULL, "DP fakepg", sizeof(struct vm_page), 0, 0, 2);
93 }
94
95 static vm_object_t
96 dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff)
97 {
98         dev_t dev;
99         vm_object_t object;
100         unsigned int npages;
101         vm_offset_t off;
102
103         /*
104          * Make sure this device can be mapped.
105          */
106         dev = handle;
107
108         /*
109          * Offset should be page aligned.
110          */
111         if (foff & PAGE_MASK)
112                 return (NULL);
113
114         size = round_page(size);
115
116         /*
117          * Check that the specified range of the device allows the desired
118          * protection.
119          *
120          * XXX assumes VM_PROT_* == PROT_*
121          */
122         npages = OFF_TO_IDX(size);
123         for (off = foff; npages--; off += PAGE_SIZE) {
124                 if (dev_dmmap(dev, off, (int)prot) == -1)
125                         return (NULL);
126         }
127
128         /*
129          * Lock to prevent object creation race condition.
130          */
131         while (dev_pager_alloc_lock) {
132                 dev_pager_alloc_lock_want++;
133                 tsleep(&dev_pager_alloc_lock, 0, "dvpall", 0);
134                 dev_pager_alloc_lock_want--;
135         }
136         dev_pager_alloc_lock = 1;
137
138         /*
139          * Look up pager, creating as necessary.
140          */
141         object = vm_pager_object_lookup(&dev_pager_object_list, handle);
142         if (object == NULL) {
143                 /*
144                  * Allocate object and associate it with the pager.
145                  */
146                 object = vm_object_allocate(OBJT_DEVICE,
147                         OFF_TO_IDX(foff + size));
148                 object->handle = handle;
149                 TAILQ_INIT(&object->un_pager.devp.devp_pglist);
150                 TAILQ_INSERT_TAIL(&dev_pager_object_list, object, pager_object_list);
151         } else {
152                 /*
153                  * Gain a reference to the object.
154                  */
155                 vm_object_reference(object);
156                 if (OFF_TO_IDX(foff + size) > object->size)
157                         object->size = OFF_TO_IDX(foff + size);
158         }
159
160         dev_pager_alloc_lock = 0;
161         if (dev_pager_alloc_lock_want)
162                 wakeup(&dev_pager_alloc_lock);
163
164         return (object);
165 }
166
167 static void
168 dev_pager_dealloc(vm_object_t object)
169 {
170         vm_page_t m;
171
172         TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
173         /*
174          * Free up our fake pages.
175          */
176         while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != 0) {
177                 TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq);
178                 dev_pager_putfake(m);
179         }
180 }
181
182 static int
183 dev_pager_getpages(vm_object_t object, vm_page_t *m, int count, int reqpage)
184 {
185         vm_offset_t offset;
186         vm_paddr_t paddr;
187         vm_page_t page;
188         dev_t dev;
189         int prot;
190         int i;
191
192         dev = object->handle;
193         offset = m[reqpage]->pindex;
194         prot = PROT_READ;       /* XXX should pass in? */
195
196         paddr = pmap_phys_address(dev_dmmap(dev, (vm_offset_t) offset << PAGE_SHIFT, prot));
197         KASSERT(paddr != -1,("dev_pager_getpage: map function returns error"));
198
199         if (m[reqpage]->flags & PG_FICTITIOUS) {
200                 /*
201                  * If the passed in reqpage page is a fake page, update it
202                  * with the new physical address.
203                  */
204                 m[reqpage]->phys_addr = paddr;
205         } else {
206                 /*
207                  * Replace the passed in reqpage page with our own fake page
208                  * and free up all the original pages.
209                  */
210                 page = dev_pager_getfake(paddr);
211                 TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq);
212                 crit_enter();
213                 vm_page_free(m[reqpage]);
214                 vm_page_insert(page, object, offset);
215                 crit_exit();
216         }
217         for (i = 0; i < count; i++) {
218                 if (i != reqpage)
219                         vm_page_free(m[i]);
220         }
221         return (VM_PAGER_OK);
222 }
223
224 static void
225 dev_pager_putpages(vm_object_t object, vm_page_t *m, int count, boolean_t sync,
226     int *rtvals)
227 {
228         panic("dev_pager_putpage called");
229 }
230
231 static boolean_t
232 dev_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
233     int *after)
234 {
235         if (before != NULL)
236                 *before = 0;
237         if (after != NULL)
238                 *after = 0;
239         return (TRUE);
240 }
241
242 static vm_page_t
243 dev_pager_getfake(vm_paddr_t paddr)
244 {
245         vm_page_t m;
246
247         m = zalloc(fakepg_zone);
248
249         m->flags = PG_BUSY | PG_FICTITIOUS;
250         m->valid = VM_PAGE_BITS_ALL;
251         m->dirty = 0;
252         m->busy = 0;
253         m->queue = PQ_NONE;
254         m->object = NULL;
255
256         m->wire_count = 1;
257         m->hold_count = 0;
258         m->phys_addr = paddr;
259
260         return (m);
261 }
262
263 static void
264 dev_pager_putfake(vm_page_t m)
265 {
266         if (!(m->flags & PG_FICTITIOUS))
267                 panic("dev_pager_putfake: bad page");
268         KKASSERT(m->object == NULL);
269         zfree(fakepg_zone, m);
270 }
271