xref: /freebsd/sys/dev/mem/memutil.c (revision 2fea6431120dc19a04430c68b2484524074b2e43)
1a20ad05bSMark Murray /*-
2a20ad05bSMark Murray  * Copyright (c) 2004 Mark R V Murray
3a20ad05bSMark Murray  * All rights reserved.
4a20ad05bSMark Murray  *
5a20ad05bSMark Murray  * Redistribution and use in source and binary forms, with or without
6a20ad05bSMark Murray  * modification, are permitted provided that the following conditions
7a20ad05bSMark Murray  * are met:
8a20ad05bSMark Murray  * 1. Redistributions of source code must retain the above copyright
9a20ad05bSMark Murray  *    notice, this list of conditions and the following disclaimer
10a20ad05bSMark Murray  *    in this position and unchanged.
11a20ad05bSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
12a20ad05bSMark Murray  *    notice, this list of conditions and the following disclaimer in the
13a20ad05bSMark Murray  *    documentation and/or other materials provided with the distribution.
14a20ad05bSMark Murray  *
15a20ad05bSMark Murray  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16a20ad05bSMark Murray  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17a20ad05bSMark Murray  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18a20ad05bSMark Murray  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19a20ad05bSMark Murray  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20a20ad05bSMark Murray  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21a20ad05bSMark Murray  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22a20ad05bSMark Murray  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23a20ad05bSMark Murray  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24a20ad05bSMark Murray  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25a20ad05bSMark Murray  */
26a20ad05bSMark Murray 
27a20ad05bSMark Murray #include <sys/cdefs.h>
28a20ad05bSMark Murray __FBSDID("$FreeBSD$");
29a20ad05bSMark Murray 
30a20ad05bSMark Murray #include <sys/param.h>
31*2fea6431SJung-uk Kim #include <sys/kernel.h>
32*2fea6431SJung-uk Kim #include <sys/lock.h>
33a20ad05bSMark Murray #include <sys/malloc.h>
34a20ad05bSMark Murray #include <sys/memrange.h>
35*2fea6431SJung-uk Kim #include <sys/rwlock.h>
36a20ad05bSMark Murray #include <sys/systm.h>
37a20ad05bSMark Murray 
38*2fea6431SJung-uk Kim static struct rwlock	mr_lock;
39*2fea6431SJung-uk Kim 
40a20ad05bSMark Murray /*
41a20ad05bSMark Murray  * Implementation-neutral, kernel-callable functions for manipulating
42a20ad05bSMark Murray  * memory range attributes.
43a20ad05bSMark Murray  */
44*2fea6431SJung-uk Kim void
45*2fea6431SJung-uk Kim mem_range_init(void)
46*2fea6431SJung-uk Kim {
47*2fea6431SJung-uk Kim 
48*2fea6431SJung-uk Kim 	if (mem_range_softc.mr_op == NULL)
49*2fea6431SJung-uk Kim 		return;
50*2fea6431SJung-uk Kim 	rw_init(&mr_lock, "memrange");
51*2fea6431SJung-uk Kim 	mem_range_softc.mr_op->init(&mem_range_softc);
52*2fea6431SJung-uk Kim }
53*2fea6431SJung-uk Kim 
54*2fea6431SJung-uk Kim void
55*2fea6431SJung-uk Kim mem_range_destroy(void)
56*2fea6431SJung-uk Kim {
57*2fea6431SJung-uk Kim 
58*2fea6431SJung-uk Kim 	if (mem_range_softc.mr_op == NULL)
59*2fea6431SJung-uk Kim 		return;
60*2fea6431SJung-uk Kim 	rw_destroy(&mr_lock);
61*2fea6431SJung-uk Kim }
62*2fea6431SJung-uk Kim 
63a20ad05bSMark Murray int
64a20ad05bSMark Murray mem_range_attr_get(struct mem_range_desc *mrd, int *arg)
65a20ad05bSMark Murray {
66*2fea6431SJung-uk Kim 	int nd;
67*2fea6431SJung-uk Kim 
68a20ad05bSMark Murray 	if (mem_range_softc.mr_op == NULL)
69a20ad05bSMark Murray 		return (EOPNOTSUPP);
70*2fea6431SJung-uk Kim 	nd = *arg;
71*2fea6431SJung-uk Kim 	rw_rlock(&mr_lock);
72*2fea6431SJung-uk Kim 	if (nd == 0)
73a20ad05bSMark Murray 		*arg = mem_range_softc.mr_ndesc;
74a20ad05bSMark Murray 	else
75*2fea6431SJung-uk Kim 		bcopy(mem_range_softc.mr_desc, mrd, nd * sizeof(*mrd));
76*2fea6431SJung-uk Kim 	rw_runlock(&mr_lock);
77a20ad05bSMark Murray 	return (0);
78a20ad05bSMark Murray }
79a20ad05bSMark Murray 
80a20ad05bSMark Murray int
81a20ad05bSMark Murray mem_range_attr_set(struct mem_range_desc *mrd, int *arg)
82a20ad05bSMark Murray {
83*2fea6431SJung-uk Kim 	int ret;
84*2fea6431SJung-uk Kim 
85a20ad05bSMark Murray 	if (mem_range_softc.mr_op == NULL)
86a20ad05bSMark Murray 		return (EOPNOTSUPP);
87*2fea6431SJung-uk Kim 	rw_wlock(&mr_lock);
88*2fea6431SJung-uk Kim 	ret = mem_range_softc.mr_op->set(&mem_range_softc, mrd, arg);
89*2fea6431SJung-uk Kim 	rw_wunlock(&mr_lock);
90*2fea6431SJung-uk Kim 	return (ret);
91a20ad05bSMark Murray }
92