2 * Copyright (c) 2016 Alexander Motin <mav@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
34 #include <machine/bus.h>
35 #include <sys/rmlock.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/sysctl.h>
42 devclass_t ntb_hw_devclass;
43 SYSCTL_NODE(_hw, OID_AUTO, ntb, CTLFLAG_RW, 0, "NTB sysctls");
55 const struct ntb_ctx_ops *ctx_ops;
56 struct rmlock ctx_lock;
57 struct ntb_child *next;
61 ntb_register_device(device_t dev)
63 struct ntb_child **cpp = device_get_softc(dev);
65 int i, mw, mwu, mwt, spad, spadu, spadt, db, dbu, dbt;
68 char *n, *np, *c, *p, *name;
71 mwt = NTB_MW_COUNT(dev);
73 spadt = NTB_SPAD_COUNT(dev);
75 dbt = flsll(NTB_DB_VALID_MASK(dev));
77 device_printf(dev, "%d memory windows, %d scratchpads, "
78 "%d doorbells\n", mwt, spadt, dbt);
80 snprintf(buf, sizeof(buf), "hint.%s.%d.config", device_get_name(dev),
81 device_get_unit(dev));
82 TUNABLE_STR_FETCH(buf, cfg, sizeof(cfg));
85 while ((c = strsep(&n, ",")) != NULL) {
87 name = strsep(&np, ":");
88 if (name != NULL && name[0] == 0)
91 mw = (p && p[0] != 0) ? strtol(p, NULL, 10) : mwt - mwu;
93 spad = (p && p[0] != 0) ? strtol(p, NULL, 10) : spadt - spadu;
94 db = (np && np[0] != 0) ? strtol(np, NULL, 10) : dbt - dbu;
96 if (mw > mwt - mwu || spad > spadt - spadu || db > dbt - dbu) {
97 device_printf(dev, "Not enough resources for config\n");
101 nc = malloc(sizeof(*nc), M_DEVBUF, M_WAITOK | M_ZERO);
107 nc->dbmask = (db == 0) ? 0 : (0xffffffffffffffff >> (64 - db));
108 rm_init(&nc->ctx_lock, "ntb ctx");
109 nc->dev = device_add_child(dev, name, -1);
110 if (nc->dev == NULL) {
111 ntb_unregister_device(dev);
114 device_set_ivars(nc->dev, nc);
119 device_printf(dev, "%d \"%s\":", i, name);
121 printf(" memory windows %d", mwu);
123 printf("-%d", mwu + mw - 1);
126 printf(" scratchpads %d", spadu);
128 printf("-%d", spadu + spad - 1);
131 printf(" doorbells %d", dbu);
133 printf("-%d", dbu + db - 1);
144 bus_generic_attach(dev);
149 ntb_unregister_device(device_t dev)
151 struct ntb_child **cpp = device_get_softc(dev);
152 struct ntb_child *nc;
155 while ((nc = *cpp) != NULL) {
157 error = device_delete_child(dev, nc->dev);
160 rm_destroy(&nc->ctx_lock);
167 ntb_link_event(device_t dev)
169 struct ntb_child **cpp = device_get_softc(dev);
170 struct ntb_child *nc;
171 struct rm_priotracker ctx_tracker;
173 for (nc = *cpp; nc != NULL; nc = nc->next) {
174 rm_rlock(&nc->ctx_lock, &ctx_tracker);
175 if (nc->ctx_ops != NULL && nc->ctx_ops->link_event != NULL)
176 nc->ctx_ops->link_event(nc->ctx);
177 rm_runlock(&nc->ctx_lock, &ctx_tracker);
182 ntb_db_event(device_t dev, uint32_t vec)
184 struct ntb_child **cpp = device_get_softc(dev);
185 struct ntb_child *nc;
186 struct rm_priotracker ctx_tracker;
188 for (nc = *cpp; nc != NULL; nc = nc->next) {
189 rm_rlock(&nc->ctx_lock, &ctx_tracker);
190 if (nc->ctx_ops != NULL && nc->ctx_ops->db_event != NULL)
191 nc->ctx_ops->db_event(nc->ctx, vec);
192 rm_runlock(&nc->ctx_lock, &ctx_tracker);
197 ntb_link_is_up(device_t ntb, enum ntb_speed *speed, enum ntb_width *width)
200 return (NTB_LINK_IS_UP(device_get_parent(ntb), speed, width));
204 ntb_link_enable(device_t ntb, enum ntb_speed speed, enum ntb_width width)
206 struct ntb_child *nc = device_get_ivars(ntb);
207 struct ntb_child **cpp = device_get_softc(device_get_parent(nc->dev));
208 struct ntb_child *nc1;
210 for (nc1 = *cpp; nc1 != NULL; nc1 = nc1->next) {
217 return (NTB_LINK_ENABLE(device_get_parent(ntb), speed, width));
221 ntb_link_disable(device_t ntb)
223 struct ntb_child *nc = device_get_ivars(ntb);
224 struct ntb_child **cpp = device_get_softc(device_get_parent(nc->dev));
225 struct ntb_child *nc1;
230 for (nc1 = *cpp; nc1 != NULL; nc1 = nc1->next) {
234 return (NTB_LINK_DISABLE(device_get_parent(ntb)));
238 ntb_link_enabled(device_t ntb)
240 struct ntb_child *nc = device_get_ivars(ntb);
242 return (nc->enabled && NTB_LINK_ENABLED(device_get_parent(ntb)));
246 ntb_set_ctx(device_t ntb, void *ctx, const struct ntb_ctx_ops *ctx_ops)
248 struct ntb_child *nc = device_get_ivars(ntb);
250 if (ctx == NULL || ctx_ops == NULL)
253 rm_wlock(&nc->ctx_lock);
254 if (nc->ctx_ops != NULL) {
255 rm_wunlock(&nc->ctx_lock);
259 nc->ctx_ops = ctx_ops;
260 rm_wunlock(&nc->ctx_lock);
266 ntb_get_ctx(device_t ntb, const struct ntb_ctx_ops **ctx_ops)
268 struct ntb_child *nc = device_get_ivars(ntb);
270 KASSERT(nc->ctx != NULL && nc->ctx_ops != NULL, ("bogus"));
272 *ctx_ops = nc->ctx_ops;
277 ntb_clear_ctx(device_t ntb)
279 struct ntb_child *nc = device_get_ivars(ntb);
281 rm_wlock(&nc->ctx_lock);
284 rm_wunlock(&nc->ctx_lock);
288 ntb_mw_count(device_t ntb)
290 struct ntb_child *nc = device_get_ivars(ntb);
296 ntb_mw_get_range(device_t ntb, unsigned mw_idx, vm_paddr_t *base,
297 caddr_t *vbase, size_t *size, size_t *align, size_t *align_size,
300 struct ntb_child *nc = device_get_ivars(ntb);
302 return (NTB_MW_GET_RANGE(device_get_parent(ntb), mw_idx + nc->mwoff,
303 base, vbase, size, align, align_size, plimit));
307 ntb_mw_set_trans(device_t ntb, unsigned mw_idx, bus_addr_t addr, size_t size)
309 struct ntb_child *nc = device_get_ivars(ntb);
311 return (NTB_MW_SET_TRANS(device_get_parent(ntb), mw_idx + nc->mwoff,
316 ntb_mw_clear_trans(device_t ntb, unsigned mw_idx)
318 struct ntb_child *nc = device_get_ivars(ntb);
320 return (NTB_MW_CLEAR_TRANS(device_get_parent(ntb), mw_idx + nc->mwoff));
324 ntb_mw_get_wc(device_t ntb, unsigned mw_idx, vm_memattr_t *mode)
326 struct ntb_child *nc = device_get_ivars(ntb);
328 return (NTB_MW_GET_WC(device_get_parent(ntb), mw_idx + nc->mwoff, mode));
332 ntb_mw_set_wc(device_t ntb, unsigned mw_idx, vm_memattr_t mode)
334 struct ntb_child *nc = device_get_ivars(ntb);
336 return (NTB_MW_SET_WC(device_get_parent(ntb), mw_idx + nc->mwoff, mode));
340 ntb_spad_count(device_t ntb)
342 struct ntb_child *nc = device_get_ivars(ntb);
344 return (nc->spadcnt);
348 ntb_spad_clear(device_t ntb)
350 struct ntb_child *nc = device_get_ivars(ntb);
353 for (i = 0; i < nc->spadcnt; i++)
354 NTB_SPAD_WRITE(device_get_parent(ntb), i + nc->spadoff, 0);
358 ntb_spad_write(device_t ntb, unsigned int idx, uint32_t val)
360 struct ntb_child *nc = device_get_ivars(ntb);
362 return (NTB_SPAD_WRITE(device_get_parent(ntb), idx + nc->spadoff, val));
366 ntb_spad_read(device_t ntb, unsigned int idx, uint32_t *val)
368 struct ntb_child *nc = device_get_ivars(ntb);
370 return (NTB_SPAD_READ(device_get_parent(ntb), idx + nc->spadoff, val));
374 ntb_peer_spad_write(device_t ntb, unsigned int idx, uint32_t val)
376 struct ntb_child *nc = device_get_ivars(ntb);
378 return (NTB_PEER_SPAD_WRITE(device_get_parent(ntb), idx + nc->spadoff,
383 ntb_peer_spad_read(device_t ntb, unsigned int idx, uint32_t *val)
385 struct ntb_child *nc = device_get_ivars(ntb);
387 return (NTB_PEER_SPAD_READ(device_get_parent(ntb), idx + nc->spadoff,
392 ntb_db_valid_mask(device_t ntb)
394 struct ntb_child *nc = device_get_ivars(ntb);
400 ntb_db_vector_count(device_t ntb)
403 return (NTB_DB_VECTOR_COUNT(device_get_parent(ntb)));
407 ntb_db_vector_mask(device_t ntb, uint32_t vector)
409 struct ntb_child *nc = device_get_ivars(ntb);
411 return ((NTB_DB_VECTOR_MASK(device_get_parent(ntb), vector)
412 >> nc->dboff) & nc->dbmask);
416 ntb_peer_db_addr(device_t ntb, bus_addr_t *db_addr, vm_size_t *db_size)
419 return (NTB_PEER_DB_ADDR(device_get_parent(ntb), db_addr, db_size));
423 ntb_db_clear(device_t ntb, uint64_t bits)
425 struct ntb_child *nc = device_get_ivars(ntb);
427 return (NTB_DB_CLEAR(device_get_parent(ntb), bits << nc->dboff));
431 ntb_db_clear_mask(device_t ntb, uint64_t bits)
433 struct ntb_child *nc = device_get_ivars(ntb);
435 return (NTB_DB_CLEAR_MASK(device_get_parent(ntb), bits << nc->dboff));
439 ntb_db_read(device_t ntb)
441 struct ntb_child *nc = device_get_ivars(ntb);
443 return ((NTB_DB_READ(device_get_parent(ntb)) >> nc->dboff)
448 ntb_db_set_mask(device_t ntb, uint64_t bits)
450 struct ntb_child *nc = device_get_ivars(ntb);
452 return (NTB_DB_SET_MASK(device_get_parent(ntb), bits << nc->dboff));
456 ntb_peer_db_set(device_t ntb, uint64_t bits)
458 struct ntb_child *nc = device_get_ivars(ntb);
460 return (NTB_PEER_DB_SET(device_get_parent(ntb), bits << nc->dboff));
463 MODULE_VERSION(ntb, 1);