xref: /freebsd/sys/dev/mem/memutil.c (revision 718cf2ccb9956613756ab15d7a0e28f2c8e91cab)
1a20ad05bSMark Murray /*-
2*718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*718cf2ccSPedro F. Giffuni  *
4a20ad05bSMark Murray  * Copyright (c) 2004 Mark R V Murray
5a20ad05bSMark Murray  * All rights reserved.
6a20ad05bSMark Murray  *
7a20ad05bSMark Murray  * Redistribution and use in source and binary forms, with or without
8a20ad05bSMark Murray  * modification, are permitted provided that the following conditions
9a20ad05bSMark Murray  * are met:
10a20ad05bSMark Murray  * 1. Redistributions of source code must retain the above copyright
11a20ad05bSMark Murray  *    notice, this list of conditions and the following disclaimer
12a20ad05bSMark Murray  *    in this position and unchanged.
13a20ad05bSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
14a20ad05bSMark Murray  *    notice, this list of conditions and the following disclaimer in the
15a20ad05bSMark Murray  *    documentation and/or other materials provided with the distribution.
16a20ad05bSMark Murray  *
17a20ad05bSMark Murray  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18a20ad05bSMark Murray  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19a20ad05bSMark Murray  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20a20ad05bSMark Murray  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21a20ad05bSMark Murray  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22a20ad05bSMark Murray  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23a20ad05bSMark Murray  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24a20ad05bSMark Murray  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25a20ad05bSMark Murray  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26a20ad05bSMark Murray  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27a20ad05bSMark Murray  */
28a20ad05bSMark Murray 
29a20ad05bSMark Murray #include <sys/cdefs.h>
30a20ad05bSMark Murray __FBSDID("$FreeBSD$");
31a20ad05bSMark Murray 
32a20ad05bSMark Murray #include <sys/param.h>
332fea6431SJung-uk Kim #include <sys/kernel.h>
342fea6431SJung-uk Kim #include <sys/lock.h>
35a20ad05bSMark Murray #include <sys/malloc.h>
36a20ad05bSMark Murray #include <sys/memrange.h>
372fea6431SJung-uk Kim #include <sys/rwlock.h>
38a20ad05bSMark Murray #include <sys/systm.h>
39a20ad05bSMark Murray 
402fea6431SJung-uk Kim static struct rwlock	mr_lock;
412fea6431SJung-uk Kim 
42a20ad05bSMark Murray /*
43a20ad05bSMark Murray  * Implementation-neutral, kernel-callable functions for manipulating
44a20ad05bSMark Murray  * memory range attributes.
45a20ad05bSMark Murray  */
462fea6431SJung-uk Kim void
472fea6431SJung-uk Kim mem_range_init(void)
482fea6431SJung-uk Kim {
492fea6431SJung-uk Kim 
502fea6431SJung-uk Kim 	if (mem_range_softc.mr_op == NULL)
512fea6431SJung-uk Kim 		return;
522fea6431SJung-uk Kim 	rw_init(&mr_lock, "memrange");
532fea6431SJung-uk Kim 	mem_range_softc.mr_op->init(&mem_range_softc);
542fea6431SJung-uk Kim }
552fea6431SJung-uk Kim 
562fea6431SJung-uk Kim void
572fea6431SJung-uk Kim mem_range_destroy(void)
582fea6431SJung-uk Kim {
592fea6431SJung-uk Kim 
602fea6431SJung-uk Kim 	if (mem_range_softc.mr_op == NULL)
612fea6431SJung-uk Kim 		return;
622fea6431SJung-uk Kim 	rw_destroy(&mr_lock);
632fea6431SJung-uk Kim }
642fea6431SJung-uk Kim 
65a20ad05bSMark Murray int
66a20ad05bSMark Murray mem_range_attr_get(struct mem_range_desc *mrd, int *arg)
67a20ad05bSMark Murray {
682fea6431SJung-uk Kim 	int nd;
692fea6431SJung-uk Kim 
70a20ad05bSMark Murray 	if (mem_range_softc.mr_op == NULL)
71a20ad05bSMark Murray 		return (EOPNOTSUPP);
722fea6431SJung-uk Kim 	nd = *arg;
732fea6431SJung-uk Kim 	rw_rlock(&mr_lock);
742fea6431SJung-uk Kim 	if (nd == 0)
75a20ad05bSMark Murray 		*arg = mem_range_softc.mr_ndesc;
76a20ad05bSMark Murray 	else
772fea6431SJung-uk Kim 		bcopy(mem_range_softc.mr_desc, mrd, nd * sizeof(*mrd));
782fea6431SJung-uk Kim 	rw_runlock(&mr_lock);
79a20ad05bSMark Murray 	return (0);
80a20ad05bSMark Murray }
81a20ad05bSMark Murray 
82a20ad05bSMark Murray int
83a20ad05bSMark Murray mem_range_attr_set(struct mem_range_desc *mrd, int *arg)
84a20ad05bSMark Murray {
852fea6431SJung-uk Kim 	int ret;
862fea6431SJung-uk Kim 
87a20ad05bSMark Murray 	if (mem_range_softc.mr_op == NULL)
88a20ad05bSMark Murray 		return (EOPNOTSUPP);
892fea6431SJung-uk Kim 	rw_wlock(&mr_lock);
902fea6431SJung-uk Kim 	ret = mem_range_softc.mr_op->set(&mem_range_softc, mrd, arg);
912fea6431SJung-uk Kim 	rw_wunlock(&mr_lock);
922fea6431SJung-uk Kim 	return (ret);
93a20ad05bSMark Murray }
94