1*9e96d213Sriastradh /* $NetBSD: acpi_dev.c,v 1.4 2024/11/12 21:49:11 riastradh Exp $ */ 2161b30afSjmcneill 3161b30afSjmcneill /*- 4161b30afSjmcneill * Copyright (c) 2020 Jared McNeill <jmcneill@invisible.ca> 5161b30afSjmcneill * All rights reserved. 6161b30afSjmcneill * 7161b30afSjmcneill * Redistribution and use in source and binary forms, with or without 8161b30afSjmcneill * modification, are permitted provided that the following conditions 9161b30afSjmcneill * are met: 10161b30afSjmcneill * 1. Redistributions of source code must retain the above copyright 11161b30afSjmcneill * notice, this list of conditions and the following disclaimer. 12161b30afSjmcneill * 2. Redistributions in binary form must reproduce the above copyright 13161b30afSjmcneill * notice, this list of conditions and the following disclaimer in the 14161b30afSjmcneill * documentation and/or other materials provided with the distribution. 15161b30afSjmcneill * 16161b30afSjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17161b30afSjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18161b30afSjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19161b30afSjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20161b30afSjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21161b30afSjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22161b30afSjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23161b30afSjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24161b30afSjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25161b30afSjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26161b30afSjmcneill * SUCH DAMAGE. 27161b30afSjmcneill */ 28161b30afSjmcneill 29161b30afSjmcneill #include <sys/cdefs.h> 30*9e96d213Sriastradh __KERNEL_RCSID(0, "$NetBSD: acpi_dev.c,v 1.4 2024/11/12 21:49:11 riastradh Exp $"); 31161b30afSjmcneill 32161b30afSjmcneill #include <sys/param.h> 33161b30afSjmcneill #include <sys/conf.h> 34161b30afSjmcneill #include <sys/mman.h> 35161b30afSjmcneill 36161b30afSjmcneill #include <dev/acpi/acpireg.h> 37161b30afSjmcneill #include <dev/acpi/acpivar.h> 38161b30afSjmcneill 39161b30afSjmcneill #define _COMPONENT ACPI_BUS_COMPONENT 40161b30afSjmcneill ACPI_MODULE_NAME ("acpi_dev") 41161b30afSjmcneill 42161b30afSjmcneill static dev_type_read(acpi_read); 43161b30afSjmcneill 44161b30afSjmcneill const struct cdevsw acpi_cdevsw = { 45161b30afSjmcneill .d_open = nullopen, 46161b30afSjmcneill .d_close = nullclose, 47161b30afSjmcneill .d_read = acpi_read, 48161b30afSjmcneill .d_write = nowrite, 49161b30afSjmcneill .d_ioctl = noioctl, 50161b30afSjmcneill .d_stop = nostop, 51161b30afSjmcneill .d_tty = notty, 52161b30afSjmcneill .d_poll = nopoll, 53161b30afSjmcneill .d_mmap = nommap, 54161b30afSjmcneill .d_kqfilter = nokqfilter, 55161b30afSjmcneill .d_discard = nodiscard, 56161b30afSjmcneill .d_flag = D_OTHER | D_MPSAFE, 57161b30afSjmcneill }; 58161b30afSjmcneill 59161b30afSjmcneill /* 60161b30afSjmcneill * acpi_find_table_rsdp -- 61161b30afSjmcneill * 62161b30afSjmcneill * Returns true if the RSDP table is found and overlaps the specified 63161b30afSjmcneill * physical address. The table's physical start address and length 64161b30afSjmcneill * are placed in 'paddr' and 'plen' when found. 65161b30afSjmcneill * 66161b30afSjmcneill */ 67161b30afSjmcneill static bool 68161b30afSjmcneill acpi_find_table_rsdp(ACPI_PHYSICAL_ADDRESS pa, 69161b30afSjmcneill ACPI_PHYSICAL_ADDRESS *paddr, uint32_t *plen) 70161b30afSjmcneill { 71161b30afSjmcneill ACPI_PHYSICAL_ADDRESS table_pa; 72161b30afSjmcneill 73161b30afSjmcneill table_pa = AcpiOsGetRootPointer(); 74161b30afSjmcneill if (table_pa == 0) { 75161b30afSjmcneill return false; 76161b30afSjmcneill } 77161b30afSjmcneill if (pa >= table_pa && pa < table_pa + sizeof(ACPI_TABLE_RSDP)) { 78161b30afSjmcneill *paddr = table_pa; 79161b30afSjmcneill *plen = sizeof(ACPI_TABLE_RSDP); 80161b30afSjmcneill return true; 81161b30afSjmcneill } 82161b30afSjmcneill 83161b30afSjmcneill return false; 84161b30afSjmcneill } 85161b30afSjmcneill 86161b30afSjmcneill /* 87161b30afSjmcneill * acpi_find_table_sdt -- 88161b30afSjmcneill * 89161b30afSjmcneill * Returns true if the XSDT/RSDT table is found and overlaps the 90161b30afSjmcneill * specified physical address. The table's physical start address 91161b30afSjmcneill * and length are placed in 'paddr' and 'plen' when found. 92161b30afSjmcneill * 93161b30afSjmcneill */ 94161b30afSjmcneill static bool 95161b30afSjmcneill acpi_find_table_sdt(ACPI_PHYSICAL_ADDRESS pa, 96161b30afSjmcneill ACPI_PHYSICAL_ADDRESS *paddr, uint32_t *plen) 97161b30afSjmcneill { 98161b30afSjmcneill ACPI_PHYSICAL_ADDRESS table_pa; 99161b30afSjmcneill ACPI_TABLE_RSDP *rsdp; 100161b30afSjmcneill ACPI_TABLE_HEADER *thdr; 101161b30afSjmcneill uint32_t table_len; 102161b30afSjmcneill 103161b30afSjmcneill table_pa = AcpiOsGetRootPointer(); 104161b30afSjmcneill KASSERT(table_pa != 0); 105161b30afSjmcneill 106161b30afSjmcneill /* 107161b30afSjmcneill * Find the XSDT/RSDT using the RSDP. 108161b30afSjmcneill */ 109161b30afSjmcneill rsdp = AcpiOsMapMemory(table_pa, sizeof(ACPI_TABLE_RSDP)); 110161b30afSjmcneill if (rsdp == NULL) { 111161b30afSjmcneill return false; 112161b30afSjmcneill } 113161b30afSjmcneill if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress) { 114161b30afSjmcneill table_pa = rsdp->XsdtPhysicalAddress; 115161b30afSjmcneill } else { 116161b30afSjmcneill table_pa = rsdp->RsdtPhysicalAddress; 117161b30afSjmcneill } 118161b30afSjmcneill AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP)); 119161b30afSjmcneill if (table_pa == 0) { 120161b30afSjmcneill return false; 121161b30afSjmcneill } 122161b30afSjmcneill 123161b30afSjmcneill /* 124161b30afSjmcneill * Map the XSDT/RSDT and check access. 125161b30afSjmcneill */ 126161b30afSjmcneill thdr = AcpiOsMapMemory(table_pa, sizeof(ACPI_TABLE_HEADER)); 127161b30afSjmcneill if (thdr == NULL) { 128161b30afSjmcneill return false; 129161b30afSjmcneill } 130161b30afSjmcneill table_len = thdr->Length; 131161b30afSjmcneill AcpiOsUnmapMemory(thdr, sizeof(ACPI_TABLE_HEADER)); 132161b30afSjmcneill if (pa >= table_pa && pa < table_pa + table_len) { 133161b30afSjmcneill *paddr = table_pa; 134161b30afSjmcneill *plen = table_len; 135161b30afSjmcneill return true; 136161b30afSjmcneill } 137161b30afSjmcneill 138161b30afSjmcneill return false; 139161b30afSjmcneill } 140161b30afSjmcneill 141161b30afSjmcneill /* 142161b30afSjmcneill * acpi_find_table -- 143161b30afSjmcneill * 144161b30afSjmcneill * Find an ACPI table that overlaps the specified physical address. 145161b30afSjmcneill * Returns true if the table is found and places the table start 146161b30afSjmcneill * address into 'paddr' and the length into 'plen'. 147161b30afSjmcneill * 148161b30afSjmcneill */ 149161b30afSjmcneill static bool 150161b30afSjmcneill acpi_find_table(ACPI_PHYSICAL_ADDRESS pa, 151161b30afSjmcneill ACPI_PHYSICAL_ADDRESS *paddr, uint32_t *plen) 152161b30afSjmcneill { 153161b30afSjmcneill ACPI_TABLE_DESC *tdesc; 15489a7c3fcSmartin ACPI_TABLE_TCPA_HDR *tcpa = NULL; 15589a7c3fcSmartin size_t tcpa_tdesc_len = 0; 156161b30afSjmcneill bool found_table; 157161b30afSjmcneill uint32_t i; 158161b30afSjmcneill 159161b30afSjmcneill /* Check for RSDP access. */ 160161b30afSjmcneill if (acpi_find_table_rsdp(pa, paddr, plen)) { 161161b30afSjmcneill return true; 162161b30afSjmcneill } 163161b30afSjmcneill 164161b30afSjmcneill /* Check for XSDT/RSDT access. */ 165161b30afSjmcneill if (acpi_find_table_sdt(pa, paddr, plen)) { 166161b30afSjmcneill return true; 167161b30afSjmcneill } 168161b30afSjmcneill 169161b30afSjmcneill /* Check for root table access. */ 170161b30afSjmcneill found_table = false; 171161b30afSjmcneill AcpiUtAcquireMutex(ACPI_MTX_TABLES); 172161b30afSjmcneill for (i = 0; i < AcpiGbl_RootTableList.CurrentTableCount; i++) { 173161b30afSjmcneill tdesc = &AcpiGbl_RootTableList.Tables[i]; 174161b30afSjmcneill if (pa >= tdesc->Address && 175161b30afSjmcneill pa < tdesc->Address + tdesc->Length) { 17689a7c3fcSmartin 17789a7c3fcSmartin /* 17889a7c3fcSmartin * allow access to all root table objects 17989a7c3fcSmartin */ 180161b30afSjmcneill *paddr = tdesc->Address; 181161b30afSjmcneill *plen = tdesc->Length; 182161b30afSjmcneill found_table = true; 183161b30afSjmcneill break; 18489a7c3fcSmartin } else if (memcmp(tdesc->Signature.Ascii, ACPI_SIG_TCPA, 4) 18589a7c3fcSmartin == 0) { 18689a7c3fcSmartin 18789a7c3fcSmartin /* 18889a7c3fcSmartin * allow acces to TCPA (which requires mapping) 18989a7c3fcSmartin */ 19089a7c3fcSmartin 19189a7c3fcSmartin /* duplicate TCPA table? buggy firmware? */ 19289a7c3fcSmartin if (tcpa != NULL && tcpa_tdesc_len > 0) 19389a7c3fcSmartin AcpiOsUnmapMemory(tcpa, tcpa_tdesc_len); 19489a7c3fcSmartin 195*9e96d213Sriastradh tcpa = AcpiOsMapMemory(tdesc->Address, tdesc->Length); 19689a7c3fcSmartin if (tcpa != NULL) 19789a7c3fcSmartin tcpa_tdesc_len = tdesc->Length; 198161b30afSjmcneill } 199161b30afSjmcneill } 20089a7c3fcSmartin 20189a7c3fcSmartin if (!found_table && tcpa != NULL) { 20289a7c3fcSmartin ACPI_PHYSICAL_ADDRESS tcpa_addr = 0; 20389a7c3fcSmartin uint32_t tcpa_len = 0; 20489a7c3fcSmartin 20589a7c3fcSmartin if (tcpa->PlatformClass == ACPI_TCPA_CLIENT_TABLE) { 20689a7c3fcSmartin ACPI_TABLE_TCPA_CLIENT *t = 20789a7c3fcSmartin (ACPI_TABLE_TCPA_CLIENT *)(tcpa + 1); 20889a7c3fcSmartin tcpa_addr = t->LogAddress; 20989a7c3fcSmartin tcpa_len = t->MinimumLogLength; 21089a7c3fcSmartin } else if (tcpa->PlatformClass == ACPI_TCPA_SERVER_TABLE) { 21189a7c3fcSmartin ACPI_TABLE_TCPA_SERVER *t = 21289a7c3fcSmartin (ACPI_TABLE_TCPA_SERVER *)(tcpa + 1); 21389a7c3fcSmartin tcpa_addr = t->LogAddress; 21489a7c3fcSmartin tcpa_len = t->MinimumLogLength; 21589a7c3fcSmartin } 21689a7c3fcSmartin if (tcpa_len != 0 && 21789a7c3fcSmartin pa >= tcpa_addr && 21889a7c3fcSmartin pa < tcpa_addr + tcpa_len) { 21989a7c3fcSmartin *paddr = tcpa_addr; 22089a7c3fcSmartin *plen = tcpa_len; 22189a7c3fcSmartin found_table = true; 22289a7c3fcSmartin } 22389a7c3fcSmartin } 22489a7c3fcSmartin 22589a7c3fcSmartin if (tcpa != NULL && tcpa_tdesc_len != 0) 22689a7c3fcSmartin AcpiOsUnmapMemory(tcpa, tcpa_tdesc_len); 22789a7c3fcSmartin 228161b30afSjmcneill AcpiUtReleaseMutex(ACPI_MTX_TABLES); 22989a7c3fcSmartin 230161b30afSjmcneill return found_table; 231161b30afSjmcneill } 232161b30afSjmcneill 233161b30afSjmcneill /* 234161b30afSjmcneill * acpi_read -- 235161b30afSjmcneill * 236161b30afSjmcneill * Read data from an ACPI configuration table that resides in 237161b30afSjmcneill * physical memory. Only supports reading one table at a time. 238161b30afSjmcneill * 239161b30afSjmcneill */ 240161b30afSjmcneill static int 241161b30afSjmcneill acpi_read(dev_t dev, struct uio *uio, int flag) 242161b30afSjmcneill { 243161b30afSjmcneill ACPI_PHYSICAL_ADDRESS pa, table_pa; 244161b30afSjmcneill uint32_t table_len; 245161b30afSjmcneill uint8_t *data; 246161b30afSjmcneill int error; 247161b30afSjmcneill size_t len; 248161b30afSjmcneill 249161b30afSjmcneill if (uio->uio_rw != UIO_READ) { 250161b30afSjmcneill return EPERM; 251161b30afSjmcneill } 252161b30afSjmcneill 253161b30afSjmcneill /* Make sure this is a read of a known table */ 254161b30afSjmcneill if (!acpi_find_table(uio->uio_offset, &table_pa, &table_len)) { 255161b30afSjmcneill return EIO; 256161b30afSjmcneill } 257161b30afSjmcneill 258161b30afSjmcneill /* Copy the contents of the table to user-space */ 259161b30afSjmcneill pa = uio->uio_offset; 2603e525fe6Sjmcneill len = uimin(table_len - (pa - table_pa), uio->uio_resid); 261161b30afSjmcneill data = AcpiOsMapMemory(pa, len); 262161b30afSjmcneill if (data == NULL) { 263161b30afSjmcneill return ENOMEM; 264161b30afSjmcneill } 265161b30afSjmcneill error = uiomove(data, len, uio); 266161b30afSjmcneill AcpiOsUnmapMemory(data, len); 267161b30afSjmcneill 268161b30afSjmcneill return error; 269161b30afSjmcneill } 270