/*-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_map.h>39#include <vm/vm_object.h>40#include <vm/vm_page.h>41#include <vm/vm_pager.h>4243#include <machine/md_var.h>4445#include "vmm_mem.h"4647vm_object_t48vmm_mmio_alloc(struct vmspace *vmspace, vm_paddr_t gpa, size_t len,49vm_paddr_t hpa)50{51int error;52vm_object_t obj;53struct sglist *sg;5455sg = sglist_alloc(1, M_WAITOK);56error = sglist_append_phys(sg, hpa, len);57KASSERT(error == 0, ("error %d appending physaddr to sglist", error));5859obj = vm_pager_allocate(OBJT_SG, sg, len, VM_PROT_RW, 0, NULL);60if (obj != NULL) {61/*62* VT-x ignores the MTRR settings when figuring out the63* memory type for translations obtained through EPT.64*65* Therefore we explicitly force the pages provided by66* this object to be mapped as uncacheable.67*/68VM_OBJECT_WLOCK(obj);69error = vm_object_set_memattr(obj, VM_MEMATTR_UNCACHEABLE);70VM_OBJECT_WUNLOCK(obj);71if (error != KERN_SUCCESS) {72panic("vmm_mmio_alloc: vm_object_set_memattr error %d",73error);74}75error = vm_map_find(&vmspace->vm_map, obj, 0, &gpa, len, 0,76VMFS_NO_SPACE, VM_PROT_RW, VM_PROT_RW, 0);77if (error != KERN_SUCCESS) {78vm_object_deallocate(obj);79obj = NULL;80}81}8283/*84* Drop the reference on the sglist.85*86* If the scatter/gather object was successfully allocated then it87* has incremented the reference count on the sglist. Dropping the88* initial reference count ensures that the sglist will be freed89* when the object is deallocated.90*91* If the object could not be allocated then we end up freeing the92* sglist.93*/94sglist_free(sg);9596return (obj);97}9899void100vmm_mmio_free(struct vmspace *vmspace, vm_paddr_t gpa, size_t len)101{102103vm_map_remove(&vmspace->vm_map, gpa, gpa + len);104}105106vm_paddr_t107vmm_mem_maxaddr(void)108{109110return (ptoa(Maxmem));111}112113114