1/*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2016-2019 Ruslan Bukin <br@bsdpad.com> 5 * 6 * This software was developed by SRI International and the University of 7 * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237 8 * ("CTSRD"), as part of the DARPA CRASH research programme. 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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33__FBSDID("$FreeBSD$"); 34 35#include "opt_platform.h" 36#include <sys/param.h> 37#include <sys/conf.h> 38#include <sys/bus.h> 39#include <sys/kernel.h> 40#include <sys/queue.h> 41#include <sys/kobj.h> 42#include <sys/malloc.h> 43#include <sys/limits.h> 44#include <sys/lock.h> 45#include <sys/sysctl.h> 46#include <sys/systm.h> 47 48#include <machine/bus.h> 49 50#ifdef FDT 51#include <dev/fdt/fdt_common.h> 52#include <dev/ofw/ofw_bus.h> 53#include <dev/ofw/ofw_bus_subr.h> 54#endif 55 56#include <dev/xdma/xdma.h> 57 58#include <xdma_if.h> 59 60/* 61 * Multiple xDMA controllers may work with single DMA device, 62 * so we have global lock for physical channel management. 63 */ 64static struct mtx xdma_mtx; 65 66#define XDMA_LOCK() mtx_lock(&xdma_mtx) 67#define XDMA_UNLOCK() mtx_unlock(&xdma_mtx) 68#define XDMA_ASSERT_LOCKED() mtx_assert(&xdma_mtx, MA_OWNED) 69 70#define FDT_REG_CELLS 4 71 72/* 73 * Allocate virtual xDMA channel. 74 */ 75xdma_channel_t * 76xdma_channel_alloc(xdma_controller_t *xdma, uint32_t caps) 77{ 78 xdma_channel_t *xchan; 79 int ret; 80 81 xchan = malloc(sizeof(xdma_channel_t), M_XDMA, M_WAITOK | M_ZERO); 82 xchan->xdma = xdma; 83 xchan->caps = caps; 84 85 XDMA_LOCK(); 86 87 /* Request a real channel from hardware driver. */ 88 ret = XDMA_CHANNEL_ALLOC(xdma->dma_dev, xchan); 89 if (ret != 0) { 90 device_printf(xdma->dev, 91 "%s: Can't request hardware channel.\n", __func__); 92 XDMA_UNLOCK(); 93 free(xchan, M_XDMA); 94 95 return (NULL); 96 } 97 98 TAILQ_INIT(&xchan->ie_handlers); 99 100 mtx_init(&xchan->mtx_lock, "xDMA chan", NULL, MTX_DEF); 101 mtx_init(&xchan->mtx_qin_lock, "xDMA qin", NULL, MTX_DEF); 102 mtx_init(&xchan->mtx_qout_lock, "xDMA qout", NULL, MTX_DEF); 103 mtx_init(&xchan->mtx_bank_lock, "xDMA bank", NULL, MTX_DEF); 104 mtx_init(&xchan->mtx_proc_lock, "xDMA proc", NULL, MTX_DEF); 105 106 TAILQ_INIT(&xchan->bank); 107 TAILQ_INIT(&xchan->queue_in); 108 TAILQ_INIT(&xchan->queue_out); 109 TAILQ_INIT(&xchan->processing); 110 111 TAILQ_INSERT_TAIL(&xdma->channels, xchan, xchan_next); 112 113 XDMA_UNLOCK(); 114 115 return (xchan); 116} 117 118int 119xdma_channel_free(xdma_channel_t *xchan) 120{ 121 xdma_controller_t *xdma; 122 int err; 123 124 xdma = xchan->xdma; 125 KASSERT(xdma != NULL, ("xdma is NULL")); 126 127 XDMA_LOCK(); 128 129 /* Free the real DMA channel. */ 130 err = XDMA_CHANNEL_FREE(xdma->dma_dev, xchan); 131 if (err != 0) { 132 device_printf(xdma->dev, 133 "%s: Can't free real hw channel.\n", __func__); 134 XDMA_UNLOCK(); 135 return (-1); 136 } 137 138 if (xchan->flags & XCHAN_TYPE_SG) 139 xdma_channel_free_sg(xchan); 140 141 xdma_teardown_all_intr(xchan); 142 143 mtx_destroy(&xchan->mtx_lock); 144 mtx_destroy(&xchan->mtx_qin_lock); 145 mtx_destroy(&xchan->mtx_qout_lock); 146 mtx_destroy(&xchan->mtx_bank_lock); 147 mtx_destroy(&xchan->mtx_proc_lock); 148 149 TAILQ_REMOVE(&xdma->channels, xchan, xchan_next); 150 151 free(xchan, M_XDMA); 152 153 XDMA_UNLOCK(); 154 155 return (0); 156} 157 158int 159xdma_setup_intr(xdma_channel_t *xchan, 160 int (*cb)(void *, xdma_transfer_status_t *), 161 void *arg, void **ihandler) 162{ 163 struct xdma_intr_handler *ih; 164 xdma_controller_t *xdma; 165 166 xdma = xchan->xdma; 167 KASSERT(xdma != NULL, ("xdma is NULL")); 168 169 /* Sanity check. */ 170 if (cb == NULL) { 171 device_printf(xdma->dev, 172 "%s: Can't setup interrupt handler.\n", 173 __func__); 174 175 return (-1); 176 } 177 178 ih = malloc(sizeof(struct xdma_intr_handler), 179 M_XDMA, M_WAITOK | M_ZERO); 180 ih->cb = cb; 181 ih->cb_user = arg; 182 183 XCHAN_LOCK(xchan); 184 TAILQ_INSERT_TAIL(&xchan->ie_handlers, ih, ih_next); 185 XCHAN_UNLOCK(xchan); 186 187 if (ihandler != NULL) 188 *ihandler = ih; 189 190 return (0); 191} 192 193int 194xdma_teardown_intr(xdma_channel_t *xchan, struct xdma_intr_handler *ih) 195{ 196 xdma_controller_t *xdma; 197 198 xdma = xchan->xdma; 199 KASSERT(xdma != NULL, ("xdma is NULL")); 200 201 /* Sanity check. */ 202 if (ih == NULL) { 203 device_printf(xdma->dev, 204 "%s: Can't teardown interrupt.\n", __func__); 205 return (-1); 206 } 207 208 TAILQ_REMOVE(&xchan->ie_handlers, ih, ih_next); 209 free(ih, M_XDMA); 210 211 return (0); 212} 213 214int 215xdma_teardown_all_intr(xdma_channel_t *xchan) 216{ 217 struct xdma_intr_handler *ih_tmp; 218 struct xdma_intr_handler *ih; 219 xdma_controller_t *xdma; 220 221 xdma = xchan->xdma; 222 KASSERT(xdma != NULL, ("xdma is NULL")); 223 224 TAILQ_FOREACH_SAFE(ih, &xchan->ie_handlers, ih_next, ih_tmp) { 225 TAILQ_REMOVE(&xchan->ie_handlers, ih, ih_next); 226 free(ih, M_XDMA); 227 } 228 229 return (0); 230} 231 232int 233xdma_request(xdma_channel_t *xchan, struct xdma_request *req) 234{ 235 xdma_controller_t *xdma; 236 int ret; 237 238 xdma = xchan->xdma; 239 240 KASSERT(xdma != NULL, ("xdma is NULL")); 241 242 XCHAN_LOCK(xchan); 243 ret = XDMA_CHANNEL_REQUEST(xdma->dma_dev, xchan, req); 244 if (ret != 0) { 245 device_printf(xdma->dev, 246 "%s: Can't request a transfer.\n", __func__); 247 XCHAN_UNLOCK(xchan); 248 249 return (-1); 250 } 251 XCHAN_UNLOCK(xchan); 252 253 return (0); 254} 255 256int 257xdma_control(xdma_channel_t *xchan, enum xdma_command cmd) 258{ 259 xdma_controller_t *xdma; 260 int ret; 261 262 xdma = xchan->xdma; 263 KASSERT(xdma != NULL, ("xdma is NULL")); 264 265 ret = XDMA_CHANNEL_CONTROL(xdma->dma_dev, xchan, cmd); 266 if (ret != 0) { 267 device_printf(xdma->dev, 268 "%s: Can't process command.\n", __func__); 269 return (-1); 270 } 271 272 return (0); 273} 274 275void 276xdma_callback(xdma_channel_t *xchan, xdma_transfer_status_t *status) 277{ 278 struct xdma_intr_handler *ih_tmp; 279 struct xdma_intr_handler *ih; 280 xdma_controller_t *xdma; 281 282 xdma = xchan->xdma; 283 KASSERT(xdma != NULL, ("xdma is NULL")); 284 285 TAILQ_FOREACH_SAFE(ih, &xchan->ie_handlers, ih_next, ih_tmp) 286 if (ih->cb != NULL) 287 ih->cb(ih->cb_user, status); 288 289 if (xchan->flags & XCHAN_TYPE_SG) 290 xdma_queue_submit(xchan); 291} 292 293#ifdef FDT 294/* 295 * Notify the DMA driver we have machine-dependent data in FDT. 296 */ 297static int 298xdma_ofw_md_data(xdma_controller_t *xdma, pcell_t *cells, int ncells) 299{ 300 uint32_t ret; 301 302 ret = XDMA_OFW_MD_DATA(xdma->dma_dev, 303 cells, ncells, (void **)&xdma->data); 304 305 return (ret); 306} 307 308static int 309xdma_handle_mem_node(vmem_t *vmem, phandle_t memory) 310{ 311 pcell_t reg[FDT_REG_CELLS * FDT_MEM_REGIONS]; 312 pcell_t *regp; 313 int addr_cells, size_cells; 314 int i, reg_len, ret, tuple_size, tuples; 315 u_long mem_start, mem_size; 316 317 if ((ret = fdt_addrsize_cells(OF_parent(memory), &addr_cells, 318 &size_cells)) != 0) 319 return (ret); 320 321 if (addr_cells > 2) 322 return (ERANGE); 323 324 tuple_size = sizeof(pcell_t) * (addr_cells + size_cells); 325 reg_len = OF_getproplen(memory, "reg"); 326 if (reg_len <= 0 || reg_len > sizeof(reg)) 327 return (ERANGE); 328 329 if (OF_getprop(memory, "reg", reg, reg_len) <= 0) 330 return (ENXIO); 331 332 tuples = reg_len / tuple_size; 333 regp = (pcell_t *)® 334 for (i = 0; i < tuples; i++) { 335 ret = fdt_data_to_res(regp, addr_cells, size_cells, 336 &mem_start, &mem_size); 337 if (ret != 0) 338 return (ret); 339 340 vmem_add(vmem, mem_start, mem_size, 0); 341 regp += addr_cells + size_cells; 342 } 343 344 return (0); 345} 346 347vmem_t * 348xdma_get_memory(device_t dev) 349{ 350 phandle_t mem_node, node; 351 pcell_t mem_handle; 352 vmem_t *vmem; 353 354 node = ofw_bus_get_node(dev); 355 if (node <= 0) { 356 device_printf(dev, 357 "%s called on not ofw based device.\n", __func__); 358 return (NULL); 359 } 360 361 if (!OF_hasprop(node, "memory-region")) 362 return (NULL); 363 364 if (OF_getencprop(node, "memory-region", (void *)&mem_handle, 365 sizeof(mem_handle)) <= 0) 366 return (NULL); 367 368 vmem = vmem_create("xDMA vmem", 0, 0, PAGE_SIZE, 369 PAGE_SIZE, M_BESTFIT | M_WAITOK); 370 if (vmem == NULL) 371 return (NULL); 372 373 mem_node = OF_node_from_xref(mem_handle); 374 if (xdma_handle_mem_node(vmem, mem_node) != 0) { 375 vmem_destroy(vmem); 376 return (NULL); 377 } 378 379 return (vmem); 380} 381 382void 383xdma_put_memory(vmem_t *vmem) 384{ 385 386 vmem_destroy(vmem); 387} 388 389void 390xchan_set_memory(xdma_channel_t *xchan, vmem_t *vmem) 391{ 392 393 xchan->vmem = vmem; 394} 395 396/* 397 * Allocate xdma controller. 398 */ 399xdma_controller_t * 400xdma_ofw_get(device_t dev, const char *prop) 401{ 402 phandle_t node, parent; 403 xdma_controller_t *xdma; 404 device_t dma_dev; 405 pcell_t *cells; 406 int ncells; 407 int error; 408 int ndmas; 409 int idx; 410 411 node = ofw_bus_get_node(dev); 412 if (node <= 0) 413 device_printf(dev, 414 "%s called on not ofw based device.\n", __func__); 415 416 error = ofw_bus_parse_xref_list_get_length(node, 417 "dmas", "#dma-cells", &ndmas); 418 if (error) { 419 device_printf(dev, 420 "%s can't get dmas list.\n", __func__); 421 return (NULL); 422 } 423 424 if (ndmas == 0) { 425 device_printf(dev, 426 "%s dmas list is empty.\n", __func__); 427 return (NULL); 428 } 429 430 error = ofw_bus_find_string_index(node, "dma-names", prop, &idx); 431 if (error != 0) { 432 device_printf(dev, 433 "%s can't find string index.\n", __func__); 434 return (NULL); 435 } 436 437 error = ofw_bus_parse_xref_list_alloc(node, "dmas", "#dma-cells", 438 idx, &parent, &ncells, &cells); 439 if (error != 0) { 440 device_printf(dev, 441 "%s can't get dma device xref.\n", __func__); 442 return (NULL); 443 } 444 445 dma_dev = OF_device_from_xref(parent); 446 if (dma_dev == NULL) { 447 device_printf(dev, 448 "%s can't get dma device.\n", __func__); 449 return (NULL); 450 } 451 452 xdma = malloc(sizeof(struct xdma_controller), 453 M_XDMA, M_WAITOK | M_ZERO); 454 xdma->dev = dev; 455 xdma->dma_dev = dma_dev; 456 457 TAILQ_INIT(&xdma->channels); 458 459 xdma_ofw_md_data(xdma, cells, ncells); 460 free(cells, M_OFWPROP); 461 462 return (xdma); 463} 464#endif 465 466/* 467 * Free xDMA controller object. 468 */ 469int 470xdma_put(xdma_controller_t *xdma) 471{ 472 473 XDMA_LOCK(); 474 475 /* Ensure no channels allocated. */ 476 if (!TAILQ_EMPTY(&xdma->channels)) { 477 device_printf(xdma->dev, "%s: Can't free xDMA\n", __func__); 478 return (-1); 479 } 480 481 free(xdma->data, M_DEVBUF); 482 free(xdma, M_XDMA); 483 484 XDMA_UNLOCK(); 485 486 return (0); 487} 488 489static void 490xdma_init(void) 491{ 492 493 mtx_init(&xdma_mtx, "xDMA", NULL, MTX_DEF); 494} 495 496SYSINIT(xdma, SI_SUB_DRIVERS, SI_ORDER_FIRST, xdma_init, NULL); 497