xref: /netbsd-src/sys/dev/fdt/fdt_mbox.c (revision f80fe7971e77dc9352e0a5fe1457390c25af088c)
1 /* $NetBSD: fdt_mbox.c,v 1.1 2022/03/04 08:19:06 skrll Exp $ */
2 
3 /*-
4  * Copyright (c) 2022 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Nick Hudson
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: fdt_mbox.c,v 1.1 2022/03/04 08:19:06 skrll Exp $");
34 
35 #include <sys/param.h>
36 
37 #include <sys/bus.h>
38 #include <sys/kmem.h>
39 #include <sys/queue.h>
40 
41 #include <libfdt.h>
42 #include <dev/fdt/fdtvar.h>
43 
44 
45 struct fdtbus_mbox_controller {
46 	device_t mc_dev;
47 	int mc_phandle;
48 
49 	int mc_cells;
50 
51 	const struct fdtbus_mbox_controller_func *mc_funcs;
52 
53 	LIST_ENTRY(fdtbus_mbox_controller) mc_next;
54 };
55 
56 static LIST_HEAD(, fdtbus_mbox_controller) fdtbus_mbox_controllers =
57     LIST_HEAD_INITIALIZER(fdtbus_mbox_controllers);
58 
59 int
fdtbus_register_mbox_controller(device_t dev,int phandle,const struct fdtbus_mbox_controller_func * funcs)60 fdtbus_register_mbox_controller(device_t dev, int phandle,
61     const struct fdtbus_mbox_controller_func *funcs)
62 {
63 	struct fdtbus_mbox_controller *mc;
64 
65 	uint32_t cells;
66 	if (of_getprop_uint32(phandle, "#mbox-cells", &cells) != 0) {
67 		aprint_debug_dev(dev, "missing #mbox-cells");
68 		return EINVAL;
69 	}
70 
71 	mc = kmem_alloc(sizeof(*mc), KM_SLEEP);
72 	mc->mc_dev = dev;
73 	mc->mc_phandle = phandle;
74 	mc->mc_funcs = funcs;
75 	mc->mc_cells = cells;
76 
77 	LIST_INSERT_HEAD(&fdtbus_mbox_controllers, mc, mc_next);
78 
79 	return 0;
80 }
81 
82 static struct fdtbus_mbox_controller *
fdtbus_mbox_lookup(int phandle)83 fdtbus_mbox_lookup(int phandle)
84 {
85 	struct fdtbus_mbox_controller *mc;
86 
87 	LIST_FOREACH(mc, &fdtbus_mbox_controllers, mc_next) {
88 		if (mc->mc_phandle == phandle)
89 			return mc;
90 	}
91 
92 	return NULL;
93 }
94 
95 struct fdtbus_mbox_channel *
fdtbus_mbox_get_index(int phandle,u_int index,void (* cb)(void *),void * cbarg)96 fdtbus_mbox_get_index(int phandle, u_int index, void (*cb)(void *), void *cbarg)
97 {
98 	struct fdtbus_mbox_controller *mc;
99 	struct fdtbus_mbox_channel *mbox = NULL;
100 	void *mbox_priv = NULL;
101 	uint32_t *mboxes = NULL;
102 	uint32_t *p;
103 	u_int n;
104 	int len, resid;
105 
106 	len = OF_getproplen(phandle, "mboxes");
107 	if (len <= 0)
108 		return NULL;
109 
110 	mboxes = kmem_alloc(len, KM_SLEEP);
111 	if (OF_getprop(phandle, "mboxes", mboxes, len) != len) {
112 		kmem_free(mboxes, len);
113 		return NULL;
114 	}
115 
116 	p = mboxes;
117 	for (n = 0, resid = len; resid > 0; n++) {
118 		const int mc_phandle =
119 		    fdtbus_get_phandle_from_native(be32toh(p[0]));
120 
121 		mc = fdtbus_mbox_lookup(mc_phandle);
122 		if (mc == NULL)
123 			break;
124 
125 		u_int mbox_cells = mc->mc_cells;
126 		if (n == index) {
127 			mbox_priv = mc->mc_funcs->mc_acquire(mc->mc_dev,
128 			    mbox_cells > 0 ? &p[1] : NULL, mbox_cells * 4,
129 			    cb, cbarg);
130 			if (mbox_priv) {
131 				mbox = kmem_alloc(sizeof(*mbox), KM_SLEEP);
132 				mbox->mb_ctlr = mc;
133 				mbox->mb_priv = mbox_priv;
134 			}
135 			break;
136 		}
137 		resid -= (mbox_cells + 1) * 4;
138 		p += mbox_cells + 1;
139 	}
140 
141 	if (mboxes)
142 		kmem_free(mboxes, len);
143 
144 	return mbox;
145 }
146 
147 struct fdtbus_mbox_channel *
fdtbus_mbox_get(int phandle,const char * name,void (* cb)(void *),void * cbarg)148 fdtbus_mbox_get(int phandle, const char *name, void (*cb)(void *), void *cbarg)
149 {
150 	u_int index;
151 	int err;
152 
153 	err = fdtbus_get_index(phandle, "mbox-names", name, &index);
154 	if (err != 0)
155 		return NULL;
156 
157 	return fdtbus_mbox_get_index(phandle, index, cb, cbarg);
158 }
159 
160 void
fdtbus_mbox_put(struct fdtbus_mbox_channel * mbox)161 fdtbus_mbox_put(struct fdtbus_mbox_channel *mbox)
162 {
163 	struct fdtbus_mbox_controller *mc = mbox->mb_ctlr;
164 
165 	mc->mc_funcs->mc_release(mc->mc_dev, mbox->mb_priv);
166 	kmem_free(mbox, sizeof(*mbox));
167 }
168 
169 int
fdtbus_mbox_send(struct fdtbus_mbox_channel * mbox,const void * data,size_t len)170 fdtbus_mbox_send(struct fdtbus_mbox_channel *mbox, const void *data, size_t len)
171 {
172 	struct fdtbus_mbox_controller * const mc = mbox->mb_ctlr;
173 
174 	return mc->mc_funcs->mc_send(mc->mc_dev, mbox->mb_priv, data, len);
175 }
176 
177 int
fdtbus_mbox_recv(struct fdtbus_mbox_channel * mbox,void * data,size_t len)178 fdtbus_mbox_recv(struct fdtbus_mbox_channel *mbox, void *data, size_t len)
179 {
180 	struct fdtbus_mbox_controller * const mc = mbox->mb_ctlr;
181 
182 	return mc->mc_funcs->mc_recv(mc->mc_dev, mbox->mb_priv, data, len);
183 }
184