xref: /freebsd/sys/amd64/vmm/vmm_mem.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 NetApp, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/sglist.h>
38 #include <sys/lock.h>
39 #include <sys/rwlock.h>
40 
41 #include <vm/vm.h>
42 #include <vm/vm_param.h>
43 #include <vm/pmap.h>
44 #include <vm/vm_map.h>
45 #include <vm/vm_object.h>
46 #include <vm/vm_page.h>
47 #include <vm/vm_pager.h>
48 
49 #include <machine/md_var.h>
50 
51 #include "vmm_mem.h"
52 
53 int
54 vmm_mem_init(void)
55 {
56 
57 	return (0);
58 }
59 
60 vm_object_t
61 vmm_mmio_alloc(struct vmspace *vmspace, vm_paddr_t gpa, size_t len,
62 	       vm_paddr_t hpa)
63 {
64 	int error;
65 	vm_object_t obj;
66 	struct sglist *sg;
67 
68 	sg = sglist_alloc(1, M_WAITOK);
69 	error = sglist_append_phys(sg, hpa, len);
70 	KASSERT(error == 0, ("error %d appending physaddr to sglist", error));
71 
72 	obj = vm_pager_allocate(OBJT_SG, sg, len, VM_PROT_RW, 0, NULL);
73 	if (obj != NULL) {
74 		/*
75 		 * VT-x ignores the MTRR settings when figuring out the
76 		 * memory type for translations obtained through EPT.
77 		 *
78 		 * Therefore we explicitly force the pages provided by
79 		 * this object to be mapped as uncacheable.
80 		 */
81 		VM_OBJECT_WLOCK(obj);
82 		error = vm_object_set_memattr(obj, VM_MEMATTR_UNCACHEABLE);
83 		VM_OBJECT_WUNLOCK(obj);
84 		if (error != KERN_SUCCESS) {
85 			panic("vmm_mmio_alloc: vm_object_set_memattr error %d",
86 				error);
87 		}
88 		error = vm_map_find(&vmspace->vm_map, obj, 0, &gpa, len, 0,
89 				    VMFS_NO_SPACE, VM_PROT_RW, VM_PROT_RW, 0);
90 		if (error != KERN_SUCCESS) {
91 			vm_object_deallocate(obj);
92 			obj = NULL;
93 		}
94 	}
95 
96 	/*
97 	 * Drop the reference on the sglist.
98 	 *
99 	 * If the scatter/gather object was successfully allocated then it
100 	 * has incremented the reference count on the sglist. Dropping the
101 	 * initial reference count ensures that the sglist will be freed
102 	 * when the object is deallocated.
103 	 *
104 	 * If the object could not be allocated then we end up freeing the
105 	 * sglist.
106 	 */
107 	sglist_free(sg);
108 
109 	return (obj);
110 }
111 
112 void
113 vmm_mmio_free(struct vmspace *vmspace, vm_paddr_t gpa, size_t len)
114 {
115 
116 	vm_map_remove(&vmspace->vm_map, gpa, gpa + len);
117 }
118 
119 vm_paddr_t
120 vmm_mem_maxaddr(void)
121 {
122 
123 	return (ptoa(Maxmem));
124 }
125