1133129Smarkm/*-
2133129Smarkm * Copyright (c) 2004 Mark R V Murray
3133129Smarkm * All rights reserved.
4133129Smarkm *
5133129Smarkm * Redistribution and use in source and binary forms, with or without
6133129Smarkm * modification, are permitted provided that the following conditions
7133129Smarkm * are met:
8133129Smarkm * 1. Redistributions of source code must retain the above copyright
9133129Smarkm *    notice, this list of conditions and the following disclaimer
10133129Smarkm *    in this position and unchanged.
11133129Smarkm * 2. Redistributions in binary form must reproduce the above copyright
12133129Smarkm *    notice, this list of conditions and the following disclaimer in the
13133129Smarkm *    documentation and/or other materials provided with the distribution.
14133129Smarkm *
15133129Smarkm * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16133129Smarkm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17133129Smarkm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18133129Smarkm * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19133129Smarkm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20133129Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21133129Smarkm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22133129Smarkm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23133129Smarkm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24133129Smarkm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25133129Smarkm */
26133129Smarkm
27133129Smarkm#include <sys/cdefs.h>
28133129Smarkm__FBSDID("$FreeBSD$");
29133129Smarkm
30133129Smarkm#include <sys/param.h>
31217515Sjkim#include <sys/kernel.h>
32217515Sjkim#include <sys/lock.h>
33133129Smarkm#include <sys/malloc.h>
34133129Smarkm#include <sys/memrange.h>
35217515Sjkim#include <sys/rwlock.h>
36133129Smarkm#include <sys/systm.h>
37133129Smarkm
38217515Sjkimstatic struct rwlock	mr_lock;
39217515Sjkim
40133129Smarkm/*
41133129Smarkm * Implementation-neutral, kernel-callable functions for manipulating
42133129Smarkm * memory range attributes.
43133129Smarkm */
44217515Sjkimvoid
45217515Sjkimmem_range_init(void)
46217515Sjkim{
47217515Sjkim
48217515Sjkim	if (mem_range_softc.mr_op == NULL)
49217515Sjkim		return;
50217515Sjkim	rw_init(&mr_lock, "memrange");
51217515Sjkim	mem_range_softc.mr_op->init(&mem_range_softc);
52217515Sjkim}
53217515Sjkim
54217515Sjkimvoid
55217515Sjkimmem_range_destroy(void)
56217515Sjkim{
57217515Sjkim
58217515Sjkim	if (mem_range_softc.mr_op == NULL)
59217515Sjkim		return;
60217515Sjkim	rw_destroy(&mr_lock);
61217515Sjkim}
62217515Sjkim
63133129Smarkmint
64133129Smarkmmem_range_attr_get(struct mem_range_desc *mrd, int *arg)
65133129Smarkm{
66217515Sjkim	int nd;
67217515Sjkim
68133129Smarkm	if (mem_range_softc.mr_op == NULL)
69133129Smarkm		return (EOPNOTSUPP);
70217515Sjkim	nd = *arg;
71217515Sjkim	rw_rlock(&mr_lock);
72217515Sjkim	if (nd == 0)
73133129Smarkm		*arg = mem_range_softc.mr_ndesc;
74133129Smarkm	else
75217515Sjkim		bcopy(mem_range_softc.mr_desc, mrd, nd * sizeof(*mrd));
76217515Sjkim	rw_runlock(&mr_lock);
77133129Smarkm	return (0);
78133129Smarkm}
79133129Smarkm
80133129Smarkmint
81133129Smarkmmem_range_attr_set(struct mem_range_desc *mrd, int *arg)
82133129Smarkm{
83217515Sjkim	int ret;
84217515Sjkim
85133129Smarkm	if (mem_range_softc.mr_op == NULL)
86133129Smarkm		return (EOPNOTSUPP);
87217515Sjkim	rw_wlock(&mr_lock);
88217515Sjkim	ret = mem_range_softc.mr_op->set(&mem_range_softc, mrd, arg);
89217515Sjkim	rw_wunlock(&mr_lock);
90217515Sjkim	return (ret);
91133129Smarkm}
92