1 1.1 jmcneill /* $NetBSD: acpi_pcc.c,v 1.1 2020/12/13 20:27:53 jmcneill 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE. 27 1.1 jmcneill */ 28 1.1 jmcneill 29 1.1 jmcneill /* 30 1.1 jmcneill * Platform Communications Channel (PCC) device driver. 31 1.1 jmcneill */ 32 1.1 jmcneill 33 1.1 jmcneill #include <sys/cdefs.h> 34 1.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: acpi_pcc.c,v 1.1 2020/12/13 20:27:53 jmcneill Exp $"); 35 1.1 jmcneill 36 1.1 jmcneill #include <sys/param.h> 37 1.1 jmcneill #include <sys/bus.h> 38 1.1 jmcneill #include <sys/cpu.h> 39 1.1 jmcneill #include <sys/device.h> 40 1.1 jmcneill #include <sys/kmem.h> 41 1.1 jmcneill 42 1.1 jmcneill #include <dev/acpi/acpireg.h> 43 1.1 jmcneill #include <dev/acpi/acpivar.h> 44 1.1 jmcneill #include <dev/acpi/acpi_pcc.h> 45 1.1 jmcneill 46 1.1 jmcneill #define PCC_MEMORY_BARRIER() membar_sync() 47 1.1 jmcneill #if defined(__aarch64__) 48 1.1 jmcneill #define PCC_DMA_BARRIER() __asm __volatile("dsb sy" ::: "memory") 49 1.1 jmcneill #else 50 1.1 jmcneill #error PCC_BARRIER not implemented on this platform 51 1.1 jmcneill #endif 52 1.1 jmcneill 53 1.1 jmcneill #define PCC_SIGNATURE(space_id) (0x50434300 | (space_id)) 54 1.1 jmcneill #define PCC_STATUS_COMMAND_COMPLETE __BIT(0) 55 1.1 jmcneill #define PCC_STATUS_COMMAND_ERROR __BIT(2) 56 1.1 jmcneill 57 1.1 jmcneill struct pcc_register { 58 1.1 jmcneill ACPI_GENERIC_ADDRESS reg_addr; 59 1.1 jmcneill uint64_t reg_preserve; 60 1.1 jmcneill uint64_t reg_set; 61 1.1 jmcneill }; 62 1.1 jmcneill 63 1.1 jmcneill struct pcc_subspace { 64 1.1 jmcneill uint8_t ss_id; 65 1.1 jmcneill uint8_t ss_type; 66 1.1 jmcneill void * ss_data; 67 1.1 jmcneill size_t ss_len; 68 1.1 jmcneill struct pcc_register ss_doorbell_reg; 69 1.1 jmcneill uint32_t ss_latency; 70 1.1 jmcneill uint32_t ss_turnaround; 71 1.1 jmcneill kmutex_t ss_lock; 72 1.1 jmcneill }; 73 1.1 jmcneill 74 1.1 jmcneill struct pcc_softc { 75 1.1 jmcneill device_t sc_dev; 76 1.1 jmcneill struct pcc_subspace * sc_ss; 77 1.1 jmcneill u_int sc_nss; 78 1.1 jmcneill }; 79 1.1 jmcneill 80 1.1 jmcneill typedef void (*pcc_subspace_callback)(ACPI_SUBTABLE_HEADER *, uint8_t, void *); 81 1.1 jmcneill 82 1.1 jmcneill static int pcc_match(device_t, cfdata_t, void *); 83 1.1 jmcneill static void pcc_attach(device_t, device_t, void *); 84 1.1 jmcneill 85 1.1 jmcneill static uint8_t pcc_subspace_foreach(ACPI_TABLE_PCCT *, 86 1.1 jmcneill pcc_subspace_callback, void *); 87 1.1 jmcneill static void pcc_subspace_attach(ACPI_SUBTABLE_HEADER *, uint8_t, void *); 88 1.1 jmcneill 89 1.1 jmcneill static struct pcc_softc *pcc_softc = NULL; 90 1.1 jmcneill 91 1.1 jmcneill CFATTACH_DECL_NEW(acpipcc, sizeof(struct pcc_softc), 92 1.1 jmcneill pcc_match, pcc_attach, NULL, NULL); 93 1.1 jmcneill 94 1.1 jmcneill static int 95 1.1 jmcneill pcc_match(device_t parent, cfdata_t cf, void *aux) 96 1.1 jmcneill { 97 1.1 jmcneill ACPI_TABLE_HEADER *hdrp = aux; 98 1.1 jmcneill 99 1.1 jmcneill return memcmp(hdrp->Signature, ACPI_SIG_PCCT, ACPI_NAMESEG_SIZE) == 0; 100 1.1 jmcneill } 101 1.1 jmcneill 102 1.1 jmcneill static void 103 1.1 jmcneill pcc_attach(device_t parent, device_t self, void *aux) 104 1.1 jmcneill { 105 1.1 jmcneill struct pcc_softc * const sc = device_private(self); 106 1.1 jmcneill ACPI_TABLE_PCCT *pcct = aux; 107 1.1 jmcneill u_int count; 108 1.1 jmcneill 109 1.1 jmcneill if (pcc_softc != NULL) { 110 1.1 jmcneill aprint_error(": Already attached!\n"); 111 1.1 jmcneill return; 112 1.1 jmcneill } 113 1.1 jmcneill 114 1.1 jmcneill count = pcc_subspace_foreach(pcct, NULL, NULL); 115 1.1 jmcneill if (count == 0) { 116 1.1 jmcneill aprint_error(": No subspaces found!\n"); 117 1.1 jmcneill return; 118 1.1 jmcneill } 119 1.1 jmcneill 120 1.1 jmcneill aprint_naive("\n"); 121 1.1 jmcneill aprint_normal(": Platform Communications Channel, %u subspace%s\n", 122 1.1 jmcneill count, count == 1 ? "" : "s"); 123 1.1 jmcneill 124 1.1 jmcneill sc->sc_dev = self; 125 1.1 jmcneill sc->sc_nss = count; 126 1.1 jmcneill sc->sc_ss = kmem_zalloc(count * sizeof(*sc->sc_ss), KM_SLEEP); 127 1.1 jmcneill pcc_subspace_foreach(pcct, pcc_subspace_attach, sc); 128 1.1 jmcneill 129 1.1 jmcneill pcc_softc = sc; 130 1.1 jmcneill } 131 1.1 jmcneill 132 1.1 jmcneill /* 133 1.1 jmcneill * pcc_subspace_foreach -- 134 1.1 jmcneill * 135 1.1 jmcneill * Find all subspaces defined in the PCCT table. If a 'func' callback 136 1.1 jmcneill * is provided, the callback is invoked for each subspace with the 137 1.1 jmcneill * table pointer, subspace ID, and 'arg' as arguments. Returns the 138 1.1 jmcneill * number of subspaces found. 139 1.1 jmcneill */ 140 1.1 jmcneill static uint8_t 141 1.1 jmcneill pcc_subspace_foreach(ACPI_TABLE_PCCT *pcct, pcc_subspace_callback func, 142 1.1 jmcneill void *arg) 143 1.1 jmcneill { 144 1.1 jmcneill ACPI_SUBTABLE_HEADER *header; 145 1.1 jmcneill char *ptr, *end; 146 1.1 jmcneill uint8_t count; 147 1.1 jmcneill 148 1.1 jmcneill end = (char *)pcct + pcct->Header.Length; 149 1.1 jmcneill ptr = (char *)pcct + sizeof(*pcct); 150 1.1 jmcneill count = 0; 151 1.1 jmcneill 152 1.1 jmcneill while (ptr < end && count < 255) { 153 1.1 jmcneill header = (ACPI_SUBTABLE_HEADER *)ptr; 154 1.1 jmcneill if (header->Length == 0 || ptr + header->Length > end) { 155 1.1 jmcneill break; 156 1.1 jmcneill } 157 1.1 jmcneill if (func != NULL) { 158 1.1 jmcneill func(header, count, arg); 159 1.1 jmcneill } 160 1.1 jmcneill ++count; 161 1.1 jmcneill ptr += header->Length; 162 1.1 jmcneill } 163 1.1 jmcneill 164 1.1 jmcneill return count; 165 1.1 jmcneill } 166 1.1 jmcneill 167 1.1 jmcneill /* 168 1.1 jmcneill * pcc_subspace_attach -- 169 1.1 jmcneill * 170 1.1 jmcneill * Allocate resources for a PCC subspace. 171 1.1 jmcneill */ 172 1.1 jmcneill static void 173 1.1 jmcneill pcc_subspace_attach(ACPI_SUBTABLE_HEADER *header, uint8_t id, void *arg) 174 1.1 jmcneill { 175 1.1 jmcneill struct pcc_softc * const sc = arg; 176 1.1 jmcneill struct pcc_subspace * const ss = &sc->sc_ss[id]; 177 1.1 jmcneill ACPI_PCCT_SUBSPACE *generic = (ACPI_PCCT_SUBSPACE *)header; 178 1.1 jmcneill 179 1.1 jmcneill ss->ss_id = id; 180 1.1 jmcneill ss->ss_type = header->Type; 181 1.1 jmcneill ss->ss_data = AcpiOsMapMemory(generic->BaseAddress, generic->Length); 182 1.1 jmcneill if (ss->ss_data == NULL) { 183 1.1 jmcneill panic("%s: couldn't map subspace 0x%02x at 0x%016" PRIx64, 184 1.1 jmcneill device_xname(sc->sc_dev), id, generic->BaseAddress); 185 1.1 jmcneill } 186 1.1 jmcneill ss->ss_len = generic->Length; 187 1.1 jmcneill ss->ss_doorbell_reg.reg_addr = generic->DoorbellRegister; 188 1.1 jmcneill ss->ss_doorbell_reg.reg_preserve = generic->PreserveMask; 189 1.1 jmcneill ss->ss_doorbell_reg.reg_set = generic->WriteMask; 190 1.1 jmcneill mutex_init(&ss->ss_lock, MUTEX_DEFAULT, IPL_NONE); 191 1.1 jmcneill } 192 1.1 jmcneill 193 1.1 jmcneill /* 194 1.1 jmcneill * pcc_wait_command -- 195 1.1 jmcneill * 196 1.1 jmcneill * Wait for command complete to be set on a PCC subspace, with timeout. 197 1.1 jmcneill */ 198 1.1 jmcneill static ACPI_STATUS 199 1.1 jmcneill pcc_wait_command(struct pcc_subspace *ss, bool first) 200 1.1 jmcneill { 201 1.1 jmcneill volatile ACPI_PCCT_SHARED_MEMORY *shmem; 202 1.1 jmcneill int retry; 203 1.1 jmcneill 204 1.1 jmcneill switch (ss->ss_type) { 205 1.1 jmcneill case ACPI_PCCT_TYPE_GENERIC_SUBSPACE: 206 1.1 jmcneill /* 207 1.1 jmcneill * OSPM does not have to wait for command complete before 208 1.1 jmcneill * sending the first command. 209 1.1 jmcneill */ 210 1.1 jmcneill if (first) { 211 1.1 jmcneill return AE_OK; 212 1.1 jmcneill } 213 1.1 jmcneill /* FALLTHROUGH */ 214 1.1 jmcneill 215 1.1 jmcneill case ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE: 216 1.1 jmcneill case ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE_TYPE2: 217 1.1 jmcneill /* 218 1.1 jmcneill * Wait for the command complete bit to be set in the status 219 1.1 jmcneill * register. 220 1.1 jmcneill */ 221 1.1 jmcneill shmem = ss->ss_data; 222 1.1 jmcneill retry = imax(1000, ss->ss_turnaround + ss->ss_latency * 100); 223 1.1 jmcneill while (retry > 0) { 224 1.1 jmcneill PCC_MEMORY_BARRIER(); 225 1.1 jmcneill if ((shmem->Status & PCC_STATUS_COMMAND_COMPLETE) != 0) 226 1.1 jmcneill return AE_OK; 227 1.1 jmcneill delay(10); 228 1.1 jmcneill retry -= 10; 229 1.1 jmcneill } 230 1.1 jmcneill return AE_TIME; 231 1.1 jmcneill 232 1.1 jmcneill default: 233 1.1 jmcneill return AE_NOT_IMPLEMENTED; 234 1.1 jmcneill } 235 1.1 jmcneill } 236 1.1 jmcneill 237 1.1 jmcneill /* 238 1.1 jmcneill * pcc_doorbell -- 239 1.1 jmcneill * 240 1.1 jmcneill * Ring the door bell by writing to the doorbell register. 241 1.1 jmcneill */ 242 1.1 jmcneill static ACPI_STATUS 243 1.1 jmcneill pcc_doorbell(struct pcc_register *reg) 244 1.1 jmcneill { 245 1.1 jmcneill ACPI_STATUS rv; 246 1.1 jmcneill UINT64 val; 247 1.1 jmcneill 248 1.1 jmcneill rv = AcpiRead(&val, ®->reg_addr); 249 1.1 jmcneill if (ACPI_FAILURE(rv)) { 250 1.1 jmcneill return rv; 251 1.1 jmcneill } 252 1.1 jmcneill val &= reg->reg_preserve; 253 1.1 jmcneill val |= reg->reg_set; 254 1.1 jmcneill return AcpiWrite(val, ®->reg_addr); 255 1.1 jmcneill } 256 1.1 jmcneill 257 1.1 jmcneill /* 258 1.1 jmcneill * pcc_send_command -- 259 1.1 jmcneill * 260 1.1 jmcneill * Write a command into PCC subspace and ring the doorbell. 261 1.1 jmcneill */ 262 1.1 jmcneill static ACPI_STATUS 263 1.1 jmcneill pcc_send_command(struct pcc_subspace *ss, ACPI_GENERIC_ADDRESS *reg, 264 1.1 jmcneill uint32_t command, int flags, ACPI_INTEGER val) 265 1.1 jmcneill { 266 1.1 jmcneill volatile ACPI_PCCT_SHARED_MEMORY *shmem = ss->ss_data; 267 1.1 jmcneill uint8_t *data = __UNVOLATILE(shmem + 1); 268 1.1 jmcneill UINT64 tmp, mask; 269 1.1 jmcneill 270 1.1 jmcneill KASSERT(ss->ss_type == ACPI_PCCT_TYPE_GENERIC_SUBSPACE || 271 1.1 jmcneill ss->ss_type == ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE || 272 1.1 jmcneill ss->ss_type == ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE_TYPE2); 273 1.1 jmcneill 274 1.1 jmcneill shmem->Signature = PCC_SIGNATURE(ss->ss_id); 275 1.1 jmcneill shmem->Command = command & 0xff; 276 1.1 jmcneill if ((flags & PCC_WRITE) != 0) { 277 1.1 jmcneill mask = __BITS(reg->BitOffset + reg->BitWidth - 1, 278 1.1 jmcneill reg->BitOffset); 279 1.1 jmcneill ACPI_MOVE_64_TO_64(&tmp, data + reg->Address); 280 1.1 jmcneill tmp &= mask; 281 1.1 jmcneill tmp |= __SHIFTIN(val, mask); 282 1.1 jmcneill ACPI_MOVE_64_TO_64(data + reg->Address, &tmp); 283 1.1 jmcneill } 284 1.1 jmcneill PCC_MEMORY_BARRIER(); 285 1.1 jmcneill shmem->Status &= ~(PCC_STATUS_COMMAND_COMPLETE | 286 1.1 jmcneill PCC_STATUS_COMMAND_ERROR); 287 1.1 jmcneill PCC_DMA_BARRIER(); 288 1.1 jmcneill 289 1.1 jmcneill return pcc_doorbell(&ss->ss_doorbell_reg); 290 1.1 jmcneill } 291 1.1 jmcneill 292 1.1 jmcneill /* 293 1.1 jmcneill * pcc_receive_response -- 294 1.1 jmcneill * 295 1.1 jmcneill * Process a command response in PCC subspace. 296 1.1 jmcneill */ 297 1.1 jmcneill static ACPI_STATUS 298 1.1 jmcneill pcc_receive_response(struct pcc_subspace *ss, ACPI_GENERIC_ADDRESS *reg, 299 1.1 jmcneill int flags, ACPI_INTEGER *val) 300 1.1 jmcneill { 301 1.1 jmcneill volatile ACPI_PCCT_SHARED_MEMORY *shmem = ss->ss_data; 302 1.1 jmcneill const uint8_t *data = __UNVOLATILE(shmem + 1); 303 1.1 jmcneill UINT64 tmp, mask; 304 1.1 jmcneill 305 1.1 jmcneill KASSERT(ss->ss_type == ACPI_PCCT_TYPE_GENERIC_SUBSPACE || 306 1.1 jmcneill ss->ss_type == ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE || 307 1.1 jmcneill ss->ss_type == ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE_TYPE2); 308 1.1 jmcneill 309 1.1 jmcneill KASSERT((shmem->Status & PCC_STATUS_COMMAND_COMPLETE) != 0); 310 1.1 jmcneill 311 1.1 jmcneill if ((shmem->Status & PCC_STATUS_COMMAND_ERROR) != 0) { 312 1.1 jmcneill return AE_ERROR; 313 1.1 jmcneill } 314 1.1 jmcneill 315 1.1 jmcneill if ((flags & PCC_READ) != 0) { 316 1.1 jmcneill mask = __BITS(reg->BitOffset + reg->BitWidth - 1, 317 1.1 jmcneill reg->BitOffset); 318 1.1 jmcneill ACPI_MOVE_64_TO_64(&tmp, data + reg->Address); 319 1.1 jmcneill *val = __SHIFTOUT(tmp, mask); 320 1.1 jmcneill } 321 1.1 jmcneill 322 1.1 jmcneill return AE_OK; 323 1.1 jmcneill } 324 1.1 jmcneill 325 1.1 jmcneill /* 326 1.1 jmcneill * pcc_message -- 327 1.1 jmcneill * 328 1.1 jmcneill * Send or receive a command over a PCC subspace. 329 1.1 jmcneill */ 330 1.1 jmcneill ACPI_STATUS 331 1.1 jmcneill pcc_message(ACPI_GENERIC_ADDRESS *reg, uint32_t command, int flags, 332 1.1 jmcneill ACPI_INTEGER *val) 333 1.1 jmcneill { 334 1.1 jmcneill struct pcc_softc * const sc = pcc_softc; 335 1.1 jmcneill struct pcc_subspace *ss; 336 1.1 jmcneill uint8_t ss_id; 337 1.1 jmcneill ACPI_STATUS rv; 338 1.1 jmcneill 339 1.1 jmcneill /* The "Access Width" in the PCC GAS is the Subspace ID */ 340 1.1 jmcneill ss_id = reg->AccessWidth; 341 1.1 jmcneill 342 1.1 jmcneill if (sc == NULL) { 343 1.1 jmcneill return AE_NOT_CONFIGURED; 344 1.1 jmcneill } 345 1.1 jmcneill if (ss_id >= sc->sc_nss) { 346 1.1 jmcneill return AE_NOT_FOUND; 347 1.1 jmcneill } 348 1.1 jmcneill 349 1.1 jmcneill ss = &sc->sc_ss[ss_id]; 350 1.1 jmcneill switch (ss->ss_type) { 351 1.1 jmcneill case ACPI_PCCT_TYPE_GENERIC_SUBSPACE: 352 1.1 jmcneill case ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE: 353 1.1 jmcneill case ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE_TYPE2: 354 1.1 jmcneill break; 355 1.1 jmcneill default: 356 1.1 jmcneill return AE_NOT_IMPLEMENTED; 357 1.1 jmcneill } 358 1.1 jmcneill 359 1.1 jmcneill mutex_enter(&ss->ss_lock); 360 1.1 jmcneill 361 1.1 jmcneill /* Wait for any previous commands to finish. */ 362 1.1 jmcneill rv = pcc_wait_command(ss, true); 363 1.1 jmcneill if (ACPI_FAILURE(rv)) { 364 1.1 jmcneill goto unlock; 365 1.1 jmcneill } 366 1.1 jmcneill 367 1.1 jmcneill /* Place command into subspace and ring the doorbell */ 368 1.1 jmcneill rv = pcc_send_command(ss, reg, command, flags, *val); 369 1.1 jmcneill if (ACPI_FAILURE(rv)) { 370 1.1 jmcneill goto unlock; 371 1.1 jmcneill } 372 1.1 jmcneill 373 1.1 jmcneill /* Wait for command to complete. */ 374 1.1 jmcneill rv = pcc_wait_command(ss, false); 375 1.1 jmcneill if (ACPI_FAILURE(rv)) { 376 1.1 jmcneill goto unlock; 377 1.1 jmcneill } 378 1.1 jmcneill 379 1.1 jmcneill /* Process the command response */ 380 1.1 jmcneill rv = pcc_receive_response(ss, reg, flags, val); 381 1.1 jmcneill 382 1.1 jmcneill if (ss->ss_turnaround != 0) 383 1.1 jmcneill delay(ss->ss_turnaround); 384 1.1 jmcneill 385 1.1 jmcneill unlock: 386 1.1 jmcneill mutex_exit(&ss->ss_lock); 387 1.1 jmcneill 388 1.1 jmcneill return rv; 389 1.1 jmcneill } 390