Home | History | Annotate | Line # | Download | only in acpi
acpi_platform.c revision 1.16
      1 /* $NetBSD: acpi_platform.c,v 1.16 2019/08/02 19:49:17 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jared McNeill <jmcneill (at) invisible.ca>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include "com.h"
     33 #include "plcom.h"
     34 #include "wsdisplay.h"
     35 #include "genfb.h"
     36 #include "opt_efi.h"
     37 #include "opt_multiprocessor.h"
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: acpi_platform.c,v 1.16 2019/08/02 19:49:17 jmcneill Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/bus.h>
     44 #include <sys/cpu.h>
     45 #include <sys/device.h>
     46 #include <sys/termios.h>
     47 
     48 #include <dev/fdt/fdtvar.h>
     49 #include <arm/fdt/arm_fdtvar.h>
     50 
     51 #include <uvm/uvm_extern.h>
     52 
     53 #include <machine/bootconfig.h>
     54 #include <arm/cpufunc.h>
     55 #include <arm/locore.h>
     56 
     57 #include <arm/cortex/gtmr_var.h>
     58 
     59 #include <arm/arm/psci.h>
     60 #include <arm/fdt/psci_fdtvar.h>
     61 
     62 #include <evbarm/fdt/platform.h>
     63 
     64 #include <evbarm/dev/plcomreg.h>
     65 #include <evbarm/dev/plcomvar.h>
     66 #include <dev/ic/ns16550reg.h>
     67 #include <dev/ic/comreg.h>
     68 #include <dev/ic/comvar.h>
     69 
     70 #if NCOM > 0
     71 #include <dev/pci/pcireg.h>
     72 #include <dev/pci/pcivar.h>
     73 #include <dev/pci/pucvar.h>
     74 #endif
     75 
     76 #if NWSDISPLAY > 0
     77 #include <dev/wscons/wsconsio.h>
     78 #include <dev/wscons/wsdisplayvar.h>
     79 #include <dev/rasops/rasops.h>
     80 #include <dev/wsfont/wsfont.h>
     81 #include <dev/wscons/wsdisplay_vconsvar.h>
     82 #endif
     83 
     84 #ifdef EFI_RUNTIME
     85 #include <arm/arm/efi_runtime.h>
     86 #endif
     87 
     88 #include <dev/acpi/acpireg.h>
     89 #include <dev/acpi/acpivar.h>
     90 #include <arm/acpi/acpi_table.h>
     91 
     92 #include <libfdt.h>
     93 
     94 #define	SPCR_BAUD_UNKNOWN			0
     95 #define	SPCR_BAUD_9600				3
     96 #define	SPCR_BAUD_19200				4
     97 #define	SPCR_BAUD_57600				6
     98 #define	SPCR_BAUD_115200			7
     99 
    100 extern struct bus_space arm_generic_bs_tag;
    101 extern struct bus_space arm_generic_a4x_bs_tag;
    102 
    103 #if NPLCOM > 0
    104 static struct plcom_instance plcom_console;
    105 #endif
    106 
    107 struct arm32_bus_dma_tag acpi_coherent_dma_tag;
    108 static struct arm32_dma_range acpi_coherent_ranges[] = {
    109 	[0] = {
    110 		.dr_sysbase = 0,
    111 		.dr_busbase = 0,
    112 		.dr_len = UINTPTR_MAX,
    113 	 	.dr_flags = _BUS_DMAMAP_COHERENT,
    114 	}
    115 };
    116 
    117 static const struct pmap_devmap *
    118 acpi_platform_devmap(void)
    119 {
    120 	static const struct pmap_devmap devmap[] = {
    121 		DEVMAP_ENTRY_END
    122 	};
    123 
    124 	return devmap;
    125 }
    126 
    127 static void
    128 acpi_platform_bootstrap(void)
    129 {
    130 	extern struct arm32_bus_dma_tag arm_generic_dma_tag;
    131 
    132 	acpi_coherent_dma_tag = arm_generic_dma_tag;
    133 	acpi_coherent_dma_tag._ranges = acpi_coherent_ranges;
    134 	acpi_coherent_dma_tag._nranges = __arraycount(acpi_coherent_ranges);
    135 }
    136 
    137 #if NWSDISPLAY > 0 && NGENFB > 0
    138 static struct wsscreen_descr acpi_platform_stdscreen = {
    139 	.name = "std",
    140 	.ncols = 0,
    141 	.nrows = 0,
    142 	.textops = NULL,
    143 	.fontwidth = 0,
    144 	.fontheight = 0,
    145 	.capabilities = 0,
    146 	.modecookie = NULL
    147 };
    148 
    149 static struct vcons_screen acpi_platform_screen;
    150 
    151 static int
    152 acpi_platform_find_simplefb(void)
    153 {
    154 	static const char * simplefb_compatible[] = { "simple-framebuffer", NULL };
    155 	int chosen_phandle, child;
    156 
    157 	chosen_phandle = OF_finddevice("/chosen");
    158 	if (chosen_phandle == -1)
    159 		return -1;
    160 
    161 	for (child = OF_child(chosen_phandle); child; child = OF_peer(child)) {
    162 		if (!fdtbus_status_okay(child))
    163 			continue;
    164 		if (!of_match_compatible(child, simplefb_compatible))
    165 			continue;
    166 
    167 		return child;
    168 	}
    169 
    170 	return -1;
    171 }
    172 
    173 static void
    174 acpi_platform_wsdisplay_preattach(void)
    175 {
    176 	struct rasops_info *ri = &acpi_platform_screen.scr_ri;
    177 	bus_space_tag_t bst = &arm_generic_bs_tag;
    178 	bus_space_handle_t bsh;
    179 	uint32_t width, height, stride;
    180 	const char *format;
    181 	bus_addr_t addr;
    182 	bus_size_t size;
    183 	uint16_t depth;
    184 	long defattr;
    185 
    186 	memset(&acpi_platform_screen, 0, sizeof(acpi_platform_screen));
    187 
    188 	const int phandle = acpi_platform_find_simplefb();
    189 	if (phandle == -1)
    190 		return;
    191 
    192 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0 || size == 0)
    193 		return;
    194 
    195 	if (of_getprop_uint32(phandle, "width", &width) != 0 ||
    196 	    of_getprop_uint32(phandle, "height", &height) != 0 ||
    197 	    of_getprop_uint32(phandle, "stride", &stride) != 0 ||
    198 	    (format = fdtbus_get_string(phandle, "format")) == NULL)
    199 		return;
    200 
    201 	if (strcmp(format, "a8b8g8r8") == 0 ||
    202 	    strcmp(format, "x8r8g8b8") == 0) {
    203 		depth = 32;
    204 	} else if (strcmp(format, "r5g6b5") == 0) {
    205 		depth = 16;
    206 	} else {
    207 		return;
    208 	}
    209 
    210 	if (bus_space_map(bst, addr, size,
    211 	    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE, &bsh) != 0)
    212 		return;
    213 
    214 	wsfont_init();
    215 
    216 	ri->ri_width = width;
    217 	ri->ri_height = height;
    218 	ri->ri_depth = depth;
    219 	ri->ri_stride = stride;
    220 	ri->ri_bits = bus_space_vaddr(bst, bsh);
    221 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR | RI_CLEAR;
    222 	rasops_init(ri, ri->ri_height / 8, ri->ri_width / 8);
    223 	ri->ri_caps = WSSCREEN_WSCOLORS;
    224 	rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
    225 	    ri->ri_width / ri->ri_font->fontwidth);
    226 
    227 	acpi_platform_stdscreen.nrows = ri->ri_rows;
    228 	acpi_platform_stdscreen.ncols = ri->ri_cols;
    229 	acpi_platform_stdscreen.textops = &ri->ri_ops;
    230 	acpi_platform_stdscreen.capabilities = ri->ri_caps;
    231 
    232 	ri->ri_ops.allocattr(ri, 0, 0, 0, &defattr);
    233 
    234 	wsdisplay_preattach(&acpi_platform_stdscreen, ri, 0, 0, defattr);
    235 }
    236 #endif
    237 
    238 static void
    239 acpi_platform_startup(void)
    240 {
    241 	ACPI_TABLE_SPCR *spcr;
    242 	ACPI_TABLE_FADT *fadt;
    243 #ifdef MULTIPROCESSOR
    244 	ACPI_TABLE_MADT *madt;
    245 #endif
    246 	int baud_rate;
    247 
    248 	/*
    249 	 * Setup serial console device
    250 	 */
    251 	if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_SPCR, (void **)&spcr))) {
    252 
    253 		switch (spcr->BaudRate) {
    254 		case SPCR_BAUD_9600:
    255 			baud_rate = 9600;
    256 			break;
    257 		case SPCR_BAUD_19200:
    258 			baud_rate = 19200;
    259 			break;
    260 		case SPCR_BAUD_57600:
    261 			baud_rate = 57600;
    262 			break;
    263 		case SPCR_BAUD_115200:
    264 		case SPCR_BAUD_UNKNOWN:
    265 		default:
    266 			baud_rate = 115200;
    267 			break;
    268 		}
    269 
    270 		if (spcr->SerialPort.SpaceId == ACPI_ADR_SPACE_SYSTEM_MEMORY &&
    271 		    spcr->SerialPort.Address != 0) {
    272 			switch (spcr->InterfaceType) {
    273 #if NPLCOM > 0
    274 			case ACPI_DBG2_ARM_PL011:
    275 			case ACPI_DBG2_ARM_SBSA_32BIT:
    276 			case ACPI_DBG2_ARM_SBSA_GENERIC:
    277 				plcom_console.pi_type = PLCOM_TYPE_PL011;
    278 				plcom_console.pi_iot = &arm_generic_bs_tag;
    279 				plcom_console.pi_iobase = spcr->SerialPort.Address;
    280 				plcom_console.pi_size = PL011COM_UART_SIZE;
    281 				plcom_console.pi_flags = PLC_FLAG_32BIT_ACCESS;
    282 
    283 				plcomcnattach(&plcom_console, baud_rate, 0, TTYDEF_CFLAG, -1);
    284 				break;
    285 #endif
    286 #if NCOM > 0
    287 			case ACPI_DBG2_16550_COMPATIBLE:
    288 			case ACPI_DBG2_16550_SUBSET:
    289 				if (ACPI_ACCESS_BIT_WIDTH(spcr->SerialPort.AccessWidth) == 8) {
    290 					comcnattach(&arm_generic_bs_tag, spcr->SerialPort.Address, baud_rate, -1,
    291 					    COM_TYPE_NORMAL, TTYDEF_CFLAG);
    292 				} else {
    293 					comcnattach(&arm_generic_a4x_bs_tag, spcr->SerialPort.Address, baud_rate, -1,
    294 					    COM_TYPE_NORMAL, TTYDEF_CFLAG);
    295 				}
    296 				break;
    297 			case ACPI_DBG2_BCM2835:
    298 				comcnattach(&arm_generic_a4x_bs_tag, spcr->SerialPort.Address + 0x40, baud_rate, -1,
    299 				    COM_TYPE_BCMAUXUART, TTYDEF_CFLAG);
    300 				cn_set_magic("+++++");
    301 				break;
    302 #endif
    303 			default:
    304 				printf("SPCR: kernel does not support interface type %#x\n", spcr->InterfaceType);
    305 				break;
    306 			}
    307 		}
    308 		acpi_table_unmap((ACPI_TABLE_HEADER *)spcr);
    309 	}
    310 
    311 	/*
    312 	 * Setup framebuffer console, if present.
    313 	 */
    314 #if NWSDISPLAY > 0 && NGENFB > 0
    315 	acpi_platform_wsdisplay_preattach();
    316 #endif
    317 
    318 	/*
    319 	 * Initialize PSCI 0.2+ if implemented
    320 	 */
    321 	if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_FADT, (void **)&fadt))) {
    322 		if (fadt->ArmBootFlags & ACPI_FADT_PSCI_COMPLIANT) {
    323 			if (fadt->ArmBootFlags & ACPI_FADT_PSCI_USE_HVC) {
    324 				psci_init(psci_call_hvc);
    325 			} else {
    326 				psci_init(psci_call_smc);
    327 			}
    328 		}
    329 		acpi_table_unmap((ACPI_TABLE_HEADER *)fadt);
    330 	}
    331 
    332 #ifdef MULTIPROCESSOR
    333 	/*
    334 	 * Count CPUs
    335 	 */
    336 	if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_MADT, (void **)&madt))) {
    337 		char *end = (char *)madt + madt->Header.Length;
    338 		char *where = (char *)madt + sizeof(ACPI_TABLE_MADT);
    339 		while (where < end) {
    340 			ACPI_SUBTABLE_HEADER *subtable = (ACPI_SUBTABLE_HEADER *)where;
    341 			if (subtable->Type == ACPI_MADT_TYPE_GENERIC_INTERRUPT)
    342 				arm_cpu_max++;
    343 			where += subtable->Length;
    344 		}
    345 		acpi_table_unmap((ACPI_TABLE_HEADER *)madt);
    346 	}
    347 #endif /* MULTIPROCESSOR */
    348 }
    349 
    350 static void
    351 acpi_platform_init_attach_args(struct fdt_attach_args *faa)
    352 {
    353 	extern struct bus_space arm_generic_bs_tag;
    354 	extern struct bus_space arm_generic_a4x_bs_tag;
    355 
    356 	faa->faa_bst = &arm_generic_bs_tag;
    357 	faa->faa_a4x_bst = &arm_generic_a4x_bs_tag;
    358 	faa->faa_dmat = &acpi_coherent_dma_tag;
    359 }
    360 
    361 static void
    362 acpi_platform_device_register(device_t self, void *aux)
    363 {
    364 #if NCOM > 0
    365 	prop_dictionary_t prop = device_properties(self);
    366 
    367 	if (device_is_a(self, "com")) {
    368 		ACPI_TABLE_SPCR *spcr;
    369 
    370 		if (ACPI_FAILURE(acpi_table_find(ACPI_SIG_SPCR, (void **)&spcr)))
    371 			return;
    372 
    373 		if (spcr->SerialPort.SpaceId != ACPI_ADR_SPACE_SYSTEM_MEMORY)
    374 			goto spcr_unmap;
    375 		if (spcr->SerialPort.Address == 0)
    376 			goto spcr_unmap;
    377 		if (spcr->InterfaceType != ACPI_DBG2_16550_COMPATIBLE &&
    378 		    spcr->InterfaceType != ACPI_DBG2_16550_SUBSET)
    379 			goto spcr_unmap;
    380 
    381 		if (device_is_a(device_parent(self), "puc")) {
    382 			const struct puc_attach_args * const paa = aux;
    383 			int b, d, f;
    384 
    385 			const int s = pci_get_segment(paa->pc);
    386 			pci_decompose_tag(paa->pc, paa->tag, &b, &d, &f);
    387 
    388 			if (spcr->PciSegment == s && spcr->PciBus == b &&
    389 			    spcr->PciDevice == d && spcr->PciFunction == f)
    390 				prop_dictionary_set_bool(prop, "force_console", true);
    391 		}
    392 
    393 		if (device_is_a(device_parent(self), "acpi")) {
    394 			struct acpi_attach_args * const aa = aux;
    395 			struct acpi_resources res;
    396 			struct acpi_mem *mem;
    397 
    398 			if (ACPI_FAILURE(acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
    399 			    &res, &acpi_resource_parse_ops_quiet)))
    400 				goto spcr_unmap;
    401 
    402 			mem = acpi_res_mem(&res, 0);
    403 			if (mem == NULL)
    404 				goto crs_cleanup;
    405 
    406 			if (mem->ar_base == spcr->SerialPort.Address)
    407 				prop_dictionary_set_bool(prop, "force_console", true);
    408 
    409 crs_cleanup:
    410 			acpi_resource_cleanup(&res);
    411 		}
    412 
    413 spcr_unmap:
    414 		acpi_table_unmap((ACPI_TABLE_HEADER *)spcr);
    415 	}
    416 #endif
    417 }
    418 
    419 static void
    420 acpi_platform_reset(void)
    421 {
    422 #ifdef EFI_RUNTIME
    423 	if (arm_efirt_reset(EFI_RESET_COLD) == 0)
    424 		return;
    425 #endif
    426 	if (psci_available())
    427 		psci_system_reset();
    428 }
    429 
    430 static u_int
    431 acpi_platform_uart_freq(void)
    432 {
    433 	return 0;
    434 }
    435 
    436 static const struct arm_platform acpi_platform = {
    437 	.ap_devmap = acpi_platform_devmap,
    438 	.ap_bootstrap = acpi_platform_bootstrap,
    439 	.ap_startup = acpi_platform_startup,
    440 	.ap_init_attach_args = acpi_platform_init_attach_args,
    441 	.ap_device_register = acpi_platform_device_register,
    442 	.ap_reset = acpi_platform_reset,
    443 	.ap_delay = gtmr_delay,
    444 	.ap_uart_freq = acpi_platform_uart_freq,
    445 };
    446 
    447 ARM_PLATFORM(virt, "netbsd,generic-acpi", &acpi_platform);
    448