Home | History | Annotate | Line # | Download | only in acpi
acpi_dev.c revision 1.2.16.1
      1  1.2.16.1  perseant /* $NetBSD: acpi_dev.c,v 1.2.16.1 2025/08/02 05:56:31 perseant 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.2.16.1  perseant __KERNEL_RCSID(0, "$NetBSD: acpi_dev.c,v 1.2.16.1 2025/08/02 05:56:31 perseant 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.2.16.1  perseant 	ACPI_TABLE_TCPA_HDR *tcpa = NULL;
    155  1.2.16.1  perseant 	size_t tcpa_tdesc_len = 0;
    156       1.1  jmcneill 	bool found_table;
    157       1.1  jmcneill 	uint32_t i;
    158       1.1  jmcneill 
    159       1.1  jmcneill 	/* Check for RSDP access. */
    160       1.1  jmcneill 	if (acpi_find_table_rsdp(pa, paddr, plen)) {
    161       1.1  jmcneill 		return true;
    162       1.1  jmcneill 	}
    163       1.1  jmcneill 
    164       1.1  jmcneill 	/* Check for XSDT/RSDT access. */
    165       1.1  jmcneill 	if (acpi_find_table_sdt(pa, paddr, plen)) {
    166       1.1  jmcneill 		return true;
    167       1.1  jmcneill 	}
    168       1.1  jmcneill 
    169       1.1  jmcneill 	/* Check for root table access. */
    170       1.1  jmcneill 	found_table = false;
    171       1.1  jmcneill 	AcpiUtAcquireMutex(ACPI_MTX_TABLES);
    172       1.1  jmcneill 	for (i = 0; i < AcpiGbl_RootTableList.CurrentTableCount; i++) {
    173       1.1  jmcneill 		tdesc = &AcpiGbl_RootTableList.Tables[i];
    174       1.1  jmcneill 		if (pa >= tdesc->Address &&
    175       1.1  jmcneill 		    pa < tdesc->Address + tdesc->Length) {
    176  1.2.16.1  perseant 
    177  1.2.16.1  perseant 			/*
    178  1.2.16.1  perseant 			 * allow access to all root table objects
    179  1.2.16.1  perseant 			 */
    180       1.1  jmcneill 			*paddr = tdesc->Address;
    181       1.1  jmcneill 			*plen = tdesc->Length;
    182       1.1  jmcneill 			found_table = true;
    183       1.1  jmcneill 			break;
    184  1.2.16.1  perseant 		} else if (memcmp(tdesc->Signature.Ascii, ACPI_SIG_TCPA, 4)
    185  1.2.16.1  perseant 		    == 0) {
    186  1.2.16.1  perseant 
    187  1.2.16.1  perseant 			/*
    188  1.2.16.1  perseant 			 * allow acces to TCPA (which requires mapping)
    189  1.2.16.1  perseant 			 */
    190  1.2.16.1  perseant 
    191  1.2.16.1  perseant 			/* duplicate TCPA table? buggy firmware? */
    192  1.2.16.1  perseant 			if (tcpa != NULL && tcpa_tdesc_len > 0)
    193  1.2.16.1  perseant 				AcpiOsUnmapMemory(tcpa, tcpa_tdesc_len);
    194  1.2.16.1  perseant 
    195  1.2.16.1  perseant 			tcpa = AcpiOsMapMemory(tdesc->Address, tdesc->Length);
    196  1.2.16.1  perseant 			if (tcpa != NULL)
    197  1.2.16.1  perseant 				tcpa_tdesc_len = tdesc->Length;
    198  1.2.16.1  perseant 		}
    199  1.2.16.1  perseant 	}
    200  1.2.16.1  perseant 
    201  1.2.16.1  perseant 	if (!found_table && tcpa != NULL) {
    202  1.2.16.1  perseant 		ACPI_PHYSICAL_ADDRESS tcpa_addr = 0;
    203  1.2.16.1  perseant 		uint32_t tcpa_len = 0;
    204  1.2.16.1  perseant 
    205  1.2.16.1  perseant 		if (tcpa->PlatformClass == ACPI_TCPA_CLIENT_TABLE) {
    206  1.2.16.1  perseant 			ACPI_TABLE_TCPA_CLIENT *t =
    207  1.2.16.1  perseant 			    (ACPI_TABLE_TCPA_CLIENT *)(tcpa + 1);
    208  1.2.16.1  perseant 			tcpa_addr = t->LogAddress;
    209  1.2.16.1  perseant 			tcpa_len = t->MinimumLogLength;
    210  1.2.16.1  perseant 		} else if (tcpa->PlatformClass == ACPI_TCPA_SERVER_TABLE) {
    211  1.2.16.1  perseant 			ACPI_TABLE_TCPA_SERVER *t =
    212  1.2.16.1  perseant 			    (ACPI_TABLE_TCPA_SERVER *)(tcpa + 1);
    213  1.2.16.1  perseant 			tcpa_addr = t->LogAddress;
    214  1.2.16.1  perseant 			tcpa_len = t->MinimumLogLength;
    215  1.2.16.1  perseant 		}
    216  1.2.16.1  perseant 		if (tcpa_len != 0 &&
    217  1.2.16.1  perseant 		    pa >= tcpa_addr &&
    218  1.2.16.1  perseant 		    pa < tcpa_addr + tcpa_len) {
    219  1.2.16.1  perseant 			*paddr = tcpa_addr;
    220  1.2.16.1  perseant 			*plen = tcpa_len;
    221  1.2.16.1  perseant 			found_table = true;
    222       1.1  jmcneill 		}
    223       1.1  jmcneill 	}
    224  1.2.16.1  perseant 
    225  1.2.16.1  perseant 	if (tcpa != NULL && tcpa_tdesc_len != 0)
    226  1.2.16.1  perseant 		AcpiOsUnmapMemory(tcpa, tcpa_tdesc_len);
    227  1.2.16.1  perseant 
    228       1.1  jmcneill 	AcpiUtReleaseMutex(ACPI_MTX_TABLES);
    229  1.2.16.1  perseant 
    230       1.1  jmcneill 	return found_table;
    231       1.1  jmcneill }
    232       1.1  jmcneill 
    233       1.1  jmcneill /*
    234       1.1  jmcneill  * acpi_read --
    235       1.1  jmcneill  *
    236       1.1  jmcneill  * 	Read data from an ACPI configuration table that resides in
    237       1.1  jmcneill  * 	physical memory. Only supports reading one table at a time.
    238       1.1  jmcneill  *
    239       1.1  jmcneill  */
    240       1.1  jmcneill static int
    241       1.1  jmcneill acpi_read(dev_t dev, struct uio *uio, int flag)
    242       1.1  jmcneill {
    243       1.1  jmcneill 	ACPI_PHYSICAL_ADDRESS pa, table_pa;
    244       1.1  jmcneill 	uint32_t table_len;
    245       1.1  jmcneill 	uint8_t *data;
    246       1.1  jmcneill 	int error;
    247       1.1  jmcneill 	size_t len;
    248       1.1  jmcneill 
    249       1.1  jmcneill 	if (uio->uio_rw != UIO_READ) {
    250       1.1  jmcneill 		return EPERM;
    251       1.1  jmcneill 	}
    252       1.1  jmcneill 
    253       1.1  jmcneill 	/* Make sure this is a read of a known table */
    254       1.1  jmcneill 	if (!acpi_find_table(uio->uio_offset, &table_pa, &table_len)) {
    255       1.1  jmcneill 		return EIO;
    256       1.1  jmcneill 	}
    257       1.1  jmcneill 
    258       1.1  jmcneill 	/* Copy the contents of the table to user-space */
    259       1.1  jmcneill 	pa = uio->uio_offset;
    260       1.2  jmcneill 	len = uimin(table_len - (pa - table_pa), uio->uio_resid);
    261       1.1  jmcneill 	data = AcpiOsMapMemory(pa, len);
    262       1.1  jmcneill 	if (data == NULL) {
    263       1.1  jmcneill 		return ENOMEM;
    264       1.1  jmcneill 	}
    265       1.1  jmcneill 	error = uiomove(data, len, uio);
    266       1.1  jmcneill 	AcpiOsUnmapMemory(data, len);
    267       1.1  jmcneill 
    268       1.1  jmcneill 	return error;
    269       1.1  jmcneill }
    270