101381811SJohn Baldwin /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni *
4179fa75eSJohn Baldwin * Copyright (c) 2009 Hudson River Trading LLC
501381811SJohn Baldwin * Written by: John H. Baldwin <jhb@FreeBSD.org>
601381811SJohn Baldwin * All rights reserved.
701381811SJohn Baldwin *
801381811SJohn Baldwin * Redistribution and use in source and binary forms, with or without
901381811SJohn Baldwin * modification, are permitted provided that the following conditions
1001381811SJohn Baldwin * are met:
1101381811SJohn Baldwin * 1. Redistributions of source code must retain the above copyright
1201381811SJohn Baldwin * notice, this list of conditions and the following disclaimer.
1301381811SJohn Baldwin * 2. Redistributions in binary form must reproduce the above copyright
1401381811SJohn Baldwin * notice, this list of conditions and the following disclaimer in the
1501381811SJohn Baldwin * documentation and/or other materials provided with the distribution.
1601381811SJohn Baldwin *
1701381811SJohn Baldwin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1801381811SJohn Baldwin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1901381811SJohn Baldwin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2001381811SJohn Baldwin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2101381811SJohn Baldwin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2201381811SJohn Baldwin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2301381811SJohn Baldwin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2401381811SJohn Baldwin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2501381811SJohn Baldwin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2601381811SJohn Baldwin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2701381811SJohn Baldwin * SUCH DAMAGE.
2801381811SJohn Baldwin */
2901381811SJohn Baldwin
3001381811SJohn Baldwin #include <sys/cdefs.h>
3101381811SJohn Baldwin /*
3201381811SJohn Baldwin * This pager manages OBJT_SG objects. These objects are backed by
3301381811SJohn Baldwin * a scatter/gather list of physical address ranges.
3401381811SJohn Baldwin */
3501381811SJohn Baldwin
3601381811SJohn Baldwin #include <sys/param.h>
3701381811SJohn Baldwin #include <sys/lock.h>
3801381811SJohn Baldwin #include <sys/mutex.h>
3989f6b863SAttilio Rao #include <sys/rwlock.h>
4001381811SJohn Baldwin #include <sys/sglist.h>
4100a3fe96SKonstantin Belousov #include <sys/user.h>
429ed01c32SGleb Smirnoff #include <sys/vmmeter.h>
439ed01c32SGleb Smirnoff
4401381811SJohn Baldwin #include <vm/vm.h>
451c771f92SKonstantin Belousov #include <vm/vm_param.h>
4601381811SJohn Baldwin #include <vm/vm_object.h>
4701381811SJohn Baldwin #include <vm/vm_page.h>
4801381811SJohn Baldwin #include <vm/vm_pager.h>
4943f48b65SKonstantin Belousov #include <vm/vm_phys.h>
5001381811SJohn Baldwin #include <vm/uma.h>
5101381811SJohn Baldwin
5201381811SJohn Baldwin static vm_object_t sg_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
5301381811SJohn Baldwin vm_ooffset_t, struct ucred *);
5401381811SJohn Baldwin static void sg_pager_dealloc(vm_object_t);
55b0cd2017SGleb Smirnoff static int sg_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
5601381811SJohn Baldwin static void sg_pager_putpages(vm_object_t, vm_page_t *, int,
57f74be55eSDimitry Andric int, int *);
5801381811SJohn Baldwin static boolean_t sg_pager_haspage(vm_object_t, vm_pindex_t, int *,
5901381811SJohn Baldwin int *);
6001381811SJohn Baldwin
61d474440aSKonstantin Belousov const struct pagerops sgpagerops = {
6200a3fe96SKonstantin Belousov .pgo_kvme_type = KVME_TYPE_SG,
6301381811SJohn Baldwin .pgo_alloc = sg_pager_alloc,
6401381811SJohn Baldwin .pgo_dealloc = sg_pager_dealloc,
6501381811SJohn Baldwin .pgo_getpages = sg_pager_getpages,
6601381811SJohn Baldwin .pgo_putpages = sg_pager_putpages,
6701381811SJohn Baldwin .pgo_haspage = sg_pager_haspage,
6801381811SJohn Baldwin };
6901381811SJohn Baldwin
7001381811SJohn Baldwin static vm_object_t
sg_pager_alloc(void * handle,vm_ooffset_t size,vm_prot_t prot,vm_ooffset_t foff,struct ucred * cred)7101381811SJohn Baldwin sg_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
7201381811SJohn Baldwin vm_ooffset_t foff, struct ucred *cred)
7301381811SJohn Baldwin {
7401381811SJohn Baldwin struct sglist *sg;
7501381811SJohn Baldwin vm_object_t object;
7601381811SJohn Baldwin vm_pindex_t npages, pindex;
7701381811SJohn Baldwin int i;
7801381811SJohn Baldwin
7901381811SJohn Baldwin /*
8001381811SJohn Baldwin * Offset should be page aligned.
8101381811SJohn Baldwin */
8201381811SJohn Baldwin if (foff & PAGE_MASK)
8301381811SJohn Baldwin return (NULL);
8401381811SJohn Baldwin
8501381811SJohn Baldwin /*
8601381811SJohn Baldwin * The scatter/gather list must only include page-aligned
8701381811SJohn Baldwin * ranges.
8801381811SJohn Baldwin */
8901381811SJohn Baldwin npages = 0;
9001381811SJohn Baldwin sg = handle;
9101381811SJohn Baldwin for (i = 0; i < sg->sg_nseg; i++) {
9201381811SJohn Baldwin if ((sg->sg_segs[i].ss_paddr % PAGE_SIZE) != 0 ||
9301381811SJohn Baldwin (sg->sg_segs[i].ss_len % PAGE_SIZE) != 0)
9401381811SJohn Baldwin return (NULL);
9501381811SJohn Baldwin npages += sg->sg_segs[i].ss_len / PAGE_SIZE;
9601381811SJohn Baldwin }
9701381811SJohn Baldwin
9801381811SJohn Baldwin /*
9901381811SJohn Baldwin * The scatter/gather list has a fixed size. Refuse requests
10001381811SJohn Baldwin * to map beyond that.
10101381811SJohn Baldwin */
10201381811SJohn Baldwin size = round_page(size);
10310d9120cSKonstantin Belousov pindex = OFF_TO_IDX(foff) + OFF_TO_IDX(size);
10410d9120cSKonstantin Belousov if (pindex > npages || pindex < OFF_TO_IDX(foff) ||
10510d9120cSKonstantin Belousov pindex < OFF_TO_IDX(size))
10601381811SJohn Baldwin return (NULL);
10701381811SJohn Baldwin
10801381811SJohn Baldwin /*
10901381811SJohn Baldwin * Allocate a new object and associate it with the
11001381811SJohn Baldwin * scatter/gather list. It is ok for our purposes to have
11101381811SJohn Baldwin * multiple VM objects associated with the same scatter/gather
11201381811SJohn Baldwin * list because scatter/gather lists are static. This is also
11301381811SJohn Baldwin * simpler than ensuring a unique object per scatter/gather
11401381811SJohn Baldwin * list.
11501381811SJohn Baldwin */
11601381811SJohn Baldwin object = vm_object_allocate(OBJT_SG, npages);
11701381811SJohn Baldwin object->handle = sglist_hold(sg);
11801381811SJohn Baldwin TAILQ_INIT(&object->un_pager.sgp.sgp_pglist);
11901381811SJohn Baldwin return (object);
12001381811SJohn Baldwin }
12101381811SJohn Baldwin
12201381811SJohn Baldwin static void
sg_pager_dealloc(vm_object_t object)12301381811SJohn Baldwin sg_pager_dealloc(vm_object_t object)
12401381811SJohn Baldwin {
12501381811SJohn Baldwin struct sglist *sg;
12601381811SJohn Baldwin vm_page_t m;
12701381811SJohn Baldwin
12801381811SJohn Baldwin /*
12901381811SJohn Baldwin * Free up our fake pages.
13001381811SJohn Baldwin */
13101381811SJohn Baldwin while ((m = TAILQ_FIRST(&object->un_pager.sgp.sgp_pglist)) != 0) {
1323cf3b4e6SJeff Roberson if (vm_page_busy_acquire(m, VM_ALLOC_WAITFAIL) == 0)
1333cf3b4e6SJeff Roberson continue;
134c325e866SKonstantin Belousov TAILQ_REMOVE(&object->un_pager.sgp.sgp_pglist, m, plinks.q);
13510cf2560SAlan Cox vm_page_putfake(m);
13601381811SJohn Baldwin }
13701381811SJohn Baldwin
13801381811SJohn Baldwin sg = object->handle;
13901381811SJohn Baldwin sglist_free(sg);
140e735691bSJohn Baldwin object->handle = NULL;
141e735691bSJohn Baldwin object->type = OBJT_DEAD;
14201381811SJohn Baldwin }
14301381811SJohn Baldwin
14401381811SJohn Baldwin static int
sg_pager_getpages(vm_object_t object,vm_page_t * m,int count,int * rbehind,int * rahead)145b0cd2017SGleb Smirnoff sg_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
146b0cd2017SGleb Smirnoff int *rahead)
14701381811SJohn Baldwin {
14801381811SJohn Baldwin struct sglist *sg;
14901381811SJohn Baldwin vm_page_t m_paddr, page;
15001381811SJohn Baldwin vm_pindex_t offset;
15101381811SJohn Baldwin vm_paddr_t paddr;
15201381811SJohn Baldwin vm_memattr_t memattr;
15301381811SJohn Baldwin size_t space;
15401381811SJohn Baldwin int i;
15501381811SJohn Baldwin
156b0cd2017SGleb Smirnoff /* Since our haspage reports zero after/before, the count is 1. */
157b0cd2017SGleb Smirnoff KASSERT(count == 1, ("%s: count %d", __func__, count));
158d6e13f3bSJeff Roberson /* Handle is stable while paging is in progress. */
15901381811SJohn Baldwin sg = object->handle;
16001381811SJohn Baldwin memattr = object->memattr;
161b0cd2017SGleb Smirnoff offset = m[0]->pindex;
16201381811SJohn Baldwin
16301381811SJohn Baldwin /*
16401381811SJohn Baldwin * Lookup the physical address of the requested page. An initial
16501381811SJohn Baldwin * value of '1' instead of '0' is used so we can assert that the
16601381811SJohn Baldwin * page is found since '0' can be a valid page-aligned physical
16701381811SJohn Baldwin * address.
16801381811SJohn Baldwin */
16901381811SJohn Baldwin space = 0;
17001381811SJohn Baldwin paddr = 1;
17101381811SJohn Baldwin for (i = 0; i < sg->sg_nseg; i++) {
17201381811SJohn Baldwin if (space + sg->sg_segs[i].ss_len <= (offset * PAGE_SIZE)) {
17301381811SJohn Baldwin space += sg->sg_segs[i].ss_len;
17401381811SJohn Baldwin continue;
17501381811SJohn Baldwin }
17601381811SJohn Baldwin paddr = sg->sg_segs[i].ss_paddr + offset * PAGE_SIZE - space;
17701381811SJohn Baldwin break;
17801381811SJohn Baldwin }
17901381811SJohn Baldwin KASSERT(paddr != 1, ("invalid SG page index"));
18001381811SJohn Baldwin
18101381811SJohn Baldwin /* If "paddr" is a real page, perform a sanity check on "memattr". */
18201381811SJohn Baldwin if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL &&
18301381811SJohn Baldwin pmap_page_get_memattr(m_paddr) != memattr) {
18401381811SJohn Baldwin memattr = pmap_page_get_memattr(m_paddr);
18501381811SJohn Baldwin printf(
18601381811SJohn Baldwin "WARNING: A device driver has set \"memattr\" inconsistently.\n");
18701381811SJohn Baldwin }
18801381811SJohn Baldwin
18901381811SJohn Baldwin /* Return a fake page for the requested page. */
190b0cd2017SGleb Smirnoff KASSERT(!(m[0]->flags & PG_FICTITIOUS),
19101381811SJohn Baldwin ("backing page for SG is fake"));
19201381811SJohn Baldwin
19301381811SJohn Baldwin /* Construct a new fake page. */
19410cf2560SAlan Cox page = vm_page_getfake(paddr, memattr);
19589f6b863SAttilio Rao VM_OBJECT_WLOCK(object);
196c325e866SKonstantin Belousov TAILQ_INSERT_TAIL(&object->un_pager.sgp.sgp_pglist, page, plinks.q);
1973cf3b4e6SJeff Roberson vm_page_replace(page, object, offset, m[0]);
198d6e13f3bSJeff Roberson VM_OBJECT_WUNLOCK(object);
199b0cd2017SGleb Smirnoff m[0] = page;
2000012f373SJeff Roberson vm_page_valid(page);
20101381811SJohn Baldwin
202b0cd2017SGleb Smirnoff if (rbehind)
203b0cd2017SGleb Smirnoff *rbehind = 0;
204b0cd2017SGleb Smirnoff if (rahead)
205b0cd2017SGleb Smirnoff *rahead = 0;
206b0cd2017SGleb Smirnoff
20701381811SJohn Baldwin return (VM_PAGER_OK);
20801381811SJohn Baldwin }
20901381811SJohn Baldwin
21001381811SJohn Baldwin static void
sg_pager_putpages(vm_object_t object,vm_page_t * m,int count,int flags,int * rtvals)21101381811SJohn Baldwin sg_pager_putpages(vm_object_t object, vm_page_t *m, int count,
212f74be55eSDimitry Andric int flags, int *rtvals)
21301381811SJohn Baldwin {
21401381811SJohn Baldwin
21501381811SJohn Baldwin panic("sg_pager_putpage called");
21601381811SJohn Baldwin }
21701381811SJohn Baldwin
21801381811SJohn Baldwin static boolean_t
sg_pager_haspage(vm_object_t object,vm_pindex_t pindex,int * before,int * after)21901381811SJohn Baldwin sg_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
22001381811SJohn Baldwin int *after)
22101381811SJohn Baldwin {
22201381811SJohn Baldwin
22301381811SJohn Baldwin if (before != NULL)
22401381811SJohn Baldwin *before = 0;
22501381811SJohn Baldwin if (after != NULL)
22601381811SJohn Baldwin *after = 0;
22701381811SJohn Baldwin return (TRUE);
22801381811SJohn Baldwin }
229