xref: /freebsd/sys/amd64/vmm/vmm_mem.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
1366f6083SPeter Grehan /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3c49761ddSPedro F. Giffuni  *
4366f6083SPeter Grehan  * Copyright (c) 2011 NetApp, Inc.
5366f6083SPeter Grehan  * All rights reserved.
6366f6083SPeter Grehan  *
7366f6083SPeter Grehan  * Redistribution and use in source and binary forms, with or without
8366f6083SPeter Grehan  * modification, are permitted provided that the following conditions
9366f6083SPeter Grehan  * are met:
10366f6083SPeter Grehan  * 1. Redistributions of source code must retain the above copyright
11366f6083SPeter Grehan  *    notice, this list of conditions and the following disclaimer.
12366f6083SPeter Grehan  * 2. Redistributions in binary form must reproduce the above copyright
13366f6083SPeter Grehan  *    notice, this list of conditions and the following disclaimer in the
14366f6083SPeter Grehan  *    documentation and/or other materials provided with the distribution.
15366f6083SPeter Grehan  *
16366f6083SPeter Grehan  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17366f6083SPeter Grehan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18366f6083SPeter Grehan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19366f6083SPeter Grehan  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20366f6083SPeter Grehan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21366f6083SPeter Grehan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22366f6083SPeter Grehan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23366f6083SPeter Grehan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24366f6083SPeter Grehan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25366f6083SPeter Grehan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26366f6083SPeter Grehan  * SUCH DAMAGE.
27366f6083SPeter Grehan  *
28366f6083SPeter Grehan  * $FreeBSD$
29366f6083SPeter Grehan  */
30366f6083SPeter Grehan 
31366f6083SPeter Grehan #include <sys/cdefs.h>
32366f6083SPeter Grehan __FBSDID("$FreeBSD$");
33366f6083SPeter Grehan 
34366f6083SPeter Grehan #include <sys/param.h>
35366f6083SPeter Grehan #include <sys/systm.h>
36366f6083SPeter Grehan #include <sys/malloc.h>
37318224bbSNeel Natu #include <sys/sglist.h>
38318224bbSNeel Natu #include <sys/lock.h>
39318224bbSNeel Natu #include <sys/rwlock.h>
40366f6083SPeter Grehan 
41366f6083SPeter Grehan #include <vm/vm.h>
42318224bbSNeel Natu #include <vm/vm_param.h>
43366f6083SPeter Grehan #include <vm/pmap.h>
44318224bbSNeel Natu #include <vm/vm_map.h>
45318224bbSNeel Natu #include <vm/vm_object.h>
467ce04d0aSNeel Natu #include <vm/vm_page.h>
47318224bbSNeel Natu #include <vm/vm_pager.h>
48366f6083SPeter Grehan 
49366f6083SPeter Grehan #include <machine/md_var.h>
50366f6083SPeter Grehan 
51366f6083SPeter Grehan #include "vmm_mem.h"
52366f6083SPeter Grehan 
53366f6083SPeter Grehan int
54366f6083SPeter Grehan vmm_mem_init(void)
55366f6083SPeter Grehan {
56366f6083SPeter Grehan 
57366f6083SPeter Grehan 	return (0);
58366f6083SPeter Grehan }
59366f6083SPeter Grehan 
60318224bbSNeel Natu vm_object_t
61318224bbSNeel Natu vmm_mmio_alloc(struct vmspace *vmspace, vm_paddr_t gpa, size_t len,
62318224bbSNeel Natu 	       vm_paddr_t hpa)
63366f6083SPeter Grehan {
64318224bbSNeel Natu 	int error;
65318224bbSNeel Natu 	vm_object_t obj;
66318224bbSNeel Natu 	struct sglist *sg;
67366f6083SPeter Grehan 
68318224bbSNeel Natu 	sg = sglist_alloc(1, M_WAITOK);
69318224bbSNeel Natu 	error = sglist_append_phys(sg, hpa, len);
70318224bbSNeel Natu 	KASSERT(error == 0, ("error %d appending physaddr to sglist", error));
71366f6083SPeter Grehan 
72318224bbSNeel Natu 	obj = vm_pager_allocate(OBJT_SG, sg, len, VM_PROT_RW, 0, NULL);
73318224bbSNeel Natu 	if (obj != NULL) {
747ce04d0aSNeel Natu 		/*
75318224bbSNeel Natu 		 * VT-x ignores the MTRR settings when figuring out the
76318224bbSNeel Natu 		 * memory type for translations obtained through EPT.
77318224bbSNeel Natu 		 *
78318224bbSNeel Natu 		 * Therefore we explicitly force the pages provided by
79318224bbSNeel Natu 		 * this object to be mapped as uncacheable.
807ce04d0aSNeel Natu 		 */
81318224bbSNeel Natu 		VM_OBJECT_WLOCK(obj);
82318224bbSNeel Natu 		error = vm_object_set_memattr(obj, VM_MEMATTR_UNCACHEABLE);
83318224bbSNeel Natu 		VM_OBJECT_WUNLOCK(obj);
84318224bbSNeel Natu 		if (error != KERN_SUCCESS) {
85318224bbSNeel Natu 			panic("vmm_mmio_alloc: vm_object_set_memattr error %d",
86318224bbSNeel Natu 				error);
87318224bbSNeel Natu 		}
88318224bbSNeel Natu 		error = vm_map_find(&vmspace->vm_map, obj, 0, &gpa, len, 0,
89318224bbSNeel Natu 				    VMFS_NO_SPACE, VM_PROT_RW, VM_PROT_RW, 0);
90318224bbSNeel Natu 		if (error != KERN_SUCCESS) {
91318224bbSNeel Natu 			vm_object_deallocate(obj);
92318224bbSNeel Natu 			obj = NULL;
93318224bbSNeel Natu 		}
94366f6083SPeter Grehan 	}
95366f6083SPeter Grehan 
96318224bbSNeel Natu 	/*
97318224bbSNeel Natu 	 * Drop the reference on the sglist.
98318224bbSNeel Natu 	 *
99318224bbSNeel Natu 	 * If the scatter/gather object was successfully allocated then it
100318224bbSNeel Natu 	 * has incremented the reference count on the sglist. Dropping the
101318224bbSNeel Natu 	 * initial reference count ensures that the sglist will be freed
102318224bbSNeel Natu 	 * when the object is deallocated.
103318224bbSNeel Natu 	 *
104318224bbSNeel Natu 	 * If the object could not be allocated then we end up freeing the
105318224bbSNeel Natu 	 * sglist.
106318224bbSNeel Natu 	 */
107318224bbSNeel Natu 	sglist_free(sg);
108366f6083SPeter Grehan 
109318224bbSNeel Natu 	return (obj);
110177fd533SPeter Grehan }
111177fd533SPeter Grehan 
112366f6083SPeter Grehan void
113318224bbSNeel Natu vmm_mmio_free(struct vmspace *vmspace, vm_paddr_t gpa, size_t len)
114366f6083SPeter Grehan {
115366f6083SPeter Grehan 
116318224bbSNeel Natu 	vm_map_remove(&vmspace->vm_map, gpa, gpa + len);
117366f6083SPeter Grehan }
118366f6083SPeter Grehan 
119366f6083SPeter Grehan vm_paddr_t
120366f6083SPeter Grehan vmm_mem_maxaddr(void)
121366f6083SPeter Grehan {
122366f6083SPeter Grehan 
1237ce04d0aSNeel Natu 	return (ptoa(Maxmem));
124366f6083SPeter Grehan }
125