Home | History | Annotate | Line # | Download | only in acpi
acpi_machdep.c revision 1.6.4.2
      1  1.6.4.2  christos /* $NetBSD: acpi_machdep.c,v 1.6.4.2 2019/06/10 22:05:50 christos Exp $ */
      2  1.6.4.2  christos 
      3  1.6.4.2  christos /*-
      4  1.6.4.2  christos  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  1.6.4.2  christos  * All rights reserved.
      6  1.6.4.2  christos  *
      7  1.6.4.2  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.6.4.2  christos  * by Jared McNeill <jmcneill (at) invisible.ca>.
      9  1.6.4.2  christos  *
     10  1.6.4.2  christos  * Redistribution and use in source and binary forms, with or without
     11  1.6.4.2  christos  * modification, are permitted provided that the following conditions
     12  1.6.4.2  christos  * are met:
     13  1.6.4.2  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.6.4.2  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.6.4.2  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.6.4.2  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.6.4.2  christos  *    documentation and/or other materials provided with the distribution.
     18  1.6.4.2  christos  *
     19  1.6.4.2  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.6.4.2  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.6.4.2  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.6.4.2  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.6.4.2  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.6.4.2  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.6.4.2  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.6.4.2  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.6.4.2  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.6.4.2  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.6.4.2  christos  * POSSIBILITY OF SUCH DAMAGE.
     30  1.6.4.2  christos  */
     31  1.6.4.2  christos 
     32  1.6.4.2  christos #include "pci.h"
     33  1.6.4.2  christos 
     34  1.6.4.2  christos #include <sys/cdefs.h>
     35  1.6.4.2  christos __KERNEL_RCSID(0, "$NetBSD: acpi_machdep.c,v 1.6.4.2 2019/06/10 22:05:50 christos Exp $");
     36  1.6.4.2  christos 
     37  1.6.4.2  christos #include <sys/param.h>
     38  1.6.4.2  christos #include <sys/systm.h>
     39  1.6.4.2  christos #include <sys/bus.h>
     40  1.6.4.2  christos #include <sys/cpu.h>
     41  1.6.4.2  christos #include <sys/device.h>
     42  1.6.4.2  christos 
     43  1.6.4.2  christos #include <uvm/uvm_extern.h>
     44  1.6.4.2  christos 
     45  1.6.4.2  christos #include <dev/fdt/fdtvar.h>
     46  1.6.4.2  christos 
     47  1.6.4.2  christos #include <dev/acpi/acpica.h>
     48  1.6.4.2  christos #include <dev/acpi/acpivar.h>
     49  1.6.4.2  christos #if NPCI > 0
     50  1.6.4.2  christos #include <dev/acpi/acpi_mcfg.h>
     51  1.6.4.2  christos #endif
     52  1.6.4.2  christos 
     53  1.6.4.2  christos #include <arm/pic/picvar.h>
     54  1.6.4.2  christos 
     55  1.6.4.2  christos #include <arm/locore.h>
     56  1.6.4.2  christos 
     57  1.6.4.2  christos #include <machine/acpi_machdep.h>
     58  1.6.4.2  christos 
     59  1.6.4.2  christos extern struct bus_space arm_generic_bs_tag;
     60  1.6.4.2  christos 
     61  1.6.4.2  christos ACPI_STATUS
     62  1.6.4.2  christos acpi_md_OsInitialize(void)
     63  1.6.4.2  christos {
     64  1.6.4.2  christos 	return AE_OK;
     65  1.6.4.2  christos }
     66  1.6.4.2  christos 
     67  1.6.4.2  christos ACPI_PHYSICAL_ADDRESS
     68  1.6.4.2  christos acpi_md_OsGetRootPointer(void)
     69  1.6.4.2  christos {
     70  1.6.4.2  christos 	uint64_t pa;
     71  1.6.4.2  christos 
     72  1.6.4.2  christos 	const int chosen = OF_finddevice("/chosen");
     73  1.6.4.2  christos 	if (chosen == -1)
     74  1.6.4.2  christos 		return 0;
     75  1.6.4.2  christos 
     76  1.6.4.2  christos 	if (of_getprop_uint64(chosen, "netbsd,acpi-root-table", &pa) != 0)
     77  1.6.4.2  christos 		return 0;
     78  1.6.4.2  christos 
     79  1.6.4.2  christos 	return (ACPI_PHYSICAL_ADDRESS)pa;
     80  1.6.4.2  christos }
     81  1.6.4.2  christos 
     82  1.6.4.2  christos ACPI_STATUS
     83  1.6.4.2  christos acpi_md_OsInstallInterruptHandler(UINT32 irq, ACPI_OSD_HANDLER handler, void *context,
     84  1.6.4.2  christos     void **cookiep, const char *xname)
     85  1.6.4.2  christos {
     86  1.6.4.2  christos 	return AE_NOT_IMPLEMENTED;
     87  1.6.4.2  christos }
     88  1.6.4.2  christos 
     89  1.6.4.2  christos void
     90  1.6.4.2  christos acpi_md_OsRemoveInterruptHandler(void *cookie)
     91  1.6.4.2  christos {
     92  1.6.4.2  christos 	intr_disestablish(cookie);
     93  1.6.4.2  christos }
     94  1.6.4.2  christos 
     95  1.6.4.2  christos ACPI_STATUS
     96  1.6.4.2  christos acpi_md_OsMapMemory(ACPI_PHYSICAL_ADDRESS pa, UINT32 size, void **vap)
     97  1.6.4.2  christos {
     98  1.6.4.2  christos 	paddr_t spa, epa, curpa;
     99  1.6.4.2  christos 	vaddr_t va, curva;
    100  1.6.4.2  christos 
    101  1.6.4.2  christos 	spa = trunc_page(pa);
    102  1.6.4.2  christos 	epa = round_page(pa + size);
    103  1.6.4.2  christos 
    104  1.6.4.2  christos 	va = uvm_km_alloc(kernel_map, epa - spa, 0, UVM_KMF_VAONLY);
    105  1.6.4.2  christos 	if (va == 0)
    106  1.6.4.2  christos 		return AE_NO_MEMORY;
    107  1.6.4.2  christos 
    108  1.6.4.2  christos 	for (curpa = spa, curva = va; curpa < epa; curpa += PAGE_SIZE, curva += PAGE_SIZE)
    109  1.6.4.2  christos 		pmap_kenter_pa(curva, curpa, VM_PROT_READ | VM_PROT_WRITE, 0);
    110  1.6.4.2  christos 	pmap_update(pmap_kernel());
    111  1.6.4.2  christos 
    112  1.6.4.2  christos 	*vap = (void *)(va + (pa - spa));
    113  1.6.4.2  christos 
    114  1.6.4.2  christos 	return AE_OK;
    115  1.6.4.2  christos }
    116  1.6.4.2  christos 
    117  1.6.4.2  christos void
    118  1.6.4.2  christos acpi_md_OsUnmapMemory(void *va, UINT32 size)
    119  1.6.4.2  christos {
    120  1.6.4.2  christos 	vaddr_t ova;
    121  1.6.4.2  christos 	vsize_t osz;
    122  1.6.4.2  christos 
    123  1.6.4.2  christos 	ova = trunc_page((vaddr_t)va);
    124  1.6.4.2  christos 	osz = round_page((vaddr_t)va + size) - ova;
    125  1.6.4.2  christos 
    126  1.6.4.2  christos 	pmap_kremove(ova, osz);
    127  1.6.4.2  christos 	pmap_update(pmap_kernel());
    128  1.6.4.2  christos 	uvm_km_free(kernel_map, ova, osz, UVM_KMF_VAONLY);
    129  1.6.4.2  christos }
    130  1.6.4.2  christos 
    131  1.6.4.2  christos ACPI_STATUS
    132  1.6.4.2  christos acpi_md_OsGetPhysicalAddress(void *va, ACPI_PHYSICAL_ADDRESS *pap)
    133  1.6.4.2  christos {
    134  1.6.4.2  christos 	paddr_t pa;
    135  1.6.4.2  christos 
    136  1.6.4.2  christos 	if (!pmap_extract(pmap_kernel(), (vaddr_t)va, &pa))
    137  1.6.4.2  christos 		return AE_ERROR;
    138  1.6.4.2  christos 
    139  1.6.4.2  christos 	*pap = pa;
    140  1.6.4.2  christos 
    141  1.6.4.2  christos 	return AE_OK;
    142  1.6.4.2  christos }
    143  1.6.4.2  christos 
    144  1.6.4.2  christos BOOLEAN
    145  1.6.4.2  christos acpi_md_OsReadable(void *va, UINT32 len)
    146  1.6.4.2  christos {
    147  1.6.4.2  christos 	vaddr_t sva, eva;
    148  1.6.4.2  christos 	pt_entry_t *pte;
    149  1.6.4.2  christos 
    150  1.6.4.2  christos 	sva = trunc_page((vaddr_t)va);
    151  1.6.4.2  christos 	eva = round_page((vaddr_t)va + len);
    152  1.6.4.2  christos 
    153  1.6.4.2  christos 	if (sva < VM_MIN_KERNEL_ADDRESS)
    154  1.6.4.2  christos 		return FALSE;
    155  1.6.4.2  christos 
    156  1.6.4.2  christos 	for (; sva < eva; sva += PAGE_SIZE) {
    157  1.6.4.2  christos 		pte = kvtopte(sva);
    158  1.6.4.2  christos 		if ((*pte & (LX_BLKPAG_AF|LX_BLKPAG_AP_RO)) != (LX_BLKPAG_AF|LX_BLKPAG_AP_RO))
    159  1.6.4.2  christos 			return FALSE;
    160  1.6.4.2  christos 	}
    161  1.6.4.2  christos 
    162  1.6.4.2  christos 	return TRUE;
    163  1.6.4.2  christos }
    164  1.6.4.2  christos 
    165  1.6.4.2  christos BOOLEAN
    166  1.6.4.2  christos acpi_md_OsWritable(void *va, UINT32 len)
    167  1.6.4.2  christos {
    168  1.6.4.2  christos 	vaddr_t sva, eva;
    169  1.6.4.2  christos 	pt_entry_t *pte;
    170  1.6.4.2  christos 
    171  1.6.4.2  christos 	sva = trunc_page((vaddr_t)va);
    172  1.6.4.2  christos 	eva = round_page((vaddr_t)va + len);
    173  1.6.4.2  christos 
    174  1.6.4.2  christos 	if (sva < VM_MIN_KERNEL_ADDRESS)
    175  1.6.4.2  christos 		return FALSE;
    176  1.6.4.2  christos 
    177  1.6.4.2  christos 	for (; sva < eva; sva += PAGE_SIZE) {
    178  1.6.4.2  christos 		pte = kvtopte(sva);
    179  1.6.4.2  christos 		if ((*pte & (LX_BLKPAG_AF|LX_BLKPAG_AP_RW)) != (LX_BLKPAG_AF|LX_BLKPAG_AP_RW))
    180  1.6.4.2  christos 			return FALSE;
    181  1.6.4.2  christos 	}
    182  1.6.4.2  christos 
    183  1.6.4.2  christos 	return TRUE;
    184  1.6.4.2  christos }
    185  1.6.4.2  christos 
    186  1.6.4.2  christos void
    187  1.6.4.2  christos acpi_md_OsEnableInterrupt(void)
    188  1.6.4.2  christos {
    189  1.6.4.2  christos 	cpsie(I32_bit);
    190  1.6.4.2  christos }
    191  1.6.4.2  christos 
    192  1.6.4.2  christos void
    193  1.6.4.2  christos acpi_md_OsDisableInterrupt(void)
    194  1.6.4.2  christos {
    195  1.6.4.2  christos 	cpsid(I32_bit);
    196  1.6.4.2  christos }
    197  1.6.4.2  christos 
    198  1.6.4.2  christos void *
    199  1.6.4.2  christos acpi_md_intr_establish(uint32_t irq, int ipl, int type, int (*handler)(void *), void *arg, bool mpsafe, const char *xname)
    200  1.6.4.2  christos {
    201  1.6.4.2  christos 	return intr_establish_xname(irq, ipl, type | (mpsafe ? IST_MPSAFE : 0), handler, arg, xname);
    202  1.6.4.2  christos }
    203  1.6.4.2  christos 
    204  1.6.4.2  christos void
    205  1.6.4.2  christos acpi_md_intr_disestablish(void *ih)
    206  1.6.4.2  christos {
    207  1.6.4.2  christos 	intr_disestablish(ih);
    208  1.6.4.2  christos }
    209  1.6.4.2  christos 
    210  1.6.4.2  christos int
    211  1.6.4.2  christos acpi_md_sleep(int state)
    212  1.6.4.2  christos {
    213  1.6.4.2  christos 	printf("ERROR: ACPI sleep not implemented on this platform\n");
    214  1.6.4.2  christos 	return -1;
    215  1.6.4.2  christos }
    216  1.6.4.2  christos 
    217  1.6.4.2  christos uint32_t
    218  1.6.4.2  christos acpi_md_pdc(void)
    219  1.6.4.2  christos {
    220  1.6.4.2  christos 	return 0;
    221  1.6.4.2  christos }
    222  1.6.4.2  christos 
    223  1.6.4.2  christos uint32_t
    224  1.6.4.2  christos acpi_md_ncpus(void)
    225  1.6.4.2  christos {
    226  1.6.4.2  christos 	return kcpuset_countset(kcpuset_attached);
    227  1.6.4.2  christos }
    228  1.6.4.2  christos 
    229  1.6.4.2  christos static ACPI_STATUS
    230  1.6.4.2  christos acpi_md_madt_probe_cpu(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
    231  1.6.4.2  christos {
    232  1.6.4.2  christos 	struct acpi_softc * const sc = aux;
    233  1.6.4.2  christos 
    234  1.6.4.2  christos 	if (hdrp->Type == ACPI_MADT_TYPE_GENERIC_INTERRUPT)
    235  1.6.4.2  christos 		config_found_ia(sc->sc_dev, "acpimadtbus", hdrp, NULL);
    236  1.6.4.2  christos 
    237  1.6.4.2  christos 	return AE_OK;
    238  1.6.4.2  christos }
    239  1.6.4.2  christos 
    240  1.6.4.2  christos static ACPI_STATUS
    241  1.6.4.2  christos acpi_md_madt_probe_gic(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
    242  1.6.4.2  christos {
    243  1.6.4.2  christos 	struct acpi_softc * const sc = aux;
    244  1.6.4.2  christos 
    245  1.6.4.2  christos 	if (hdrp->Type == ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR)
    246  1.6.4.2  christos 		config_found_ia(sc->sc_dev, "acpimadtbus", hdrp, NULL);
    247  1.6.4.2  christos 
    248  1.6.4.2  christos 	return AE_OK;
    249  1.6.4.2  christos }
    250  1.6.4.2  christos 
    251  1.6.4.2  christos static ACPI_STATUS
    252  1.6.4.2  christos acpi_md_gtdt_probe(ACPI_GTDT_HEADER *hdrp, void *aux)
    253  1.6.4.2  christos {
    254  1.6.4.2  christos 	struct acpi_softc * const sc = aux;
    255  1.6.4.2  christos 
    256  1.6.4.2  christos 	config_found_ia(sc->sc_dev, "acpigtdtbus", hdrp, NULL);
    257  1.6.4.2  christos 
    258  1.6.4.2  christos 	return AE_OK;
    259  1.6.4.2  christos }
    260  1.6.4.2  christos 
    261  1.6.4.2  christos void
    262  1.6.4.2  christos acpi_md_callback(struct acpi_softc *sc)
    263  1.6.4.2  christos {
    264  1.6.4.2  christos 	ACPI_TABLE_HEADER *hdrp;
    265  1.6.4.2  christos 
    266  1.6.4.2  christos #if NPCI > 0
    267  1.6.4.2  christos 	acpimcfg_init(&arm_generic_bs_tag, NULL);
    268  1.6.4.2  christos #endif
    269  1.6.4.2  christos 
    270  1.6.4.2  christos 	if (acpi_madt_map() != AE_OK)
    271  1.6.4.2  christos 		panic("Failed to map MADT");
    272  1.6.4.2  christos 	acpi_madt_walk(acpi_md_madt_probe_cpu, sc);
    273  1.6.4.2  christos 	acpi_madt_walk(acpi_md_madt_probe_gic, sc);
    274  1.6.4.2  christos 	acpi_madt_unmap();
    275  1.6.4.2  christos 
    276  1.6.4.2  christos 	if (acpi_gtdt_map() != AE_OK)
    277  1.6.4.2  christos 		panic("Failed to map GTDT");
    278  1.6.4.2  christos 	acpi_gtdt_walk(acpi_md_gtdt_probe, sc);
    279  1.6.4.2  christos 	acpi_gtdt_unmap();
    280  1.6.4.2  christos 
    281  1.6.4.2  christos 	if (ACPI_SUCCESS(AcpiGetTable(ACPI_SIG_GTDT, 0, &hdrp)))
    282  1.6.4.2  christos 		config_found_ia(sc->sc_dev, "acpisdtbus", hdrp, NULL);
    283  1.6.4.2  christos }
    284