vmm_mem.c revision 241362
172195Sasmodai/*-
272195Sasmodai * Copyright (c) 2011 NetApp, Inc.
372195Sasmodai * All rights reserved.
472195Sasmodai *
572195Sasmodai * Redistribution and use in source and binary forms, with or without
672195Sasmodai * modification, are permitted provided that the following conditions
772195Sasmodai * are met:
872195Sasmodai * 1. Redistributions of source code must retain the above copyright
972195Sasmodai *    notice, this list of conditions and the following disclaimer.
1072195Sasmodai * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD$
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#include <sys/param.h>
33#include <sys/lock.h>
34#include <sys/mutex.h>
35#include <sys/linker.h>
36#include <sys/systm.h>
37#include <sys/malloc.h>
38#include <sys/kernel.h>
39#include <sys/sysctl.h>
40
41#include <vm/vm.h>
42#include <vm/pmap.h>
43#include <vm/vm_page.h>
44#include <vm/vm_pageout.h>
45
46#include <machine/md_var.h>
47#include <machine/metadata.h>
48#include <machine/pc/bios.h>
49#include <machine/vmparam.h>
50#include <machine/pmap.h>
51
52#include "vmm_util.h"
53#include "vmm_mem.h"
54
55SYSCTL_DECL(_hw_vmm);
56
57static u_long pages_allocated;
58SYSCTL_ULONG(_hw_vmm, OID_AUTO, pages_allocated, CTLFLAG_RD,
59	     &pages_allocated, 0, "4KB pages allocated");
60
61static void
62update_pages_allocated(int howmany)
63{
64	pages_allocated += howmany;	/* XXX locking? */
65}
66
67int
68vmm_mem_init(void)
69{
70
71	return (0);
72}
73
74vm_paddr_t
75vmm_mem_alloc(size_t size)
76{
77	int flags;
78	vm_page_t m;
79	vm_paddr_t pa;
80
81	if (size != PAGE_SIZE)
82		panic("vmm_mem_alloc: invalid allocation size %lu", size);
83
84	flags = VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
85		VM_ALLOC_ZERO;
86
87	while (1) {
88		/*
89		 * XXX need policy to determine when to back off the allocation
90		 */
91		m = vm_page_alloc(NULL, 0, flags);
92		if (m == NULL)
93			VM_WAIT;
94		else
95			break;
96	}
97
98	pa = VM_PAGE_TO_PHYS(m);
99
100	if ((m->flags & PG_ZERO) == 0)
101		pagezero((void *)PHYS_TO_DMAP(pa));
102
103	update_pages_allocated(1);
104
105	return (pa);
106}
107
108void
109vmm_mem_free(vm_paddr_t base, size_t length)
110{
111	vm_page_t m;
112
113	if (base & PAGE_MASK) {
114		panic("vmm_mem_free: base 0x%0lx must be aligned on a "
115		      "0x%0x boundary\n", base, PAGE_SIZE);
116	}
117
118	if (length != PAGE_SIZE)
119		panic("vmm_mem_free: invalid length %lu", length);
120
121	m = PHYS_TO_VM_PAGE(base);
122	m->wire_count--;
123	vm_page_free(m);
124	atomic_subtract_int(&cnt.v_wire_count, 1);
125
126	update_pages_allocated(-1);
127}
128
129vm_paddr_t
130vmm_mem_maxaddr(void)
131{
132
133	return (ptoa(Maxmem));
134}
135