FreeBSD/Linux Kernel Cross Reference
sys/vm/sg_pager.c
1 /*-
2 * Copyright (c) 2009 Advanced Computing Technologies LLC
3 * Written by: John H. Baldwin <jhb@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD: releng/7.3/sys/vm/sg_pager.c 197197 2009-09-14 17:34:49Z jhb $");
30
31 /*
32 * This pager manages OBJT_SG objects. These objects are backed by
33 * a scatter/gather list of physical address ranges.
34 */
35
36 #include <sys/param.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #include <sys/sglist.h>
40 #include <sys/systm.h>
41 #include <vm/vm.h>
42 #include <vm/vm_object.h>
43 #include <vm/vm_page.h>
44 #include <vm/vm_pager.h>
45 #include <vm/uma.h>
46
47 static void sg_pager_init(void);
48 static vm_object_t sg_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
49 vm_ooffset_t);
50 static void sg_pager_dealloc(vm_object_t);
51 static int sg_pager_getpages(vm_object_t, vm_page_t *, int, int);
52 static void sg_pager_putpages(vm_object_t, vm_page_t *, int,
53 boolean_t, int *);
54 static boolean_t sg_pager_haspage(vm_object_t, vm_pindex_t, int *,
55 int *);
56
57 static uma_zone_t fakepg_zone;
58
59 static vm_page_t sg_pager_getfake(vm_paddr_t, vm_memattr_t);
60 static void sg_pager_putfake(vm_page_t);
61
62 struct pagerops sgpagerops = {
63 .pgo_init = sg_pager_init,
64 .pgo_alloc = sg_pager_alloc,
65 .pgo_dealloc = sg_pager_dealloc,
66 .pgo_getpages = sg_pager_getpages,
67 .pgo_putpages = sg_pager_putpages,
68 .pgo_haspage = sg_pager_haspage,
69 };
70
71 static void
72 sg_pager_init(void)
73 {
74
75 fakepg_zone = uma_zcreate("SG fakepg", sizeof(struct vm_page),
76 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
77 UMA_ZONE_NOFREE|UMA_ZONE_VM);
78 }
79
80 static vm_object_t
81 sg_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
82 vm_ooffset_t foff)
83 {
84 struct sglist *sg;
85 vm_object_t object;
86 vm_pindex_t npages, pindex;
87 int i;
88
89 /*
90 * Offset should be page aligned.
91 */
92 if (foff & PAGE_MASK)
93 return (NULL);
94
95 /*
96 * The scatter/gather list must only include page-aligned
97 * ranges.
98 */
99 npages = 0;
100 sg = handle;
101 for (i = 0; i < sg->sg_nseg; i++) {
102 if ((sg->sg_segs[i].ss_paddr % PAGE_SIZE) != 0 ||
103 (sg->sg_segs[i].ss_len % PAGE_SIZE) != 0)
104 return (NULL);
105 npages += sg->sg_segs[i].ss_len / PAGE_SIZE;
106 }
107
108 /*
109 * The scatter/gather list has a fixed size. Refuse requests
110 * to map beyond that.
111 */
112 size = round_page(size);
113 pindex = OFF_TO_IDX(foff + size);
114 if (pindex > npages)
115 return (NULL);
116
117 /*
118 * Allocate a new object and associate it with the
119 * scatter/gather list. It is ok for our purposes to have
120 * multiple VM objects associated with the same scatter/gather
121 * list because scatter/gather lists are static. This is also
122 * simpler than ensuring a unique object per scatter/gather
123 * list.
124 */
125 object = vm_object_allocate(OBJT_SG, npages);
126 object->handle = sglist_hold(sg);
127 TAILQ_INIT(&object->un_pager.sgp.sgp_pglist);
128 return (object);
129 }
130
131 static void
132 sg_pager_dealloc(vm_object_t object)
133 {
134 struct sglist *sg;
135 vm_page_t m;
136
137 /*
138 * Free up our fake pages.
139 */
140 while ((m = TAILQ_FIRST(&object->un_pager.sgp.sgp_pglist)) != 0) {
141 TAILQ_REMOVE(&object->un_pager.sgp.sgp_pglist, m, pageq);
142 sg_pager_putfake(m);
143 }
144
145 sg = object->handle;
146 sglist_free(sg);
147 }
148
149 static int
150 sg_pager_getpages(vm_object_t object, vm_page_t *m, int count, int reqpage)
151 {
152 struct sglist *sg;
153 vm_page_t m_paddr, page;
154 vm_pindex_t offset;
155 vm_paddr_t paddr;
156 vm_memattr_t memattr;
157 size_t space;
158 int i;
159
160 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
161 sg = object->handle;
162 memattr = object->memattr;
163 VM_OBJECT_UNLOCK(object);
164 offset = m[reqpage]->pindex;
165
166 /*
167 * Lookup the physical address of the requested page. An initial
168 * value of '1' instead of '' is used so we can assert that the
169 * page is found since '' can be a valid page-aligned physical
170 * address.
171 */
172 space = 0;
173 paddr = 1;
174 for (i = 0; i < sg->sg_nseg; i++) {
175 if (space + sg->sg_segs[i].ss_len <= (offset * PAGE_SIZE)) {
176 space += sg->sg_segs[i].ss_len;
177 continue;
178 }
179 paddr = sg->sg_segs[i].ss_paddr + offset * PAGE_SIZE - space;
180 break;
181 }
182 KASSERT(paddr != 1, ("invalid SG page index"));
183
184 /* If "paddr" is a real page, perform a sanity check on "memattr". */
185 if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL &&
186 pmap_page_get_memattr(m_paddr) != memattr) {
187 memattr = pmap_page_get_memattr(m_paddr);
188 printf(
189 "WARNING: A device driver has set \"memattr\" inconsistently.\n");
190 }
191
192 /* Return a fake page for the requested page. */
193 KASSERT(!(m[reqpage]->flags & PG_FICTITIOUS),
194 ("backing page for SG is fake"));
195
196 /* Construct a new fake page. */
197 page = sg_pager_getfake(paddr, memattr);
198 VM_OBJECT_LOCK(object);
199 TAILQ_INSERT_TAIL(&object->un_pager.sgp.sgp_pglist, page, pageq);
200
201 /* Free the original pages and insert this fake page into the object. */
202 vm_page_lock_queues();
203 for (i = 0; i < count; i++)
204 vm_page_free(m[i]);
205 vm_page_unlock_queues();
206 vm_page_insert(page, object, offset);
207 m[reqpage] = page;
208 page->valid = VM_PAGE_BITS_ALL;
209
210 return (VM_PAGER_OK);
211 }
212
213 static void
214 sg_pager_putpages(vm_object_t object, vm_page_t *m, int count,
215 boolean_t sync, int *rtvals)
216 {
217
218 panic("sg_pager_putpage called");
219 }
220
221 static boolean_t
222 sg_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
223 int *after)
224 {
225
226 if (before != NULL)
227 *before = 0;
228 if (after != NULL)
229 *after = 0;
230 return (TRUE);
231 }
232
233 /*
234 * Create a fictitious page with the specified physical address and memory
235 * attribute. The memory attribute is the only the machine-dependent aspect
236 * of a fictitious page that must be initialized.
237 */
238 static vm_page_t
239 sg_pager_getfake(vm_paddr_t paddr, vm_memattr_t memattr)
240 {
241 vm_page_t m;
242
243 m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO);
244 m->phys_addr = paddr;
245 /* Fictitious pages don't use "segind". */
246 m->flags = PG_FICTITIOUS;
247 /* Fictitious pages don't use "order" or "pool". */
248 m->oflags = VPO_BUSY;
249 m->wire_count = 1;
250 pmap_page_set_memattr(m, memattr);
251 return (m);
252 }
253
254 static void
255 sg_pager_putfake(vm_page_t m)
256 {
257
258 if (!(m->flags & PG_FICTITIOUS))
259 panic("sg_pager_putfake: bad page");
260 uma_zfree(fakepg_zone, m);
261 }
Cache object: b6e8848d5be3c46b49f30cbc7eb7fe69
|