xref: /freebsd/sys/amd64/vmm/vmm_mem.c (revision f3754afd5901857787271e73f9c34d3b9069a03f)
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 
29366f6083SPeter Grehan #include <sys/param.h>
30366f6083SPeter Grehan #include <sys/systm.h>
31366f6083SPeter Grehan #include <sys/malloc.h>
32318224bbSNeel Natu #include <sys/sglist.h>
33318224bbSNeel Natu #include <sys/lock.h>
34318224bbSNeel Natu #include <sys/rwlock.h>
35366f6083SPeter Grehan 
36366f6083SPeter Grehan #include <vm/vm.h>
37318224bbSNeel Natu #include <vm/vm_param.h>
38366f6083SPeter Grehan #include <vm/pmap.h>
39318224bbSNeel Natu #include <vm/vm_map.h>
40318224bbSNeel Natu #include <vm/vm_object.h>
417ce04d0aSNeel Natu #include <vm/vm_page.h>
42318224bbSNeel Natu #include <vm/vm_pager.h>
43366f6083SPeter Grehan 
44366f6083SPeter Grehan #include <machine/md_var.h>
45366f6083SPeter Grehan 
46366f6083SPeter Grehan #include "vmm_mem.h"
47366f6083SPeter Grehan 
48366f6083SPeter Grehan int
vmm_mem_init(void)49366f6083SPeter Grehan vmm_mem_init(void)
50366f6083SPeter Grehan {
51366f6083SPeter Grehan 
52366f6083SPeter Grehan 	return (0);
53366f6083SPeter Grehan }
54366f6083SPeter Grehan 
55318224bbSNeel Natu vm_object_t
vmm_mmio_alloc(struct vmspace * vmspace,vm_paddr_t gpa,size_t len,vm_paddr_t hpa)56318224bbSNeel Natu vmm_mmio_alloc(struct vmspace *vmspace, vm_paddr_t gpa, size_t len,
57318224bbSNeel Natu 	       vm_paddr_t hpa)
58366f6083SPeter Grehan {
59318224bbSNeel Natu 	int error;
60318224bbSNeel Natu 	vm_object_t obj;
61318224bbSNeel Natu 	struct sglist *sg;
62366f6083SPeter Grehan 
63318224bbSNeel Natu 	sg = sglist_alloc(1, M_WAITOK);
64318224bbSNeel Natu 	error = sglist_append_phys(sg, hpa, len);
65318224bbSNeel Natu 	KASSERT(error == 0, ("error %d appending physaddr to sglist", error));
66366f6083SPeter Grehan 
67318224bbSNeel Natu 	obj = vm_pager_allocate(OBJT_SG, sg, len, VM_PROT_RW, 0, NULL);
68318224bbSNeel Natu 	if (obj != NULL) {
697ce04d0aSNeel Natu 		/*
70318224bbSNeel Natu 		 * VT-x ignores the MTRR settings when figuring out the
71318224bbSNeel Natu 		 * memory type for translations obtained through EPT.
72318224bbSNeel Natu 		 *
73318224bbSNeel Natu 		 * Therefore we explicitly force the pages provided by
74318224bbSNeel Natu 		 * this object to be mapped as uncacheable.
757ce04d0aSNeel Natu 		 */
76318224bbSNeel Natu 		VM_OBJECT_WLOCK(obj);
77318224bbSNeel Natu 		error = vm_object_set_memattr(obj, VM_MEMATTR_UNCACHEABLE);
78318224bbSNeel Natu 		VM_OBJECT_WUNLOCK(obj);
79318224bbSNeel Natu 		if (error != KERN_SUCCESS) {
80318224bbSNeel Natu 			panic("vmm_mmio_alloc: vm_object_set_memattr error %d",
81318224bbSNeel Natu 				error);
82318224bbSNeel Natu 		}
83318224bbSNeel Natu 		error = vm_map_find(&vmspace->vm_map, obj, 0, &gpa, len, 0,
84318224bbSNeel Natu 				    VMFS_NO_SPACE, VM_PROT_RW, VM_PROT_RW, 0);
85318224bbSNeel Natu 		if (error != KERN_SUCCESS) {
86318224bbSNeel Natu 			vm_object_deallocate(obj);
87318224bbSNeel Natu 			obj = NULL;
88318224bbSNeel Natu 		}
89366f6083SPeter Grehan 	}
90366f6083SPeter Grehan 
91318224bbSNeel Natu 	/*
92318224bbSNeel Natu 	 * Drop the reference on the sglist.
93318224bbSNeel Natu 	 *
94318224bbSNeel Natu 	 * If the scatter/gather object was successfully allocated then it
95318224bbSNeel Natu 	 * has incremented the reference count on the sglist. Dropping the
96318224bbSNeel Natu 	 * initial reference count ensures that the sglist will be freed
97318224bbSNeel Natu 	 * when the object is deallocated.
98318224bbSNeel Natu 	 *
99318224bbSNeel Natu 	 * If the object could not be allocated then we end up freeing the
100318224bbSNeel Natu 	 * sglist.
101318224bbSNeel Natu 	 */
102318224bbSNeel Natu 	sglist_free(sg);
103366f6083SPeter Grehan 
104318224bbSNeel Natu 	return (obj);
105177fd533SPeter Grehan }
106177fd533SPeter Grehan 
107366f6083SPeter Grehan void
vmm_mmio_free(struct vmspace * vmspace,vm_paddr_t gpa,size_t len)108318224bbSNeel Natu vmm_mmio_free(struct vmspace *vmspace, vm_paddr_t gpa, size_t len)
109366f6083SPeter Grehan {
110366f6083SPeter Grehan 
111318224bbSNeel Natu 	vm_map_remove(&vmspace->vm_map, gpa, gpa + len);
112366f6083SPeter Grehan }
113366f6083SPeter Grehan 
114366f6083SPeter Grehan vm_paddr_t
vmm_mem_maxaddr(void)115366f6083SPeter Grehan vmm_mem_maxaddr(void)
116366f6083SPeter Grehan {
117366f6083SPeter Grehan 
1187ce04d0aSNeel Natu 	return (ptoa(Maxmem));
119366f6083SPeter Grehan }
120