Home | History | Annotate | Line # | Download | only in acpi
apei_mapreg.c revision 1.3
      1  1.3  riastrad /*	$NetBSD: apei_mapreg.c,v 1.3 2024/03/22 20:47:31 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*-
      4  1.1  riastrad  * Copyright (c) 2024 The NetBSD Foundation, Inc.
      5  1.1  riastrad  * All rights reserved.
      6  1.1  riastrad  *
      7  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
      8  1.1  riastrad  * modification, are permitted provided that the following conditions
      9  1.1  riastrad  * are met:
     10  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     11  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     12  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     15  1.1  riastrad  *
     16  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  riastrad  */
     28  1.1  riastrad 
     29  1.1  riastrad /*
     30  1.1  riastrad  * Pre-mapped ACPI register access
     31  1.1  riastrad  *
     32  1.1  riastrad  * XXX This isn't APEI-specific -- it should be moved into the general
     33  1.1  riastrad  * ACPI API, and unified with the AcpiRead/AcpiWrite implementation.
     34  1.1  riastrad  */
     35  1.1  riastrad 
     36  1.1  riastrad #include <sys/cdefs.h>
     37  1.3  riastrad __KERNEL_RCSID(0, "$NetBSD: apei_mapreg.c,v 1.3 2024/03/22 20:47:31 riastradh Exp $");
     38  1.1  riastrad 
     39  1.1  riastrad #include <sys/types.h>
     40  1.1  riastrad 
     41  1.1  riastrad #include <sys/atomic.h>
     42  1.1  riastrad 
     43  1.1  riastrad #include <dev/acpi/acpivar.h>
     44  1.1  riastrad #include <dev/acpi/apei_mapreg.h>
     45  1.1  riastrad 
     46  1.1  riastrad /*
     47  1.1  riastrad  * apei_mapreg_map(reg)
     48  1.1  riastrad  *
     49  1.1  riastrad  *	Return a mapping for use with apei_mapreg_read, or NULL if it
     50  1.1  riastrad  *	can't be mapped.
     51  1.1  riastrad  */
     52  1.1  riastrad struct apei_mapreg *
     53  1.1  riastrad apei_mapreg_map(const ACPI_GENERIC_ADDRESS *reg)
     54  1.1  riastrad {
     55  1.1  riastrad 
     56  1.1  riastrad 	/*
     57  1.1  riastrad 	 * Verify the result is reasonable.
     58  1.1  riastrad 	 */
     59  1.1  riastrad 	switch (reg->BitWidth) {
     60  1.1  riastrad 	case 8:
     61  1.1  riastrad 	case 16:
     62  1.1  riastrad 	case 32:
     63  1.1  riastrad 	case 64:
     64  1.1  riastrad 		break;
     65  1.1  riastrad 	default:
     66  1.1  riastrad 		return NULL;
     67  1.1  riastrad 	}
     68  1.1  riastrad 
     69  1.1  riastrad 	/*
     70  1.1  riastrad 	 * Verify we know how to do the access width.
     71  1.1  riastrad 	 */
     72  1.1  riastrad 	switch (reg->AccessWidth) {
     73  1.1  riastrad 	case 1:			/* 8-bit */
     74  1.1  riastrad 	case 2:			/* 16-bit */
     75  1.1  riastrad 	case 3:			/* 32-bit */
     76  1.1  riastrad 	case 4:			/* 64-bit */
     77  1.1  riastrad 		break;
     78  1.1  riastrad 	default:
     79  1.1  riastrad 		return NULL;
     80  1.1  riastrad 	}
     81  1.1  riastrad 
     82  1.1  riastrad 	/*
     83  1.1  riastrad 	 * Verify we don't need to shift anything, because I can't
     84  1.1  riastrad 	 * figure out how the shifting is supposed to work in five
     85  1.1  riastrad 	 * minutes of looking at the spec.
     86  1.1  riastrad 	 */
     87  1.1  riastrad 	switch (reg->BitOffset) {
     88  1.1  riastrad 	case 0:
     89  1.1  riastrad 		break;
     90  1.1  riastrad 	default:
     91  1.1  riastrad 		return NULL;
     92  1.1  riastrad 	}
     93  1.1  riastrad 
     94  1.1  riastrad 	/*
     95  1.1  riastrad 	 * Verify the bit width is a multiple of the access width so
     96  1.1  riastrad 	 * we're not accessing more than we need.
     97  1.1  riastrad 	 */
     98  1.1  riastrad 	if (reg->BitWidth % (8*(1 << (reg->AccessWidth - 1))))
     99  1.1  riastrad 		return NULL;
    100  1.1  riastrad 
    101  1.1  riastrad 	/*
    102  1.1  riastrad 	 * Dispatch on the space id.
    103  1.1  riastrad 	 *
    104  1.1  riastrad 	 * Currently this only handles memory space because I/O space
    105  1.1  riastrad 	 * is too painful to contemplate reimplementing here.
    106  1.1  riastrad 	 */
    107  1.1  riastrad 	switch (reg->SpaceId) {
    108  1.1  riastrad 	case ACPI_ADR_SPACE_SYSTEM_MEMORY:
    109  1.1  riastrad 		return AcpiOsMapMemory(reg->Address,
    110  1.1  riastrad 		    1 << (reg->AccessWidth - 1));
    111  1.1  riastrad 	default:
    112  1.1  riastrad 		return NULL;
    113  1.1  riastrad 	}
    114  1.1  riastrad }
    115  1.1  riastrad 
    116  1.1  riastrad /*
    117  1.1  riastrad  * apei_mapreg_unmap(reg, map)
    118  1.1  riastrad  *
    119  1.1  riastrad  *	Unmap a mapping previously returned by apei_mapreg_map.
    120  1.1  riastrad  */
    121  1.1  riastrad void
    122  1.1  riastrad apei_mapreg_unmap(const ACPI_GENERIC_ADDRESS *reg,
    123  1.1  riastrad     struct apei_mapreg *map)
    124  1.1  riastrad {
    125  1.1  riastrad 
    126  1.1  riastrad 	AcpiOsUnmapMemory(map, 1 << (reg->AccessWidth - 1));
    127  1.1  riastrad }
    128  1.1  riastrad 
    129  1.1  riastrad /*
    130  1.1  riastrad  * apei_mapreg_read(reg, map)
    131  1.1  riastrad  *
    132  1.1  riastrad  *	Read from reg via map previously obtained by apei_mapreg_map.
    133  1.1  riastrad  */
    134  1.1  riastrad uint64_t
    135  1.1  riastrad apei_mapreg_read(const ACPI_GENERIC_ADDRESS *reg,
    136  1.1  riastrad     const struct apei_mapreg *map)
    137  1.1  riastrad {
    138  1.1  riastrad 	unsigned chunkbits = NBBY*(1 << (reg->AccessWidth - 1));
    139  1.3  riastrad 	unsigned i, n = reg->BitWidth / chunkbits;
    140  1.1  riastrad 	uint64_t v = 0;
    141  1.1  riastrad 
    142  1.1  riastrad 	for (i = 0; i < n; i++) {
    143  1.1  riastrad 		uint64_t chunk;
    144  1.1  riastrad 
    145  1.1  riastrad 		switch (reg->AccessWidth) {
    146  1.1  riastrad 		case 1:
    147  1.2  riastrad 			chunk = *((volatile const uint8_t *)map + i);
    148  1.1  riastrad 			break;
    149  1.1  riastrad 		case 2:
    150  1.2  riastrad 			chunk = *((volatile const uint16_t *)map + i);
    151  1.1  riastrad 			break;
    152  1.1  riastrad 		case 3:
    153  1.2  riastrad 			chunk = *((volatile const uint32_t *)map + i);
    154  1.1  riastrad 			break;
    155  1.1  riastrad 		case 4:
    156  1.2  riastrad 			chunk = *((volatile const uint64_t *)map + i);
    157  1.1  riastrad 			break;
    158  1.1  riastrad 		default:
    159  1.1  riastrad 			__unreachable();
    160  1.1  riastrad 		}
    161  1.1  riastrad 		v |= chunk << (i*chunkbits);
    162  1.1  riastrad 	}
    163  1.1  riastrad 
    164  1.1  riastrad 	membar_acquire();	/* XXX probably not right for MMIO */
    165  1.1  riastrad 	return v;
    166  1.1  riastrad }
    167  1.1  riastrad 
    168  1.1  riastrad /*
    169  1.1  riastrad  * apei_mapreg_write(reg, map, v)
    170  1.1  riastrad  *
    171  1.1  riastrad  *	Write to reg via map previously obtained by apei_mapreg_map.
    172  1.1  riastrad  */
    173  1.1  riastrad void
    174  1.1  riastrad apei_mapreg_write(const ACPI_GENERIC_ADDRESS *reg, struct apei_mapreg *map,
    175  1.1  riastrad     uint64_t v)
    176  1.1  riastrad {
    177  1.1  riastrad 	unsigned chunkbits = NBBY*(1 << (reg->AccessWidth - 1));
    178  1.3  riastrad 	unsigned i, n = reg->BitWidth / chunkbits;
    179  1.1  riastrad 
    180  1.1  riastrad 	membar_release();	/* XXX probably not right for MMIO */
    181  1.1  riastrad 	for (i = 0; i < n; i++) {
    182  1.1  riastrad 		uint64_t chunk = v >> (i*chunkbits);
    183  1.1  riastrad 
    184  1.1  riastrad 		switch (reg->AccessWidth) {
    185  1.1  riastrad 		case 1:
    186  1.2  riastrad 			*((volatile uint8_t *)map + i) = chunk;
    187  1.1  riastrad 			break;
    188  1.1  riastrad 		case 2:
    189  1.2  riastrad 			*((volatile uint16_t *)map + i) = chunk;
    190  1.1  riastrad 			break;
    191  1.1  riastrad 		case 3:
    192  1.2  riastrad 			*((volatile uint32_t *)map + i) = chunk;
    193  1.1  riastrad 			break;
    194  1.1  riastrad 		case 4:
    195  1.2  riastrad 			*((volatile uint64_t *)map + i) = chunk;
    196  1.1  riastrad 			break;
    197  1.1  riastrad 		default:
    198  1.1  riastrad 			__unreachable();
    199  1.1  riastrad 		}
    200  1.1  riastrad 	}
    201  1.1  riastrad }
    202