1a20ad05bSMark Murray /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro 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/param.h>
302fea6431SJung-uk Kim #include <sys/kernel.h>
312fea6431SJung-uk Kim #include <sys/lock.h>
32a20ad05bSMark Murray #include <sys/malloc.h>
33a20ad05bSMark Murray #include <sys/memrange.h>
342fea6431SJung-uk Kim #include <sys/rwlock.h>
35a20ad05bSMark Murray #include <sys/systm.h>
36a20ad05bSMark Murray
372fea6431SJung-uk Kim static struct rwlock mr_lock;
382fea6431SJung-uk Kim
39a20ad05bSMark Murray /*
40a20ad05bSMark Murray * Implementation-neutral, kernel-callable functions for manipulating
41a20ad05bSMark Murray * memory range attributes.
42a20ad05bSMark Murray */
432fea6431SJung-uk Kim void
mem_range_init(void)442fea6431SJung-uk Kim mem_range_init(void)
452fea6431SJung-uk Kim {
462fea6431SJung-uk Kim
472fea6431SJung-uk Kim if (mem_range_softc.mr_op == NULL)
482fea6431SJung-uk Kim return;
492fea6431SJung-uk Kim rw_init(&mr_lock, "memrange");
502fea6431SJung-uk Kim mem_range_softc.mr_op->init(&mem_range_softc);
512fea6431SJung-uk Kim }
522fea6431SJung-uk Kim
532fea6431SJung-uk Kim void
mem_range_destroy(void)542fea6431SJung-uk Kim mem_range_destroy(void)
552fea6431SJung-uk Kim {
562fea6431SJung-uk Kim
572fea6431SJung-uk Kim if (mem_range_softc.mr_op == NULL)
582fea6431SJung-uk Kim return;
592fea6431SJung-uk Kim rw_destroy(&mr_lock);
602fea6431SJung-uk Kim }
612fea6431SJung-uk Kim
62a20ad05bSMark Murray int
mem_range_attr_get(struct mem_range_desc * mrd,int * arg)63a20ad05bSMark Murray mem_range_attr_get(struct mem_range_desc *mrd, int *arg)
64a20ad05bSMark Murray {
652fea6431SJung-uk Kim int nd;
662fea6431SJung-uk Kim
67a20ad05bSMark Murray if (mem_range_softc.mr_op == NULL)
68a20ad05bSMark Murray return (EOPNOTSUPP);
692fea6431SJung-uk Kim nd = *arg;
702fea6431SJung-uk Kim rw_rlock(&mr_lock);
712fea6431SJung-uk Kim if (nd == 0)
72a20ad05bSMark Murray *arg = mem_range_softc.mr_ndesc;
73a20ad05bSMark Murray else
742fea6431SJung-uk Kim bcopy(mem_range_softc.mr_desc, mrd, nd * sizeof(*mrd));
752fea6431SJung-uk Kim rw_runlock(&mr_lock);
76a20ad05bSMark Murray return (0);
77a20ad05bSMark Murray }
78a20ad05bSMark Murray
79a20ad05bSMark Murray int
mem_range_attr_set(struct mem_range_desc * mrd,int * arg)80a20ad05bSMark Murray mem_range_attr_set(struct mem_range_desc *mrd, int *arg)
81a20ad05bSMark Murray {
822fea6431SJung-uk Kim int ret;
832fea6431SJung-uk Kim
84a20ad05bSMark Murray if (mem_range_softc.mr_op == NULL)
85a20ad05bSMark Murray return (EOPNOTSUPP);
862fea6431SJung-uk Kim rw_wlock(&mr_lock);
872fea6431SJung-uk Kim ret = mem_range_softc.mr_op->set(&mem_range_softc, mrd, arg);
882fea6431SJung-uk Kim rw_wunlock(&mr_lock);
892fea6431SJung-uk Kim return (ret);
90a20ad05bSMark Murray }
91