/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2011 NetApp, Inc.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#include <sys/param.h>29#include <sys/systm.h>30#include <sys/malloc.h>31#include <sys/sglist.h>32#include <sys/lock.h>33#include <sys/rwlock.h>3435#include <vm/vm.h>36#include <vm/vm_param.h>37#include <vm/pmap.h>38#include <vm/vm_extern.h>39#include <vm/vm_map.h>40#include <vm/vm_object.h>41#include <vm/vm_page.h>42#include <vm/vm_pager.h>4344#include <machine/md_var.h>4546#include "vmm_mem.h"4748int49vmm_mmio_alloc(struct vmspace *vmspace, vm_paddr_t gpa, size_t len,50vm_paddr_t hpa)51{52struct sglist *sg;53vm_object_t obj;54int error;5556if (gpa + len < gpa || hpa + len < hpa || (gpa & PAGE_MASK) != 0 ||57(hpa & PAGE_MASK) != 0 || (len & PAGE_MASK) != 0)58return (EINVAL);5960sg = sglist_alloc(1, M_WAITOK);61error = sglist_append_phys(sg, hpa, len);62KASSERT(error == 0, ("error %d appending physaddr to sglist", error));6364obj = vm_pager_allocate(OBJT_SG, sg, len, VM_PROT_RW, 0, NULL);65if (obj == NULL)66return (ENOMEM);6768/*69* VT-x ignores the MTRR settings when figuring out the memory type for70* translations obtained through EPT.71*72* Therefore we explicitly force the pages provided by this object to be73* mapped as uncacheable.74*/75VM_OBJECT_WLOCK(obj);76error = vm_object_set_memattr(obj, VM_MEMATTR_UNCACHEABLE);77VM_OBJECT_WUNLOCK(obj);78if (error != KERN_SUCCESS)79panic("vmm_mmio_alloc: vm_object_set_memattr error %d", error);8081vm_map_lock(&vmspace->vm_map);82error = vm_map_insert(&vmspace->vm_map, obj, 0, gpa, gpa + len,83VM_PROT_RW, VM_PROT_RW, 0);84vm_map_unlock(&vmspace->vm_map);85if (error != KERN_SUCCESS) {86error = vm_mmap_to_errno(error);87vm_object_deallocate(obj);88} else {89error = 0;90}9192/*93* Drop the reference on the sglist.94*95* If the scatter/gather object was successfully allocated then it96* has incremented the reference count on the sglist. Dropping the97* initial reference count ensures that the sglist will be freed98* when the object is deallocated.99*100* If the object could not be allocated then we end up freeing the101* sglist.102*/103sglist_free(sg);104105return (error);106}107108void109vmm_mmio_free(struct vmspace *vmspace, vm_paddr_t gpa, size_t len)110{111112vm_map_remove(&vmspace->vm_map, gpa, gpa + len);113}114115vm_paddr_t116vmm_mem_maxaddr(void)117{118119return (ptoa(Maxmem));120}121122123