Home | History | Annotate | Line # | Download | only in acpi
acpi_dev.c revision 1.1.6.1
      1  1.1.6.1   thorpej /* $NetBSD: acpi_dev.c,v 1.1.6.1 2021/08/01 22:42:21 thorpej Exp $ */
      2      1.1  jmcneill 
      3      1.1  jmcneill /*-
      4      1.1  jmcneill  * Copyright (c) 2020 Jared McNeill <jmcneill (at) invisible.ca>
      5      1.1  jmcneill  * All rights reserved.
      6      1.1  jmcneill  *
      7      1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8      1.1  jmcneill  * modification, are permitted provided that the following conditions
      9      1.1  jmcneill  * are met:
     10      1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11      1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12      1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15      1.1  jmcneill  *
     16      1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17      1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18      1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19      1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20      1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21      1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22      1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23      1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24      1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25      1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26      1.1  jmcneill  * SUCH DAMAGE.
     27      1.1  jmcneill  */
     28      1.1  jmcneill 
     29      1.1  jmcneill #include <sys/cdefs.h>
     30  1.1.6.1   thorpej __KERNEL_RCSID(0, "$NetBSD: acpi_dev.c,v 1.1.6.1 2021/08/01 22:42:21 thorpej Exp $");
     31      1.1  jmcneill 
     32      1.1  jmcneill #include <sys/param.h>
     33      1.1  jmcneill #include <sys/conf.h>
     34      1.1  jmcneill #include <sys/mman.h>
     35      1.1  jmcneill 
     36      1.1  jmcneill #include <dev/acpi/acpireg.h>
     37      1.1  jmcneill #include <dev/acpi/acpivar.h>
     38      1.1  jmcneill 
     39      1.1  jmcneill #define	_COMPONENT	ACPI_BUS_COMPONENT
     40      1.1  jmcneill ACPI_MODULE_NAME	("acpi_dev")
     41      1.1  jmcneill 
     42      1.1  jmcneill static dev_type_read(acpi_read);
     43      1.1  jmcneill 
     44      1.1  jmcneill const struct cdevsw acpi_cdevsw = {
     45      1.1  jmcneill 	.d_open		= nullopen,
     46      1.1  jmcneill 	.d_close	= nullclose,
     47      1.1  jmcneill 	.d_read		= acpi_read,
     48      1.1  jmcneill 	.d_write	= nowrite,
     49      1.1  jmcneill 	.d_ioctl	= noioctl,
     50      1.1  jmcneill 	.d_stop		= nostop,
     51      1.1  jmcneill 	.d_tty		= notty,
     52      1.1  jmcneill 	.d_poll		= nopoll,
     53      1.1  jmcneill 	.d_mmap		= nommap,
     54      1.1  jmcneill 	.d_kqfilter	= nokqfilter,
     55      1.1  jmcneill 	.d_discard	= nodiscard,
     56      1.1  jmcneill 	.d_flag		= D_OTHER | D_MPSAFE,
     57      1.1  jmcneill };
     58      1.1  jmcneill 
     59      1.1  jmcneill /*
     60      1.1  jmcneill  * acpi_find_table_rsdp --
     61      1.1  jmcneill  *
     62      1.1  jmcneill  * 	Returns true if the RSDP table is found and overlaps the specified
     63      1.1  jmcneill  * 	physical address. The table's physical start address and length
     64      1.1  jmcneill  * 	are placed in 'paddr' and 'plen' when found.
     65      1.1  jmcneill  *
     66      1.1  jmcneill  */
     67      1.1  jmcneill static bool
     68      1.1  jmcneill acpi_find_table_rsdp(ACPI_PHYSICAL_ADDRESS pa,
     69      1.1  jmcneill     ACPI_PHYSICAL_ADDRESS *paddr, uint32_t *plen)
     70      1.1  jmcneill {
     71      1.1  jmcneill 	ACPI_PHYSICAL_ADDRESS table_pa;
     72      1.1  jmcneill 
     73      1.1  jmcneill 	table_pa = AcpiOsGetRootPointer();
     74      1.1  jmcneill 	if (table_pa == 0) {
     75      1.1  jmcneill 		return false;
     76      1.1  jmcneill 	}
     77      1.1  jmcneill 	if (pa >= table_pa && pa < table_pa + sizeof(ACPI_TABLE_RSDP)) {
     78      1.1  jmcneill 		*paddr = table_pa;
     79      1.1  jmcneill 		*plen = sizeof(ACPI_TABLE_RSDP);
     80      1.1  jmcneill 		return true;
     81      1.1  jmcneill 	}
     82      1.1  jmcneill 
     83      1.1  jmcneill 	return false;
     84      1.1  jmcneill }
     85      1.1  jmcneill 
     86      1.1  jmcneill /*
     87      1.1  jmcneill  * acpi_find_table_sdt --
     88      1.1  jmcneill  *
     89      1.1  jmcneill  * 	Returns true if the XSDT/RSDT table is found and overlaps the
     90      1.1  jmcneill  * 	specified physical address. The table's physical start address
     91      1.1  jmcneill  * 	and length are placed in 'paddr' and 'plen' when found.
     92      1.1  jmcneill  *
     93      1.1  jmcneill  */
     94      1.1  jmcneill static bool
     95      1.1  jmcneill acpi_find_table_sdt(ACPI_PHYSICAL_ADDRESS pa,
     96      1.1  jmcneill     ACPI_PHYSICAL_ADDRESS *paddr, uint32_t *plen)
     97      1.1  jmcneill {
     98      1.1  jmcneill 	ACPI_PHYSICAL_ADDRESS table_pa;
     99      1.1  jmcneill 	ACPI_TABLE_RSDP *rsdp;
    100      1.1  jmcneill 	ACPI_TABLE_HEADER *thdr;
    101      1.1  jmcneill 	uint32_t table_len;
    102      1.1  jmcneill 
    103      1.1  jmcneill 	table_pa = AcpiOsGetRootPointer();
    104      1.1  jmcneill 	KASSERT(table_pa != 0);
    105      1.1  jmcneill 
    106      1.1  jmcneill 	/*
    107      1.1  jmcneill 	 * Find the XSDT/RSDT using the RSDP.
    108      1.1  jmcneill 	 */
    109      1.1  jmcneill 	rsdp = AcpiOsMapMemory(table_pa, sizeof(ACPI_TABLE_RSDP));
    110      1.1  jmcneill 	if (rsdp == NULL) {
    111      1.1  jmcneill 		return false;
    112      1.1  jmcneill 	}
    113      1.1  jmcneill 	if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress) {
    114      1.1  jmcneill 		table_pa = rsdp->XsdtPhysicalAddress;
    115      1.1  jmcneill 	} else {
    116      1.1  jmcneill 		table_pa = rsdp->RsdtPhysicalAddress;
    117      1.1  jmcneill 	}
    118      1.1  jmcneill 	AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
    119      1.1  jmcneill 	if (table_pa == 0) {
    120      1.1  jmcneill 		return false;
    121      1.1  jmcneill 	}
    122      1.1  jmcneill 
    123      1.1  jmcneill 	/*
    124      1.1  jmcneill 	 * Map the XSDT/RSDT and check access.
    125      1.1  jmcneill 	 */
    126      1.1  jmcneill 	thdr = AcpiOsMapMemory(table_pa, sizeof(ACPI_TABLE_HEADER));
    127      1.1  jmcneill 	if (thdr == NULL) {
    128      1.1  jmcneill 		return false;
    129      1.1  jmcneill 	}
    130      1.1  jmcneill 	table_len = thdr->Length;
    131      1.1  jmcneill 	AcpiOsUnmapMemory(thdr, sizeof(ACPI_TABLE_HEADER));
    132      1.1  jmcneill 	if (pa >= table_pa && pa < table_pa + table_len) {
    133      1.1  jmcneill 		*paddr = table_pa;
    134      1.1  jmcneill 		*plen = table_len;
    135      1.1  jmcneill 		return true;
    136      1.1  jmcneill 	}
    137      1.1  jmcneill 
    138      1.1  jmcneill 	return false;
    139      1.1  jmcneill }
    140      1.1  jmcneill 
    141      1.1  jmcneill /*
    142      1.1  jmcneill  * acpi_find_table --
    143      1.1  jmcneill  *
    144      1.1  jmcneill  * 	Find an ACPI table that overlaps the specified physical address.
    145      1.1  jmcneill  * 	Returns true if the table is found and places the table start
    146      1.1  jmcneill  * 	address into 'paddr' and the length into 'plen'.
    147      1.1  jmcneill  *
    148      1.1  jmcneill  */
    149      1.1  jmcneill static bool
    150      1.1  jmcneill acpi_find_table(ACPI_PHYSICAL_ADDRESS pa,
    151      1.1  jmcneill     ACPI_PHYSICAL_ADDRESS *paddr, uint32_t *plen)
    152      1.1  jmcneill {
    153      1.1  jmcneill 	ACPI_TABLE_DESC *tdesc;
    154      1.1  jmcneill 	bool found_table;
    155      1.1  jmcneill 	uint32_t i;
    156      1.1  jmcneill 
    157      1.1  jmcneill 	/* Check for RSDP access. */
    158      1.1  jmcneill 	if (acpi_find_table_rsdp(pa, paddr, plen)) {
    159      1.1  jmcneill 		return true;
    160      1.1  jmcneill 	}
    161      1.1  jmcneill 
    162      1.1  jmcneill 	/* Check for XSDT/RSDT access. */
    163      1.1  jmcneill 	if (acpi_find_table_sdt(pa, paddr, plen)) {
    164      1.1  jmcneill 		return true;
    165      1.1  jmcneill 	}
    166      1.1  jmcneill 
    167      1.1  jmcneill 	/* Check for root table access. */
    168      1.1  jmcneill 	found_table = false;
    169      1.1  jmcneill 	AcpiUtAcquireMutex(ACPI_MTX_TABLES);
    170      1.1  jmcneill 	for (i = 0; i < AcpiGbl_RootTableList.CurrentTableCount; i++) {
    171      1.1  jmcneill 		tdesc = &AcpiGbl_RootTableList.Tables[i];
    172      1.1  jmcneill 		if (pa >= tdesc->Address &&
    173      1.1  jmcneill 		    pa < tdesc->Address + tdesc->Length) {
    174      1.1  jmcneill 			*paddr = tdesc->Address;
    175      1.1  jmcneill 			*plen = tdesc->Length;
    176      1.1  jmcneill 			found_table = true;
    177      1.1  jmcneill 			break;
    178      1.1  jmcneill 		}
    179      1.1  jmcneill 	}
    180      1.1  jmcneill 	AcpiUtReleaseMutex(ACPI_MTX_TABLES);
    181      1.1  jmcneill 	return found_table;
    182      1.1  jmcneill }
    183      1.1  jmcneill 
    184      1.1  jmcneill /*
    185      1.1  jmcneill  * acpi_read --
    186      1.1  jmcneill  *
    187      1.1  jmcneill  * 	Read data from an ACPI configuration table that resides in
    188      1.1  jmcneill  * 	physical memory. Only supports reading one table at a time.
    189      1.1  jmcneill  *
    190      1.1  jmcneill  */
    191      1.1  jmcneill static int
    192      1.1  jmcneill acpi_read(dev_t dev, struct uio *uio, int flag)
    193      1.1  jmcneill {
    194      1.1  jmcneill 	ACPI_PHYSICAL_ADDRESS pa, table_pa;
    195      1.1  jmcneill 	uint32_t table_len;
    196      1.1  jmcneill 	uint8_t *data;
    197      1.1  jmcneill 	int error;
    198      1.1  jmcneill 	size_t len;
    199      1.1  jmcneill 
    200      1.1  jmcneill 	if (uio->uio_rw != UIO_READ) {
    201      1.1  jmcneill 		return EPERM;
    202      1.1  jmcneill 	}
    203      1.1  jmcneill 
    204      1.1  jmcneill 	/* Make sure this is a read of a known table */
    205      1.1  jmcneill 	if (!acpi_find_table(uio->uio_offset, &table_pa, &table_len)) {
    206      1.1  jmcneill 		return EIO;
    207      1.1  jmcneill 	}
    208      1.1  jmcneill 
    209      1.1  jmcneill 	/* Copy the contents of the table to user-space */
    210      1.1  jmcneill 	pa = uio->uio_offset;
    211  1.1.6.1   thorpej 	len = uimin(table_len - (pa - table_pa), uio->uio_resid);
    212      1.1  jmcneill 	data = AcpiOsMapMemory(pa, len);
    213      1.1  jmcneill 	if (data == NULL) {
    214      1.1  jmcneill 		return ENOMEM;
    215      1.1  jmcneill 	}
    216      1.1  jmcneill 	error = uiomove(data, len, uio);
    217      1.1  jmcneill 	AcpiOsUnmapMemory(data, len);
    218      1.1  jmcneill 
    219      1.1  jmcneill 	return error;
    220      1.1  jmcneill }
    221