Correct BSD License clause numbering from 1-2-4 to 1-2-3.
[dragonfly.git] / sys / vm / device_pager.c
1 /*
2  * (MPSAFE)
3  *
4  * Copyright (c) 1990 University of Utah.
5  * Copyright (c) 1991, 1993
6  *      The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * the Systems Programming Group of the University of Utah Computer
10  * Science Department.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *      @(#)device_pager.c      8.1 (Berkeley) 6/11/93
37  * $FreeBSD: src/sys/vm/device_pager.c,v 1.46.2.1 2000/08/02 21:54:37 peter Exp $
38  */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/conf.h>
44 #include <sys/mman.h>
45 #include <sys/device.h>
46 #include <sys/queue.h>
47 #include <sys/malloc.h>
48 #include <sys/thread2.h>
49 #include <sys/mutex2.h>
50
51 #include <vm/vm.h>
52 #include <vm/vm_object.h>
53 #include <vm/vm_page.h>
54 #include <vm/vm_pager.h>
55 #include <vm/vm_zone.h>
56
57 static void dev_pager_dealloc (vm_object_t);
58 static int dev_pager_getpage (vm_object_t, vm_page_t *, int);
59 static void dev_pager_putpages (vm_object_t, vm_page_t *, int, 
60                 boolean_t, int *);
61 static boolean_t dev_pager_haspage (vm_object_t, vm_pindex_t);
62
63 /* list of device pager objects */
64 static TAILQ_HEAD(, vm_page) dev_freepages_list =
65                 TAILQ_HEAD_INITIALIZER(dev_freepages_list);
66 static MALLOC_DEFINE(M_FICTITIOUS_PAGES, "device-mapped pages",
67                 "Device mapped pages");
68
69 static vm_page_t dev_pager_getfake (vm_paddr_t);
70 static void dev_pager_putfake (vm_page_t);
71
72 struct pagerops devicepagerops = {
73         dev_pager_dealloc,
74         dev_pager_getpage,
75         dev_pager_putpages,
76         dev_pager_haspage
77 };
78
79 static struct mtx dev_pager_mtx = MTX_INITIALIZER;
80
81 /*
82  * No requirements.
83  */
84 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_page64(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          * Look up pager, creating as necessary.
119          */
120         mtx_lock(&dev_pager_mtx);
121         object = dev->si_object;
122         if (object == NULL) {
123                 /*
124                  * Allocate object and associate it with the pager.
125                  */
126                 object = vm_object_allocate_hold(OBJT_DEVICE,
127                                                  OFF_TO_IDX(foff + size));
128                 object->handle = handle;
129                 TAILQ_INIT(&object->un_pager.devp.devp_pglist);
130                 dev->si_object = object;
131                 vm_object_drop(object);
132         } else {
133                 /*
134                  * Gain a reference to the object.
135                  */
136                 vm_object_hold(object);
137                 vm_object_reference_locked(object);
138                 if (OFF_TO_IDX(foff + size) > object->size)
139                         object->size = OFF_TO_IDX(foff + size);
140                 vm_object_drop(object);
141         }
142         mtx_unlock(&dev_pager_mtx);
143
144         return (object);
145 }
146
147 /*
148  * No requirements.
149  */
150 static void
151 dev_pager_dealloc(vm_object_t object)
152 {
153         vm_page_t m;
154         cdev_t dev;
155
156         mtx_lock(&dev_pager_mtx);
157
158         if ((dev = object->handle) != NULL) {
159                 KKASSERT(dev->si_object);
160                 dev->si_object = NULL;
161         }
162         KKASSERT(object->swblock_count == 0);
163
164         /*
165          * Free up our fake pages.
166          */
167         while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != 0) {
168                 TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq);
169                 dev_pager_putfake(m);
170         }
171         mtx_unlock(&dev_pager_mtx);
172 }
173
174 /*
175  * No requirements.
176  */
177 static int
178 dev_pager_getpage(vm_object_t object, vm_page_t *mpp, int seqaccess)
179 {
180         vm_offset_t offset;
181         vm_paddr_t paddr;
182         vm_page_t page;
183         cdev_t dev;
184         int prot;
185
186         mtx_lock(&dev_pager_mtx);
187
188         page = *mpp;
189         dev = object->handle;
190         offset = page->pindex;
191         prot = PROT_READ;       /* XXX should pass in? */
192
193         paddr = pmap_phys_address(
194                     dev_dmmap(dev, offset << PAGE_SHIFT, prot));
195         KASSERT(paddr != -1,("dev_pager_getpage: map function returns error"));
196
197         if (page->flags & PG_FICTITIOUS) {
198                 /*
199                  * If the passed in reqpage page is a fake page, update it
200                  * with the new physical address.
201                  */
202                 page->phys_addr = paddr;
203                 page->valid = VM_PAGE_BITS_ALL;
204         } else {
205                 /*
206                  * Replace the passed in reqpage page with our own fake page
207                  * and free up all the original pages.
208                  */
209                 page = dev_pager_getfake(paddr);
210                 TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist,
211                                   page, pageq);
212                 vm_object_hold(object);
213                 vm_page_free(*mpp);
214                 if (vm_page_insert(page, object, offset) == FALSE) {
215                         panic("dev_pager_getpage: page (%p,%ld) exists",
216                               object, offset);
217                 }
218                 vm_object_drop(object);
219         }
220         mtx_unlock(&dev_pager_mtx);
221         return (VM_PAGER_OK);
222 }
223
224 /*
225  * No requirements.
226  */
227 static void
228 dev_pager_putpages(vm_object_t object, vm_page_t *m,
229                    int count, boolean_t sync, int *rtvals)
230 {
231         panic("dev_pager_putpage called");
232 }
233
234 /*
235  * No requirements.
236  */
237 static boolean_t
238 dev_pager_haspage(vm_object_t object, vm_pindex_t pindex)
239 {
240         return (TRUE);
241 }
242
243 /*
244  * The caller must hold dev_pager_mtx
245  */
246 static vm_page_t
247 dev_pager_getfake(vm_paddr_t paddr)
248 {
249         vm_page_t m;
250
251         if ((m = TAILQ_FIRST(&dev_freepages_list)) != NULL) {
252                 TAILQ_REMOVE(&dev_freepages_list, m, pageq);
253         } else {
254                 m = kmalloc(sizeof(*m), M_FICTITIOUS_PAGES, M_WAITOK);
255         }
256         bzero(m, sizeof(*m));
257
258         m->flags = PG_BUSY | PG_FICTITIOUS;
259         m->valid = VM_PAGE_BITS_ALL;
260         m->dirty = 0;
261         m->busy = 0;
262         m->queue = PQ_NONE;
263         m->object = NULL;
264
265         m->wire_count = 1;
266         m->hold_count = 0;
267         m->phys_addr = paddr;
268
269         return (m);
270 }
271
272 /*
273  * Synthesized VM pages must be structurally stable for lockless lookups to
274  * work properly.
275  *
276  * The caller must hold dev_pager_mtx
277  */
278 static void
279 dev_pager_putfake(vm_page_t m)
280 {
281         if (!(m->flags & PG_FICTITIOUS))
282                 panic("dev_pager_putfake: bad page");
283         KKASSERT(m->object == NULL);
284         TAILQ_INSERT_HEAD(&dev_freepages_list, m, pageq);
285 }
286