17fa7744bSHiten Pandya /*
29cca02b8SHiten Pandya * Copyright (c) 2003, 2004 The DragonFly Project. All rights reserved.
37fa7744bSHiten Pandya *
49cca02b8SHiten Pandya * This code is derived from software contributed to The DragonFly Project
59cca02b8SHiten Pandya * by Hiten Pandya <hmp@backplane.com>.
69cca02b8SHiten Pandya *
79cca02b8SHiten Pandya * Redistribution and use in source and binary forms, with or without
89cca02b8SHiten Pandya * modification, are permitted provided that the following conditions
99cca02b8SHiten Pandya * are met:
109cca02b8SHiten Pandya *
119cca02b8SHiten Pandya * 1. Redistributions of source code must retain the above copyright
129cca02b8SHiten Pandya * notice, this list of conditions and the following disclaimer.
139cca02b8SHiten Pandya * 2. Redistributions in binary form must reproduce the above copyright
149cca02b8SHiten Pandya * notice, this list of conditions and the following disclaimer in
159cca02b8SHiten Pandya * the documentation and/or other materials provided with the
169cca02b8SHiten Pandya * distribution.
179cca02b8SHiten Pandya * 3. Neither the name of The DragonFly Project nor the names of its
189cca02b8SHiten Pandya * contributors may be used to endorse or promote products derived
199cca02b8SHiten Pandya * from this software without specific, prior written permission.
209cca02b8SHiten Pandya *
219cca02b8SHiten Pandya * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
229cca02b8SHiten Pandya * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
239cca02b8SHiten Pandya * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
249cca02b8SHiten Pandya * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
259cca02b8SHiten Pandya * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
269cca02b8SHiten Pandya * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
279cca02b8SHiten Pandya * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
289cca02b8SHiten Pandya * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
299cca02b8SHiten Pandya * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
309cca02b8SHiten Pandya * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
319cca02b8SHiten Pandya * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
329cca02b8SHiten Pandya * SUCH DAMAGE.
339cca02b8SHiten Pandya *
349cca02b8SHiten Pandya */
359cca02b8SHiten Pandya /*
367fa7744bSHiten Pandya * Copyright (c) 1991 Regents of the University of California.
377fa7744bSHiten Pandya * All rights reserved.
387fa7744bSHiten Pandya *
397fa7744bSHiten Pandya * This code is derived from software contributed to Berkeley by
407fa7744bSHiten Pandya * The Mach Operating System project at Carnegie-Mellon University.
417fa7744bSHiten Pandya *
427fa7744bSHiten Pandya * Redistribution and use in source and binary forms, with or without
437fa7744bSHiten Pandya * modification, are permitted provided that the following conditions
447fa7744bSHiten Pandya * are met:
457fa7744bSHiten Pandya * 1. Redistributions of source code must retain the above copyright
467fa7744bSHiten Pandya * notice, this list of conditions and the following disclaimer.
477fa7744bSHiten Pandya * 2. Redistributions in binary form must reproduce the above copyright
487fa7744bSHiten Pandya * notice, this list of conditions and the following disclaimer in the
497fa7744bSHiten Pandya * documentation and/or other materials provided with the distribution.
505959bdc9SHiten Pandya * 3. Neither the name of the University nor the names of its contributors
517fa7744bSHiten Pandya * may be used to endorse or promote products derived from this software
527fa7744bSHiten Pandya * without specific prior written permission.
537fa7744bSHiten Pandya *
547fa7744bSHiten Pandya * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
557fa7744bSHiten Pandya * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
567fa7744bSHiten Pandya * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
577fa7744bSHiten Pandya * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
587fa7744bSHiten Pandya * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
597fa7744bSHiten Pandya * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
607fa7744bSHiten Pandya * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
617fa7744bSHiten Pandya * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
627fa7744bSHiten Pandya * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
637fa7744bSHiten Pandya * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
647fa7744bSHiten Pandya * SUCH DAMAGE.
657fa7744bSHiten Pandya *
667fa7744bSHiten Pandya * from: @(#)vm_page.c 7.4 (Berkeley) 5/7/91
677fa7744bSHiten Pandya */
687fa7744bSHiten Pandya
697fa7744bSHiten Pandya /*
707fa7744bSHiten Pandya * Copyright (c) 1987, 1990 Carnegie-Mellon University.
717fa7744bSHiten Pandya * All rights reserved.
727fa7744bSHiten Pandya *
737fa7744bSHiten Pandya * Authors: Avadis Tevanian, Jr., Michael Wayne Young
747fa7744bSHiten Pandya *
757fa7744bSHiten Pandya * Permission to use, copy, modify and distribute this software and
767fa7744bSHiten Pandya * its documentation is hereby granted, provided that both the copyright
777fa7744bSHiten Pandya * notice and this permission notice appear in all copies of the
787fa7744bSHiten Pandya * software, derivative works or modified versions, and any portions
797fa7744bSHiten Pandya * thereof, and that both notices appear in supporting documentation.
807fa7744bSHiten Pandya *
817fa7744bSHiten Pandya * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
827fa7744bSHiten Pandya * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
837fa7744bSHiten Pandya * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
847fa7744bSHiten Pandya *
857fa7744bSHiten Pandya * Carnegie Mellon requests users of this software to return to
867fa7744bSHiten Pandya *
877fa7744bSHiten Pandya * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
887fa7744bSHiten Pandya * School of Computer Science
897fa7744bSHiten Pandya * Carnegie Mellon University
907fa7744bSHiten Pandya * Pittsburgh PA 15213-3890
917fa7744bSHiten Pandya *
927fa7744bSHiten Pandya * any improvements or extensions that they make and grant Carnegie the
937fa7744bSHiten Pandya * rights to redistribute these changes.
947fa7744bSHiten Pandya */
957fa7744bSHiten Pandya
967fa7744bSHiten Pandya /*
977fa7744bSHiten Pandya * Contiguous memory allocation API.
987fa7744bSHiten Pandya */
997fa7744bSHiten Pandya
1007fa7744bSHiten Pandya #include <sys/param.h>
1017fa7744bSHiten Pandya #include <sys/systm.h>
1027fa7744bSHiten Pandya #include <sys/malloc.h>
1037fa7744bSHiten Pandya #include <sys/proc.h>
1047fa7744bSHiten Pandya #include <sys/lock.h>
1057fa7744bSHiten Pandya #include <sys/vmmeter.h>
1067fa7744bSHiten Pandya #include <sys/vnode.h>
1077fa7744bSHiten Pandya
1087fa7744bSHiten Pandya #include <vm/vm.h>
1097fa7744bSHiten Pandya #include <vm/vm_param.h>
1107fa7744bSHiten Pandya #include <vm/vm_kern.h>
1117fa7744bSHiten Pandya #include <vm/pmap.h>
1127fa7744bSHiten Pandya #include <vm/vm_map.h>
1137fa7744bSHiten Pandya #include <vm/vm_object.h>
1147fa7744bSHiten Pandya #include <vm/vm_page.h>
1157fa7744bSHiten Pandya #include <vm/vm_pageout.h>
1167fa7744bSHiten Pandya #include <vm/vm_pager.h>
1177fa7744bSHiten Pandya #include <vm/vm_extern.h>
1185fd012e0SMatthew Dillon
119b12defdcSMatthew Dillon #include <sys/spinlock2.h>
1207fa7744bSHiten Pandya #include <vm/vm_page2.h>
1217fa7744bSHiten Pandya
122df47a6f0SMatthew Dillon #include <machine/bus_dma.h>
123df47a6f0SMatthew Dillon
124b7ea2f3fSMatthew Dillon static void vm_contig_pg_free(vm_pindex_t start, u_long size);
125b12defdcSMatthew Dillon
1267fa7744bSHiten Pandya /*
1277fa7744bSHiten Pandya * vm_contig_pg_clean:
1287fa7744bSHiten Pandya *
1297fa7744bSHiten Pandya * Do a thorough cleanup of the specified 'queue', which can be either
1307fa7744bSHiten Pandya * PQ_ACTIVE or PQ_INACTIVE by doing a walkthrough. If the page is not
1317fa7744bSHiten Pandya * marked dirty, it is shoved into the page cache, provided no one has
1327fa7744bSHiten Pandya * currently aqcuired it, otherwise localized action per object type
1337fa7744bSHiten Pandya * is taken for cleanup:
1347fa7744bSHiten Pandya *
1357fa7744bSHiten Pandya * In the OBJT_VNODE case, the whole page range is cleaned up
1367fa7744bSHiten Pandya * using the vm_object_page_clean() routine, by specyfing a
1377fa7744bSHiten Pandya * start and end of '0'.
1387fa7744bSHiten Pandya *
1397fa7744bSHiten Pandya * Otherwise if the object is of any other type, the generic
1407fa7744bSHiten Pandya * pageout (daemon) flush routine is invoked.
1417fa7744bSHiten Pandya */
142b12defdcSMatthew Dillon static void
vm_contig_pg_clean(int queue,vm_pindex_t count)143b7ea2f3fSMatthew Dillon vm_contig_pg_clean(int queue, vm_pindex_t count)
1447fa7744bSHiten Pandya {
1457fa7744bSHiten Pandya vm_object_t object;
146b12defdcSMatthew Dillon vm_page_t m, m_tmp;
147b12defdcSMatthew Dillon struct vm_page marker;
148b12defdcSMatthew Dillon struct vpgqueues *pq = &vm_page_queues[queue];
1497fa7744bSHiten Pandya
150b12defdcSMatthew Dillon /*
151b12defdcSMatthew Dillon * Setup a local marker
152b12defdcSMatthew Dillon */
153b12defdcSMatthew Dillon bzero(&marker, sizeof(marker));
154bc0aa189SMatthew Dillon marker.flags = PG_FICTITIOUS | PG_MARKER;
155bc0aa189SMatthew Dillon marker.busy_count = PBUSY_LOCKED;
156b12defdcSMatthew Dillon marker.queue = queue;
157b12defdcSMatthew Dillon marker.wire_count = 1;
15899ad9bc4SMatthew Dillon
159b12defdcSMatthew Dillon vm_page_queues_spin_lock(queue);
160b12defdcSMatthew Dillon TAILQ_INSERT_HEAD(&pq->pl, &marker, pageq);
161b12defdcSMatthew Dillon vm_page_queues_spin_unlock(queue);
162ac2c8895SMatthew Dillon
163b12defdcSMatthew Dillon /*
164b12defdcSMatthew Dillon * Iterate the queue. Note that the vm_page spinlock must be
165b12defdcSMatthew Dillon * acquired before the pageq spinlock so it's easiest to simply
166b12defdcSMatthew Dillon * not hold it in the loop iteration.
167b12defdcSMatthew Dillon */
168b7ea2f3fSMatthew Dillon while ((long)count-- > 0 &&
169b7ea2f3fSMatthew Dillon (m = TAILQ_NEXT(&marker, pageq)) != NULL) {
170b12defdcSMatthew Dillon vm_page_and_queue_spin_lock(m);
171b12defdcSMatthew Dillon if (m != TAILQ_NEXT(&marker, pageq)) {
172b12defdcSMatthew Dillon vm_page_and_queue_spin_unlock(m);
173b12defdcSMatthew Dillon ++count;
174ac2c8895SMatthew Dillon continue;
175b12defdcSMatthew Dillon }
176b12defdcSMatthew Dillon KKASSERT(m->queue == queue);
1777fa7744bSHiten Pandya
178b12defdcSMatthew Dillon TAILQ_REMOVE(&pq->pl, &marker, pageq);
179b12defdcSMatthew Dillon TAILQ_INSERT_AFTER(&pq->pl, m, &marker, pageq);
1807fa7744bSHiten Pandya
181b12defdcSMatthew Dillon if (m->flags & PG_MARKER) {
182b12defdcSMatthew Dillon vm_page_and_queue_spin_unlock(m);
183b12defdcSMatthew Dillon continue;
184b12defdcSMatthew Dillon }
185b12defdcSMatthew Dillon if (vm_page_busy_try(m, TRUE)) {
186b12defdcSMatthew Dillon vm_page_and_queue_spin_unlock(m);
187b12defdcSMatthew Dillon continue;
188b12defdcSMatthew Dillon }
189b12defdcSMatthew Dillon vm_page_and_queue_spin_unlock(m);
190b12defdcSMatthew Dillon
191b12defdcSMatthew Dillon /*
192b12defdcSMatthew Dillon * We've successfully busied the page
193b12defdcSMatthew Dillon */
194b12defdcSMatthew Dillon if (m->queue - m->pc != queue) {
195b12defdcSMatthew Dillon vm_page_wakeup(m);
196b12defdcSMatthew Dillon continue;
197b12defdcSMatthew Dillon }
19879d182b0SMatthew Dillon if (m->wire_count || m->hold_count) {
19979d182b0SMatthew Dillon vm_page_wakeup(m);
20079d182b0SMatthew Dillon continue;
20179d182b0SMatthew Dillon }
202b12defdcSMatthew Dillon if ((object = m->object) == NULL) {
203b12defdcSMatthew Dillon vm_page_wakeup(m);
204b12defdcSMatthew Dillon continue;
205b12defdcSMatthew Dillon }
2067fa7744bSHiten Pandya vm_page_test_dirty(m);
2079bf025dbSMatthew Dillon if (m->dirty || (m->flags & PG_NEED_COMMIT)) {
208b12defdcSMatthew Dillon vm_object_hold(object);
209b12defdcSMatthew Dillon KKASSERT(m->object == object);
210b12defdcSMatthew Dillon
2117fa7744bSHiten Pandya if (object->type == OBJT_VNODE) {
212b12defdcSMatthew Dillon vm_page_wakeup(m);
213ca466baeSMatthew Dillon vn_lock(object->handle, LK_EXCLUSIVE|LK_RETRY);
2147fa7744bSHiten Pandya vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
215a11aaa81SMatthew Dillon vn_unlock(((struct vnode *)object->handle));
2167fa7744bSHiten Pandya } else if (object->type == OBJT_SWAP ||
2177fa7744bSHiten Pandya object->type == OBJT_DEFAULT) {
2187fa7744bSHiten Pandya m_tmp = m;
2197fa7744bSHiten Pandya vm_pageout_flush(&m_tmp, 1, 0);
220b12defdcSMatthew Dillon } else {
221b12defdcSMatthew Dillon vm_page_wakeup(m);
2227fa7744bSHiten Pandya }
223b12defdcSMatthew Dillon vm_object_drop(object);
224b12defdcSMatthew Dillon } else if (m->hold_count == 0) {
2257fa7744bSHiten Pandya vm_page_cache(m);
226b12defdcSMatthew Dillon } else {
227b12defdcSMatthew Dillon vm_page_wakeup(m);
2287fa7744bSHiten Pandya }
229a491077eSMatthew Dillon }
2307fa7744bSHiten Pandya
2317fa7744bSHiten Pandya /*
232b12defdcSMatthew Dillon * Scrap our local marker
233ec59a781SMatthew Dillon */
234b12defdcSMatthew Dillon vm_page_queues_spin_lock(queue);
235b12defdcSMatthew Dillon TAILQ_REMOVE(&pq->pl, &marker, pageq);
236b12defdcSMatthew Dillon vm_page_queues_spin_unlock(queue);
237ec59a781SMatthew Dillon }
238b12defdcSMatthew Dillon
239ec59a781SMatthew Dillon /*
2407fa7744bSHiten Pandya * vm_contig_pg_alloc:
2417fa7744bSHiten Pandya *
2427fa7744bSHiten Pandya * Allocate contiguous pages from the VM. This function does not
2437fa7744bSHiten Pandya * map the allocated pages into the kernel map, otherwise it is
2447fa7744bSHiten Pandya * impossible to make large allocations (i.e. >2G).
2457fa7744bSHiten Pandya *
2467fa7744bSHiten Pandya * Malloc()'s data structures have been used for collection of
2477fa7744bSHiten Pandya * statistics and for allocations of less than a page.
2487fa7744bSHiten Pandya */
249b7ea2f3fSMatthew Dillon static vm_pindex_t
vm_contig_pg_alloc(unsigned long size,vm_paddr_t low,vm_paddr_t high,unsigned long alignment,unsigned long boundary,int mflags)250ec59a781SMatthew Dillon vm_contig_pg_alloc(unsigned long size, vm_paddr_t low, vm_paddr_t high,
251ec59a781SMatthew Dillon unsigned long alignment, unsigned long boundary, int mflags)
2527fa7744bSHiten Pandya {
253b7ea2f3fSMatthew Dillon vm_pindex_t i, q, start;
2547fa7744bSHiten Pandya vm_offset_t phys;
2557fa7744bSHiten Pandya vm_page_t pga = vm_page_array;
2563e0c7583SMatthew Dillon vm_page_t m;
257b7ea2f3fSMatthew Dillon int pass;
2583e0c7583SMatthew Dillon int pqtype;
2597fa7744bSHiten Pandya
2607fa7744bSHiten Pandya size = round_page(size);
2617fa7744bSHiten Pandya if (size == 0)
2627fa7744bSHiten Pandya panic("vm_contig_pg_alloc: size must not be 0");
2637fa7744bSHiten Pandya if ((alignment & (alignment - 1)) != 0)
2647fa7744bSHiten Pandya panic("vm_contig_pg_alloc: alignment must be a power of 2");
2657fa7744bSHiten Pandya if ((boundary & (boundary - 1)) != 0)
2667fa7744bSHiten Pandya panic("vm_contig_pg_alloc: boundary must be a power of 2");
2677fa7744bSHiten Pandya
26879d182b0SMatthew Dillon /*
26979d182b0SMatthew Dillon * See if we can get the pages from the contiguous page reserve
27079d182b0SMatthew Dillon * alist. The returned pages will be allocated and wired but not
27179d182b0SMatthew Dillon * busied.
272df47a6f0SMatthew Dillon *
273df47a6f0SMatthew Dillon * If high is not set to BUS_SPACE_MAXADDR we try using our
274df47a6f0SMatthew Dillon * free memory reserve first, otherwise we try it last.
275fe63a898SMatthew Dillon *
276fe63a898SMatthew Dillon * XXX Always use the dma reserve first for performance, until
277fe63a898SMatthew Dillon * we find a better way to differentiate the DRM API.
27879d182b0SMatthew Dillon */
279fe63a898SMatthew Dillon #if 0
280fe63a898SMatthew Dillon if (high != BUS_SPACE_MAXADDR)
281fe63a898SMatthew Dillon #endif
282fe63a898SMatthew Dillon {
2838b9ed12eSFrançois Tigeot m = vm_page_alloc_contig(
284df47a6f0SMatthew Dillon low, high, alignment, boundary,
285df47a6f0SMatthew Dillon size, VM_MEMATTR_DEFAULT);
28679d182b0SMatthew Dillon if (m)
28779d182b0SMatthew Dillon return (m - &pga[0]);
288df47a6f0SMatthew Dillon }
289ec59a781SMatthew Dillon
2907fa7744bSHiten Pandya /*
291ec59a781SMatthew Dillon * Three passes (0, 1, 2). Each pass scans the VM page list for
292ec59a781SMatthew Dillon * free or cached pages. After each pass if the entire scan failed
293ec59a781SMatthew Dillon * we attempt to flush inactive pages and reset the start index back
294ec59a781SMatthew Dillon * to 0. For passes 1 and 2 we also attempt to flush active pages.
2957fa7744bSHiten Pandya */
29679d182b0SMatthew Dillon start = 0;
297ec59a781SMatthew Dillon for (pass = 0; pass < 3; pass++) {
298ec59a781SMatthew Dillon /*
299ec59a781SMatthew Dillon * Find first page in array that is free, within range,
300ec59a781SMatthew Dillon * aligned, and such that the boundary won't be crossed.
301ec59a781SMatthew Dillon */
302ec59a781SMatthew Dillon again:
3037fa7744bSHiten Pandya for (i = start; i < vmstats.v_page_count; i++) {
3043e0c7583SMatthew Dillon m = &pga[i];
3053e0c7583SMatthew Dillon phys = VM_PAGE_TO_PHYS(m);
3063e0c7583SMatthew Dillon pqtype = m->queue - m->pc;
3077fa7744bSHiten Pandya if (((pqtype == PQ_FREE) || (pqtype == PQ_CACHE)) &&
3087fa7744bSHiten Pandya (phys >= low) && (phys < high) &&
3097fa7744bSHiten Pandya ((phys & (alignment - 1)) == 0) &&
3103f7b7260SSascha Wildner ((rounddown2(phys ^ (phys + size - 1), boundary)) == 0) &&
311bc0aa189SMatthew Dillon m->wire_count == 0 && m->hold_count == 0 &&
312bc0aa189SMatthew Dillon (m->busy_count &
313bc0aa189SMatthew Dillon (PBUSY_LOCKED | PBUSY_MASK)) == 0 &&
314bc0aa189SMatthew Dillon (m->flags & PG_NEED_COMMIT) == 0)
3159bf025dbSMatthew Dillon {
3167fa7744bSHiten Pandya break;
3177fa7744bSHiten Pandya }
3183e0c7583SMatthew Dillon }
3197fa7744bSHiten Pandya
3207fa7744bSHiten Pandya /*
3217fa7744bSHiten Pandya * If we cannot find the page in the given range, or we have
3227fa7744bSHiten Pandya * crossed the boundary, call the vm_contig_pg_clean() function
3237fa7744bSHiten Pandya * for flushing out the queues, and returning it back to
3247fa7744bSHiten Pandya * normal state.
3257fa7744bSHiten Pandya */
3267fa7744bSHiten Pandya if ((i == vmstats.v_page_count) ||
3277fa7744bSHiten Pandya ((VM_PAGE_TO_PHYS(&pga[i]) + size) > high)) {
3287fa7744bSHiten Pandya
329ec59a781SMatthew Dillon /*
330ec59a781SMatthew Dillon * Best effort flush of all inactive pages.
331ec59a781SMatthew Dillon * This is quite quick, for now stall all
332ec59a781SMatthew Dillon * callers, even if they've specified M_NOWAIT.
333ec59a781SMatthew Dillon */
334027193ebSMatthew Dillon for (q = 0; q < PQ_L2_SIZE; ++q) {
335027193ebSMatthew Dillon vm_contig_pg_clean(PQ_INACTIVE + q,
336ec59a781SMatthew Dillon vmstats.v_inactive_count);
337027193ebSMatthew Dillon lwkt_yield();
338027193ebSMatthew Dillon }
3397fa7744bSHiten Pandya
340ec59a781SMatthew Dillon /*
341ec59a781SMatthew Dillon * Best effort flush of active pages.
342ec59a781SMatthew Dillon *
343ec59a781SMatthew Dillon * This is very, very slow.
344ec59a781SMatthew Dillon * Only do this if the caller has agreed to M_WAITOK.
345ec59a781SMatthew Dillon *
346ec59a781SMatthew Dillon * If enough pages are flushed, we may succeed on
347ec59a781SMatthew Dillon * next (final) pass, if not the caller, contigmalloc(),
348ec59a781SMatthew Dillon * will fail in the index < 0 case.
349ec59a781SMatthew Dillon */
350ec59a781SMatthew Dillon if (pass > 0 && (mflags & M_WAITOK)) {
351027193ebSMatthew Dillon for (q = 0; q < PQ_L2_SIZE; ++q) {
352027193ebSMatthew Dillon vm_contig_pg_clean(PQ_ACTIVE + q,
353ec59a781SMatthew Dillon vmstats.v_active_count);
354ec59a781SMatthew Dillon }
355027193ebSMatthew Dillon lwkt_yield();
356027193ebSMatthew Dillon }
357ec59a781SMatthew Dillon
358ec59a781SMatthew Dillon /*
359ec59a781SMatthew Dillon * We're already too high in the address space
360ec59a781SMatthew Dillon * to succeed, reset to 0 for the next iteration.
361ec59a781SMatthew Dillon */
362ec59a781SMatthew Dillon start = 0;
3637fa7744bSHiten Pandya continue; /* next pass */
3647fa7744bSHiten Pandya }
3657fa7744bSHiten Pandya start = i;
3667fa7744bSHiten Pandya
3677fa7744bSHiten Pandya /*
3687fa7744bSHiten Pandya * Check successive pages for contiguous and free.
3695fd012e0SMatthew Dillon *
3705fd012e0SMatthew Dillon * (still in critical section)
3717fa7744bSHiten Pandya */
3727fa7744bSHiten Pandya for (i = start + 1; i < (start + size / PAGE_SIZE); i++) {
3733e0c7583SMatthew Dillon m = &pga[i];
3743e0c7583SMatthew Dillon pqtype = m->queue - m->pc;
3753e0c7583SMatthew Dillon if ((VM_PAGE_TO_PHYS(&m[0]) !=
3763e0c7583SMatthew Dillon (VM_PAGE_TO_PHYS(&m[-1]) + PAGE_SIZE)) ||
3773e0c7583SMatthew Dillon ((pqtype != PQ_FREE) && (pqtype != PQ_CACHE)) ||
378bc0aa189SMatthew Dillon m->wire_count ||
3799bf025dbSMatthew Dillon m->hold_count ||
380bc0aa189SMatthew Dillon (m->busy_count & (PBUSY_LOCKED | PBUSY_MASK)) ||
381bc0aa189SMatthew Dillon (m->flags & PG_NEED_COMMIT))
3829bf025dbSMatthew Dillon {
3837fa7744bSHiten Pandya start++;
3847fa7744bSHiten Pandya goto again;
3857fa7744bSHiten Pandya }
3867fa7744bSHiten Pandya }
3877fa7744bSHiten Pandya
3885fd012e0SMatthew Dillon /*
38979d182b0SMatthew Dillon * Try to allocate the pages, wiring them as we go.
390b12defdcSMatthew Dillon *
3915fd012e0SMatthew Dillon * (still in critical section)
3925fd012e0SMatthew Dillon */
3937fa7744bSHiten Pandya for (i = start; i < (start + size / PAGE_SIZE); i++) {
3943e0c7583SMatthew Dillon m = &pga[i];
395b12defdcSMatthew Dillon
396b12defdcSMatthew Dillon if (vm_page_busy_try(m, TRUE)) {
397b12defdcSMatthew Dillon vm_contig_pg_free(start,
398b12defdcSMatthew Dillon (i - start) * PAGE_SIZE);
399b12defdcSMatthew Dillon start++;
400b12defdcSMatthew Dillon goto again;
401b12defdcSMatthew Dillon }
4027fa7744bSHiten Pandya pqtype = m->queue - m->pc;
403709f5959SMatthew Dillon if (pqtype == PQ_CACHE &&
404709f5959SMatthew Dillon m->hold_count == 0 &&
405709f5959SMatthew Dillon m->wire_count == 0 &&
406831a8507SMatthew Dillon (m->flags & PG_NEED_COMMIT) == 0) {
407709f5959SMatthew Dillon vm_page_protect(m, VM_PROT_NONE);
408831a8507SMatthew Dillon KKASSERT((m->flags &
409831a8507SMatthew Dillon (PG_MAPPED | PG_UNQUEUED)) == 0);
410709f5959SMatthew Dillon KKASSERT(m->dirty == 0);
4117fa7744bSHiten Pandya vm_page_free(m);
412b12defdcSMatthew Dillon --i;
413b12defdcSMatthew Dillon continue; /* retry the page */
414b12defdcSMatthew Dillon }
415709f5959SMatthew Dillon if (pqtype != PQ_FREE || m->hold_count) {
416b12defdcSMatthew Dillon vm_page_wakeup(m);
417b12defdcSMatthew Dillon vm_contig_pg_free(start,
418b12defdcSMatthew Dillon (i - start) * PAGE_SIZE);
419b12defdcSMatthew Dillon start++;
420b12defdcSMatthew Dillon goto again;
4217fa7744bSHiten Pandya }
422709f5959SMatthew Dillon KKASSERT((m->valid & m->dirty) == 0);
423709f5959SMatthew Dillon KKASSERT(m->wire_count == 0);
4243e0c7583SMatthew Dillon KKASSERT(m->object == NULL);
4257fa7744bSHiten Pandya vm_page_unqueue_nowakeup(m);
4267fa7744bSHiten Pandya m->valid = VM_PAGE_BITS_ALL;
4277fa7744bSHiten Pandya KASSERT(m->dirty == 0,
4287fa7744bSHiten Pandya ("vm_contig_pg_alloc: page %p was dirty", m));
429b12defdcSMatthew Dillon KKASSERT(m->wire_count == 0);
430bc0aa189SMatthew Dillon KKASSERT((m->busy_count & PBUSY_MASK) == 0);
431d0aa00e8SMatthew Dillon
432d0aa00e8SMatthew Dillon /*
433831a8507SMatthew Dillon * Clear all flags, set FICTITIOUS and UNQUEUED to
434831a8507SMatthew Dillon * indicate the the pages are special, then unbusy
435831a8507SMatthew Dillon * the now allocated page.
436831a8507SMatthew Dillon *
437831a8507SMatthew Dillon * XXX setting FICTITIOUS and UNQUEUED in the future.
438831a8507SMatthew Dillon * (also pair up with vm_contig_pg_free)
439d0aa00e8SMatthew Dillon */
440e4b2227aSMatthew Dillon vm_page_flag_clear(m, ~PG_KEEP_NEWPAGE_MASK);
441831a8507SMatthew Dillon /* vm_page_flag_set(m, PG_FICTITIOUS | PG_UNQUEUED);*/
44279d182b0SMatthew Dillon vm_page_wire(m);
443b12defdcSMatthew Dillon vm_page_wakeup(m);
4447fa7744bSHiten Pandya }
4457fa7744bSHiten Pandya
4467fa7744bSHiten Pandya /*
4477fa7744bSHiten Pandya * Our job is done, return the index page of vm_page_array.
4487fa7744bSHiten Pandya */
4497fa7744bSHiten Pandya return (start); /* aka &pga[start] */
4507fa7744bSHiten Pandya }
4517fa7744bSHiten Pandya
452fe63a898SMatthew Dillon #if 0
4537fa7744bSHiten Pandya /*
454df47a6f0SMatthew Dillon * Failed, if we haven't already tried, allocate from our reserved
455df47a6f0SMatthew Dillon * dma memory.
456fe63a898SMatthew Dillon *
457fe63a898SMatthew Dillon * XXX (see conditionalized code above)
458df47a6f0SMatthew Dillon */
459df47a6f0SMatthew Dillon if (high == BUS_SPACE_MAXADDR) {
460df47a6f0SMatthew Dillon m = vm_page_alloc_contig(
461df47a6f0SMatthew Dillon low, high, alignment, boundary,
462df47a6f0SMatthew Dillon size, VM_MEMATTR_DEFAULT);
463df47a6f0SMatthew Dillon if (m)
464df47a6f0SMatthew Dillon return (m - &pga[0]);
465df47a6f0SMatthew Dillon }
466fe63a898SMatthew Dillon #endif
467df47a6f0SMatthew Dillon
468df47a6f0SMatthew Dillon /*
4697fa7744bSHiten Pandya * Failed.
4707fa7744bSHiten Pandya */
471b7ea2f3fSMatthew Dillon return ((vm_pindex_t)-1);
4727fa7744bSHiten Pandya }
4737fa7744bSHiten Pandya
4747fa7744bSHiten Pandya /*
4757fa7744bSHiten Pandya * vm_contig_pg_free:
4767fa7744bSHiten Pandya *
4777fa7744bSHiten Pandya * Remove pages previously allocated by vm_contig_pg_alloc, and
4787fa7744bSHiten Pandya * assume all references to the pages have been removed, and that
4797fa7744bSHiten Pandya * it is OK to add them back to the free list.
48099ad9bc4SMatthew Dillon *
48199ad9bc4SMatthew Dillon * Caller must ensure no races on the page range in question.
48299ad9bc4SMatthew Dillon * No other requirements.
4837fa7744bSHiten Pandya */
48432e5dfe5SVenkatesh Srinivas static void
vm_contig_pg_free(vm_pindex_t start,u_long size)485b7ea2f3fSMatthew Dillon vm_contig_pg_free(vm_pindex_t start, u_long size)
4867fa7744bSHiten Pandya {
4877fa7744bSHiten Pandya vm_page_t pga = vm_page_array;
4887fa7744bSHiten Pandya
4897fa7744bSHiten Pandya size = round_page(size);
4907fa7744bSHiten Pandya if (size == 0)
4917fa7744bSHiten Pandya panic("vm_contig_pg_free: size must not be 0");
4927fa7744bSHiten Pandya
49379d182b0SMatthew Dillon /*
49479d182b0SMatthew Dillon * The pages are wired, vm_page_free_contig() determines whether they
49579d182b0SMatthew Dillon * belong to the contig space or not and either frees them to that
49679d182b0SMatthew Dillon * space (leaving them wired), or unwires the page and frees it to the
49779d182b0SMatthew Dillon * normal PQ_FREE queue.
49879d182b0SMatthew Dillon */
49979d182b0SMatthew Dillon vm_page_free_contig(&pga[start], size);
5007fa7744bSHiten Pandya }
5017fa7744bSHiten Pandya
5027fa7744bSHiten Pandya /*
5037fa7744bSHiten Pandya * vm_contig_pg_kmap:
5047fa7744bSHiten Pandya *
5057fa7744bSHiten Pandya * Map previously allocated (vm_contig_pg_alloc) range of pages from
5067fa7744bSHiten Pandya * vm_page_array[] into the KVA. Once mapped, the pages are part of
5071eeaf6b2SAaron LI * the Kernel, and are to free'ed with kmem_free(kernel_map, addr, size).
50899ad9bc4SMatthew Dillon *
50999ad9bc4SMatthew Dillon * No requirements.
5107fa7744bSHiten Pandya */
51132e5dfe5SVenkatesh Srinivas static vm_offset_t
vm_contig_pg_kmap(vm_pindex_t start,u_long size,vm_map_t map,int flags)512b7ea2f3fSMatthew Dillon vm_contig_pg_kmap(vm_pindex_t start, u_long size, vm_map_t map, int flags)
5137fa7744bSHiten Pandya {
51479d182b0SMatthew Dillon vm_offset_t addr;
51579d182b0SMatthew Dillon vm_paddr_t pa;
5167fa7744bSHiten Pandya vm_page_t pga = vm_page_array;
51779d182b0SMatthew Dillon u_long offset;
5187fa7744bSHiten Pandya
5197fa7744bSHiten Pandya if (size == 0)
5207fa7744bSHiten Pandya panic("vm_contig_pg_kmap: size must not be 0");
52179d182b0SMatthew Dillon size = round_page(size);
5221eeaf6b2SAaron LI addr = kmem_alloc_pageable(kernel_map, size, VM_SUBSYS_CONTIG);
52379d182b0SMatthew Dillon if (addr) {
52479d182b0SMatthew Dillon pa = VM_PAGE_TO_PHYS(&pga[start]);
52579d182b0SMatthew Dillon for (offset = 0; offset < size; offset += PAGE_SIZE)
526ccd67bf6SMatthew Dillon pmap_kenter_noinval(addr + offset, pa + offset);
527*c713db65SAaron LI pmap_invalidate_range(kernel_pmap, addr, addr + size);
52879d182b0SMatthew Dillon if (flags & M_ZERO)
52979d182b0SMatthew Dillon bzero((void *)addr, size);
5307fa7744bSHiten Pandya }
5317fa7744bSHiten Pandya return(addr);
5327fa7744bSHiten Pandya }
5337fa7744bSHiten Pandya
53499ad9bc4SMatthew Dillon /*
53599ad9bc4SMatthew Dillon * No requirements.
53699ad9bc4SMatthew Dillon */
5377fa7744bSHiten Pandya void *
contigmalloc(unsigned long size,struct malloc_type * type,int flags,vm_paddr_t low,vm_paddr_t high,unsigned long alignment,unsigned long boundary)5387fa7744bSHiten Pandya contigmalloc(
5397fa7744bSHiten Pandya unsigned long size, /* should be size_t here and for malloc() */
5407fa7744bSHiten Pandya struct malloc_type *type,
5417fa7744bSHiten Pandya int flags,
5426ef943a3SMatthew Dillon vm_paddr_t low,
5436ef943a3SMatthew Dillon vm_paddr_t high,
5447fa7744bSHiten Pandya unsigned long alignment,
5457fa7744bSHiten Pandya unsigned long boundary)
5467fa7744bSHiten Pandya {
5477fa7744bSHiten Pandya return contigmalloc_map(size, type, flags, low, high, alignment,
5481eeaf6b2SAaron LI boundary, kernel_map);
5497fa7744bSHiten Pandya }
5507fa7744bSHiten Pandya
55199ad9bc4SMatthew Dillon /*
55299ad9bc4SMatthew Dillon * No requirements.
55399ad9bc4SMatthew Dillon */
5547fa7744bSHiten Pandya void *
contigmalloc_map(unsigned long size,struct malloc_type * type,int flags,vm_paddr_t low,vm_paddr_t high,unsigned long alignment,unsigned long boundary,vm_map_t map)55579d182b0SMatthew Dillon contigmalloc_map(unsigned long size, struct malloc_type *type,
55679d182b0SMatthew Dillon int flags, vm_paddr_t low, vm_paddr_t high,
55779d182b0SMatthew Dillon unsigned long alignment, unsigned long boundary,
5587fa7744bSHiten Pandya vm_map_t map)
5597fa7744bSHiten Pandya {
560b7ea2f3fSMatthew Dillon vm_pindex_t index;
5617fa7744bSHiten Pandya void *rv;
5627fa7744bSHiten Pandya
563ec59a781SMatthew Dillon index = vm_contig_pg_alloc(size, low, high, alignment, boundary, flags);
564b7ea2f3fSMatthew Dillon if (index == (vm_pindex_t)-1) {
56571cb41a2SMatthew Dillon kprintf("contigmalloc_map: failed size %lu low=%llx "
56671cb41a2SMatthew Dillon "high=%llx align=%lu boundary=%lu flags=%08x\n",
56771cb41a2SMatthew Dillon size, (long long)low, (long long)high,
56871cb41a2SMatthew Dillon alignment, boundary, flags);
5697fa7744bSHiten Pandya return NULL;
5707fa7744bSHiten Pandya }
5717fa7744bSHiten Pandya
572ba0fefd4SJoerg Sonnenberger rv = (void *)vm_contig_pg_kmap(index, size, map, flags);
57399ad9bc4SMatthew Dillon if (rv == NULL)
5747fa7744bSHiten Pandya vm_contig_pg_free(index, size);
5757fa7744bSHiten Pandya
5767fa7744bSHiten Pandya return rv;
5777fa7744bSHiten Pandya }
5787fa7744bSHiten Pandya
57999ad9bc4SMatthew Dillon /*
58099ad9bc4SMatthew Dillon * No requirements.
58199ad9bc4SMatthew Dillon */
5827fa7744bSHiten Pandya void
contigfree(void * addr,unsigned long size,struct malloc_type * type)5837fa7744bSHiten Pandya contigfree(void *addr, unsigned long size, struct malloc_type *type)
5847fa7744bSHiten Pandya {
58579d182b0SMatthew Dillon vm_paddr_t pa;
58679d182b0SMatthew Dillon vm_page_t m;
58779d182b0SMatthew Dillon
58879d182b0SMatthew Dillon if (size == 0)
58979d182b0SMatthew Dillon panic("vm_contig_pg_kmap: size must not be 0");
59079d182b0SMatthew Dillon size = round_page(size);
59179d182b0SMatthew Dillon
59276f1911eSMatthew Dillon pa = pmap_kextract((vm_offset_t)addr);
59379d182b0SMatthew Dillon pmap_qremove((vm_offset_t)addr, size / PAGE_SIZE);
5941eeaf6b2SAaron LI kmem_free(kernel_map, (vm_offset_t)addr, size);
59579d182b0SMatthew Dillon
59679d182b0SMatthew Dillon m = PHYS_TO_VM_PAGE(pa);
59779d182b0SMatthew Dillon vm_page_free_contig(m, size);
5987fa7744bSHiten Pandya }
5997fa7744bSHiten Pandya
60099ad9bc4SMatthew Dillon /*
60199ad9bc4SMatthew Dillon * No requirements.
60299ad9bc4SMatthew Dillon */
6037fa7744bSHiten Pandya vm_offset_t
kmem_alloc_contig(vm_offset_t size,vm_paddr_t low,vm_paddr_t high,vm_offset_t alignment)60479d182b0SMatthew Dillon kmem_alloc_contig(vm_offset_t size, vm_paddr_t low, vm_paddr_t high,
6057fa7744bSHiten Pandya vm_offset_t alignment)
6067fa7744bSHiten Pandya {
6077fa7744bSHiten Pandya return ((vm_offset_t)contigmalloc_map(size, M_DEVBUF, M_NOWAIT, low,
6081eeaf6b2SAaron LI high, alignment, 0ul, kernel_map));
6097fa7744bSHiten Pandya }
610