vmm_mem.c revision 256072
1206917Smarius/*-
2206917Smarius * Copyright (c) 2011 NetApp, Inc.
3206917Smarius * All rights reserved.
4206917Smarius *
5206917Smarius * Redistribution and use in source and binary forms, with or without
6206917Smarius * modification, are permitted provided that the following conditions
7206917Smarius * are met:
8206917Smarius * 1. Redistributions of source code must retain the above copyright
9206917Smarius *    notice, this list of conditions and the following disclaimer.
10206917Smarius * 2. Redistributions in binary form must reproduce the above copyright
11206917Smarius *    notice, this list of conditions and the following disclaimer in the
12206917Smarius *    documentation and/or other materials provided with the distribution.
13206917Smarius *
14206917Smarius * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15206917Smarius * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16206917Smarius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17206917Smarius * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18206917Smarius * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19206917Smarius * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20206917Smarius * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21206917Smarius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22206917Smarius * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23206917Smarius * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24206917Smarius * SUCH DAMAGE.
25206917Smarius *
26207151Smarius * $FreeBSD: head/sys/amd64/vmm/vmm_mem.c 256072 2013-10-05 21:22:35Z neel $
27207151Smarius */
28207151Smarius
29206917Smarius#include <sys/cdefs.h>
30206917Smarius__FBSDID("$FreeBSD: head/sys/amd64/vmm/vmm_mem.c 256072 2013-10-05 21:22:35Z neel $");
31206917Smarius
32206917Smarius#include <sys/param.h>
33206917Smarius#include <sys/systm.h>
34206917Smarius#include <sys/malloc.h>
35206917Smarius#include <sys/sglist.h>
36207151Smarius#include <sys/lock.h>
37206917Smarius#include <sys/rwlock.h>
38206917Smarius
39206917Smarius#include <vm/vm.h>
40206917Smarius#include <vm/vm_param.h>
41206917Smarius#include <vm/pmap.h>
42206917Smarius#include <vm/vm_map.h>
43207151Smarius#include <vm/vm_object.h>
44206917Smarius#include <vm/vm_page.h>
45206917Smarius#include <vm/vm_pager.h>
46206917Smarius
47206917Smarius#include <machine/md_var.h>
48206917Smarius
49206917Smarius#include "vmm_mem.h"
50207151Smarius
51206917Smariusint
52206917Smariusvmm_mem_init(void)
53206917Smarius{
54206917Smarius
55206917Smarius	return (0);
56206917Smarius}
57206917Smarius
58206917Smariusvm_object_t
59206917Smariusvmm_mmio_alloc(struct vmspace *vmspace, vm_paddr_t gpa, size_t len,
60206917Smarius	       vm_paddr_t hpa)
61206917Smarius{
62206917Smarius	int error;
63206917Smarius	vm_object_t obj;
64	struct sglist *sg;
65
66	sg = sglist_alloc(1, M_WAITOK);
67	error = sglist_append_phys(sg, hpa, len);
68	KASSERT(error == 0, ("error %d appending physaddr to sglist", error));
69
70	obj = vm_pager_allocate(OBJT_SG, sg, len, VM_PROT_RW, 0, NULL);
71	if (obj != NULL) {
72		/*
73		 * VT-x ignores the MTRR settings when figuring out the
74		 * memory type for translations obtained through EPT.
75		 *
76		 * Therefore we explicitly force the pages provided by
77		 * this object to be mapped as uncacheable.
78		 */
79		VM_OBJECT_WLOCK(obj);
80		error = vm_object_set_memattr(obj, VM_MEMATTR_UNCACHEABLE);
81		VM_OBJECT_WUNLOCK(obj);
82		if (error != KERN_SUCCESS) {
83			panic("vmm_mmio_alloc: vm_object_set_memattr error %d",
84				error);
85		}
86		error = vm_map_find(&vmspace->vm_map, obj, 0, &gpa, len, 0,
87				    VMFS_NO_SPACE, VM_PROT_RW, VM_PROT_RW, 0);
88		if (error != KERN_SUCCESS) {
89			vm_object_deallocate(obj);
90			obj = NULL;
91		}
92	}
93
94	/*
95	 * Drop the reference on the sglist.
96	 *
97	 * If the scatter/gather object was successfully allocated then it
98	 * has incremented the reference count on the sglist. Dropping the
99	 * initial reference count ensures that the sglist will be freed
100	 * when the object is deallocated.
101	 *
102	 * If the object could not be allocated then we end up freeing the
103	 * sglist.
104	 */
105	sglist_free(sg);
106
107	return (obj);
108}
109
110void
111vmm_mmio_free(struct vmspace *vmspace, vm_paddr_t gpa, size_t len)
112{
113
114	vm_map_remove(&vmspace->vm_map, gpa, gpa + len);
115}
116
117vm_object_t
118vmm_mem_alloc(struct vmspace *vmspace, vm_paddr_t gpa, size_t len)
119{
120	int error;
121	vm_object_t obj;
122
123	if (gpa & PAGE_MASK)
124		panic("vmm_mem_alloc: invalid gpa %#lx", gpa);
125
126	if (len == 0 || (len & PAGE_MASK) != 0)
127		panic("vmm_mem_alloc: invalid allocation size %lu", len);
128
129	obj = vm_object_allocate(OBJT_DEFAULT, len >> PAGE_SHIFT);
130	if (obj != NULL) {
131		error = vm_map_find(&vmspace->vm_map, obj, 0, &gpa, len, 0,
132				    VMFS_NO_SPACE, VM_PROT_ALL, VM_PROT_ALL, 0);
133		if (error != KERN_SUCCESS) {
134			vm_object_deallocate(obj);
135			obj = NULL;
136		}
137	}
138
139	return (obj);
140}
141
142void
143vmm_mem_free(struct vmspace *vmspace, vm_paddr_t gpa, size_t len)
144{
145
146	vm_map_remove(&vmspace->vm_map, gpa, gpa + len);
147}
148
149vm_paddr_t
150vmm_mem_maxaddr(void)
151{
152
153	return (ptoa(Maxmem));
154}
155