Tue Dec 31 13:54:22 2019 UTC ()
Create bus_dma tags for each device node based on _CCA and _DMA properties
found by walking up the device node tree. These tags encode range
restrictions, address translations, and whether or not the device is
cache coherent.


(jmcneill)
diff -r1.16 -r1.17 src/sys/arch/arm/acpi/acpi_machdep.c

cvs diff -r1.16 -r1.17 src/sys/arch/arm/acpi/acpi_machdep.c (expand / switch to unified diff)

--- src/sys/arch/arm/acpi/acpi_machdep.c 2019/12/31 11:42:46 1.16
+++ src/sys/arch/arm/acpi/acpi_machdep.c 2019/12/31 13:54:22 1.17
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: acpi_machdep.c,v 1.16 2019/12/31 11:42:46 jmcneill Exp $ */ 1/* $NetBSD: acpi_machdep.c,v 1.17 2019/12/31 13:54:22 jmcneill Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2018 The NetBSD Foundation, Inc. 4 * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jared McNeill <jmcneill@invisible.ca>. 8 * by Jared McNeill <jmcneill@invisible.ca>.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -22,33 +22,34 @@ @@ -22,33 +22,34 @@
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 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 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32#include "pci.h" 32#include "pci.h"
33 33
34#include <sys/cdefs.h> 34#include <sys/cdefs.h>
35__KERNEL_RCSID(0, "$NetBSD: acpi_machdep.c,v 1.16 2019/12/31 11:42:46 jmcneill Exp $"); 35__KERNEL_RCSID(0, "$NetBSD: acpi_machdep.c,v 1.17 2019/12/31 13:54:22 jmcneill Exp $");
36 36
37#include <sys/param.h> 37#include <sys/param.h>
38#include <sys/systm.h> 38#include <sys/systm.h>
39#include <sys/bus.h> 39#include <sys/bus.h>
40#include <sys/cpu.h> 40#include <sys/cpu.h>
41#include <sys/device.h> 41#include <sys/device.h>
 42#include <sys/kmem.h>
42 43
43#include <uvm/uvm_extern.h> 44#include <uvm/uvm_extern.h>
44 45
45#include <dev/fdt/fdtvar.h> 46#include <dev/fdt/fdtvar.h>
46 47
47#include <dev/acpi/acpica.h> 48#include <dev/acpi/acpica.h>
48#include <dev/acpi/acpivar.h> 49#include <dev/acpi/acpivar.h>
49#if NPCI > 0 50#if NPCI > 0
50#include <dev/acpi/acpi_mcfg.h> 51#include <dev/acpi/acpi_mcfg.h>
51#endif 52#endif
52 53
53#include <arm/arm/efi_runtime.h> 54#include <arm/arm/efi_runtime.h>
54 55
@@ -362,100 +363,135 @@ acpi_md_callback(struct acpi_softc *sc) @@ -362,100 +363,135 @@ acpi_md_callback(struct acpi_softc *sc)
362 panic("Failed to map GTDT"); 363 panic("Failed to map GTDT");
363 acpi_gtdt_walk(acpi_md_gtdt_probe, sc); 364 acpi_gtdt_walk(acpi_md_gtdt_probe, sc);
364 acpi_gtdt_unmap(); 365 acpi_gtdt_unmap();
365 366
366 if (ACPI_SUCCESS(AcpiGetTable(ACPI_SIG_GTDT, 0, &hdrp))) 367 if (ACPI_SUCCESS(AcpiGetTable(ACPI_SIG_GTDT, 0, &hdrp)))
367 config_found_ia(sc->sc_dev, "acpisdtbus", hdrp, NULL); 368 config_found_ia(sc->sc_dev, "acpisdtbus", hdrp, NULL);
368} 369}
369 370
370static const char * const module_hid[] = { 371static const char * const module_hid[] = {
371 "ACPI0004", /* Module device */ 372 "ACPI0004", /* Module device */
372 NULL 373 NULL
373}; 374};
374 375
375static bus_dma_tag_t 376static ACPI_HANDLE
376arm_acpi_dma_tag_subregion(struct acpi_softc *sc, bus_dma_tag_t dmat, 377arm_acpi_dma_module(struct acpi_softc *sc, struct acpi_devnode *ad)
377 ACPI_HANDLE handle) 378{
 379 ACPI_HANDLE tmp;
 380 ACPI_STATUS rv;
 381
 382 /*
 383 * Search up the tree for a module device with a _DMA method.
 384 */
 385 for (; ad != NULL; ad = ad->ad_parent) {
 386 if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
 387 continue;
 388 if (!acpi_match_hid(ad->ad_devinfo, module_hid))
 389 continue;
 390 rv = AcpiGetHandle(ad->ad_handle, "_DMA", &tmp);
 391 if (ACPI_SUCCESS(rv))
 392 return ad->ad_handle;
 393 }
 394
 395 return NULL;
 396}
 397
 398static void
 399arm_acpi_dma_init_ranges(struct acpi_softc *sc, struct acpi_devnode *ad,
 400 struct arm32_bus_dma_tag *dmat, uint32_t flags)
378{ 401{
379 struct acpi_resources res; 402 struct acpi_resources res;
380 struct acpi_mem *mem; 403 struct acpi_mem *mem;
381 bus_dma_tag_t newtag; 404 ACPI_HANDLE module;
382 ACPI_STATUS rv; 405 ACPI_STATUS rv;
383 int error; 406 int n;
384 407
385 rv = acpi_resource_parse(sc->sc_dev, handle, "_DMA", &res, 408 module = arm_acpi_dma_module(sc, ad->ad_parent);
386 &acpi_resource_parse_ops_quiet); 409 if (module == NULL) {
387 if (ACPI_FAILURE(rv)) 410default_tag:
388 return dmat; /* no translation required */ 411 /* No translation required */
 412 dmat->_nranges = 1;
 413 dmat->_ranges = kmem_zalloc(sizeof(*dmat->_ranges), KM_SLEEP);
 414 dmat->_ranges[0].dr_sysbase = 0;
 415 dmat->_ranges[0].dr_busbase = 0;
 416 dmat->_ranges[0].dr_len = UINTPTR_MAX;
 417 dmat->_ranges[0].dr_flags = flags;
 418 return;
 419 }
389 420
390 mem = acpi_res_mem(&res, 0); 421 rv = acpi_resource_parse(sc->sc_dev, module, "_DMA", &res,
391 if (mem == NULL) 422 &acpi_resource_parse_ops_quiet);
392 goto done; 423 if (ACPI_FAILURE(rv)) {
393 
394 aprint_debug_dev(sc->sc_dev, "_DMA range %#lx-%#lx\n", 
395 mem->ar_base, mem->ar_base + mem->ar_length - 1); 
396 
397 error = bus_dmatag_subregion(dmat, 
398 mem->ar_base, mem->ar_base + mem->ar_length - 1, 
399 &newtag, BUS_DMA_WAITOK); 
400 if (error != 0) { 
401 aprint_error_dev(sc->sc_dev, 424 aprint_error_dev(sc->sc_dev,
402 "_DMA subregion failed: %d\n", error); 425 "failed to parse _DMA on %s: %s\n",
403 goto done; 426 acpi_name(module), AcpiFormatException(rv));
 427 goto default_tag;
 428 }
 429 if (res.ar_nmem == 0) {
 430 acpi_resource_cleanup(&res);
 431 goto default_tag;
404 } 432 }
405 dmat = newtag; 
406 433
407done: 434 dmat->_nranges = res.ar_nmem;
408 acpi_resource_cleanup(&res); 435 dmat->_ranges = kmem_zalloc(sizeof(*dmat->_ranges) * res.ar_nmem,
 436 KM_SLEEP);
 437
 438 for (n = 0; n < res.ar_nmem; n++) {
 439 mem = acpi_res_mem(&res, n);
 440 dmat->_ranges[n].dr_busbase = mem->ar_base;
 441 dmat->_ranges[n].dr_sysbase = mem->ar_base;
 442 if (mem->ar_decode == ACPI_POS_DECODE)
 443 dmat->_ranges[n].dr_sysbase += mem->ar_offset;
 444 else
 445 dmat->_ranges[n].dr_sysbase -= mem->ar_offset;
 446 dmat->_ranges[n].dr_len = mem->ar_length;
 447 dmat->_ranges[n].dr_flags = flags;
 448
 449 aprint_debug_dev(sc->sc_dev,
 450 "%s: DMA sysbase %#lx busbase %#lx len %#lx%s\n",
 451 acpi_name(ad->ad_handle),
 452 dmat->_ranges[n].dr_sysbase,
 453 dmat->_ranges[n].dr_busbase,
 454 dmat->_ranges[n].dr_len,
 455 flags ? " (coherent)" : "");
 456 }
409 457
410 return dmat; 458 acpi_resource_cleanup(&res);
411} 459}
412 460
413static ACPI_HANDLE 461static uint32_t
414arm_acpi_dma_module(struct acpi_softc *sc, struct acpi_devnode *ad) 462arm_acpi_dma_flags(struct acpi_softc *sc, struct acpi_devnode *ad)
415{ 463{
416 ACPI_HANDLE tmp; 464 ACPI_INTEGER cca = 1; /* default cache coherent */
417 ACPI_STATUS rv; 465 ACPI_STATUS rv;
418 466
419 /* 
420 * Search up the tree for a module device with a _DMA method. 
421 */ 
422 for (; ad != NULL; ad = ad->ad_parent) { 467 for (; ad != NULL; ad = ad->ad_parent) {
423 if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE) 468 if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
424 continue; 469 continue;
425 if (!acpi_match_hid(ad->ad_devinfo, module_hid)) 470
426 continue; 471 rv = acpi_eval_integer(ad->ad_handle, "_CCA", &cca);
427 rv = AcpiGetHandle(ad->ad_handle, "_DMA", &tmp); 
428 if (ACPI_SUCCESS(rv)) 472 if (ACPI_SUCCESS(rv))
429 return ad->ad_handle; 473 break;
430 } 474 }
431 475
432 return NULL; 476 return cca ? _BUS_DMAMAP_COHERENT : 0;
433} 477}
434 478
 479
435bus_dma_tag_t 480bus_dma_tag_t
436arm_acpi_dma_tag(struct acpi_softc *sc, struct acpi_devnode *ad) 481arm_acpi_dma_tag(struct acpi_softc *sc, struct acpi_devnode *ad)
437{ 482{
438 ACPI_HANDLE module; 483 struct arm32_bus_dma_tag *dmat;
439 ACPI_INTEGER cca; 
440 bus_dma_tag_t dmat; 
441 484
442 if (ACPI_FAILURE(acpi_eval_integer(ad->ad_handle, "_CCA", &cca))) 485 if (ad->ad_dmat != NULL)
443 cca = 1; 486 return ad->ad_dmat;
 487
 488 dmat = kmem_alloc(sizeof(*dmat), KM_SLEEP);
 489 *dmat = arm_generic_dma_tag;
444 490
445 if (cca) 491 const uint32_t flags = arm_acpi_dma_flags(sc, ad);
446 dmat = &acpi_coherent_dma_tag; 492 arm_acpi_dma_init_ranges(sc, ad, dmat, flags);
447 else 
448 dmat = &arm_generic_dma_tag; 
449 
450 /* 
451 * If a parent device is a bus, it may define valid DMA ranges 
452 * and translations for child nodes. 
453 */ 
454 module = arm_acpi_dma_module(sc, ad); 
455 if (module != NULL) 
456 dmat = arm_acpi_dma_tag_subregion(sc, dmat, module); 
457 493
458 return dmat; 494 return dmat;
459} 495}
460__strong_alias(acpi_get_dma_tag,arm_acpi_dma_tag); 496__strong_alias(acpi_get_dma_tag,arm_acpi_dma_tag);
461__strong_alias(acpi_get_dma64_tag,arm_acpi_dma_tag); 497__strong_alias(acpi_get_dma64_tag,arm_acpi_dma_tag);