1243730Srwatson/*- 2243730Srwatson * Copyright (c) 2000 Michael Smith 3243730Srwatson * Copyright (c) 2000 BSDi 4243730Srwatson * All rights reserved. 5243730Srwatson * 6243730Srwatson * Redistribution and use in source and binary forms, with or without 7243730Srwatson * modification, are permitted provided that the following conditions 8243730Srwatson * are met: 9243730Srwatson * 1. Redistributions of source code must retain the above copyright 10243730Srwatson * notice, this list of conditions and the following disclaimer. 11243730Srwatson * 2. Redistributions in binary form must reproduce the above copyright 12243730Srwatson * notice, this list of conditions and the following disclaimer in the 13243730Srwatson * documentation and/or other materials provided with the distribution. 14243730Srwatson * 15243730Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16243730Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17243730Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18243730Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19243730Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20243730Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21243730Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22243730Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23243730Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24243730Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25243730Srwatson * SUCH DAMAGE. 26243730Srwatson */ 27243730Srwatson 28243730Srwatson#include <sys/cdefs.h> 29243730Srwatson__FBSDID("$FreeBSD: stable/10/sys/dev/acpica/acpi_resource.c 333080 2018-04-28 00:16:54Z jhb $"); 30243730Srwatson 31243730Srwatson#include "opt_acpi.h" 32243730Srwatson#include <sys/param.h> 33243730Srwatson#include <sys/kernel.h> 34243730Srwatson#include <sys/bus.h> 35243730Srwatson#include <sys/limits.h> 36243730Srwatson#include <sys/malloc.h> 37243730Srwatson#include <sys/module.h> 38243730Srwatson 39243730Srwatson#include <machine/bus.h> 40243730Srwatson#include <machine/resource.h> 41243734Srwatson#include <sys/rman.h> 42243730Srwatson 43243730Srwatson#include <contrib/dev/acpica/include/acpi.h> 44243730Srwatson#include <contrib/dev/acpica/include/accommon.h> 45243730Srwatson 46243730Srwatson#include <dev/acpica/acpivar.h> 47243730Srwatson 48243730Srwatson/* Hooks for the ACPI CA debugging infrastructure */ 49243730Srwatson#define _COMPONENT ACPI_BUS 50243730SrwatsonACPI_MODULE_NAME("RESOURCE") 51243730Srwatson 52243730Srwatsonstruct lookup_irq_request { 53243730Srwatson ACPI_RESOURCE *acpi_res; 54243730Srwatson struct resource *res; 55243730Srwatson int counter; 56243730Srwatson int rid; 57243730Srwatson int found; 58243730Srwatson}; 59243730Srwatson 60243730Srwatsonstatic ACPI_STATUS 61243730Srwatsonacpi_lookup_irq_handler(ACPI_RESOURCE *res, void *context) 62243730Srwatson{ 63243730Srwatson struct lookup_irq_request *req; 64243730Srwatson size_t len; 65243730Srwatson u_int irqnum, irq; 66243730Srwatson 67243730Srwatson switch (res->Type) { 68243730Srwatson case ACPI_RESOURCE_TYPE_IRQ: 69243730Srwatson irqnum = res->Data.Irq.InterruptCount; 70243730Srwatson irq = res->Data.Irq.Interrupts[0]; 71243730Srwatson len = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ); 72243730Srwatson break; 73243730Srwatson case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 74243730Srwatson irqnum = res->Data.ExtendedIrq.InterruptCount; 75243730Srwatson irq = res->Data.ExtendedIrq.Interrupts[0]; 76243730Srwatson len = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ); 77243730Srwatson break; 78243730Srwatson default: 79243730Srwatson return (AE_OK); 80243730Srwatson } 81243730Srwatson if (irqnum != 1) 82243730Srwatson return (AE_OK); 83243730Srwatson req = (struct lookup_irq_request *)context; 84243730Srwatson if (req->counter != req->rid) { 85243730Srwatson req->counter++; 86243730Srwatson return (AE_OK); 87243730Srwatson } 88243730Srwatson req->found = 1; 89243730Srwatson KASSERT(irq == rman_get_start(req->res), 90243730Srwatson ("IRQ resources do not match")); 91243730Srwatson bcopy(res, req->acpi_res, len); 92243730Srwatson return (AE_CTRL_TERMINATE); 93243730Srwatson} 94243730Srwatson 95243730SrwatsonACPI_STATUS 96243730Srwatsonacpi_lookup_irq_resource(device_t dev, int rid, struct resource *res, 97243730Srwatson ACPI_RESOURCE *acpi_res) 98243730Srwatson{ 99243730Srwatson struct lookup_irq_request req; 100243730Srwatson ACPI_STATUS status; 101243730Srwatson 102243730Srwatson req.acpi_res = acpi_res; 103243730Srwatson req.res = res; 104243730Srwatson req.counter = 0; 105243730Srwatson req.rid = rid; 106243730Srwatson req.found = 0; 107243730Srwatson status = AcpiWalkResources(acpi_get_handle(dev), "_CRS", 108243730Srwatson acpi_lookup_irq_handler, &req); 109243730Srwatson if (ACPI_SUCCESS(status) && req.found == 0) 110243730Srwatson status = AE_NOT_FOUND; 111243730Srwatson return (status); 112243730Srwatson} 113243730Srwatson 114243730Srwatsonvoid 115243730Srwatsonacpi_config_intr(device_t dev, ACPI_RESOURCE *res) 116243730Srwatson{ 117243730Srwatson u_int irq; 118243730Srwatson int pol, trig; 119243730Srwatson 120243730Srwatson switch (res->Type) { 121243730Srwatson case ACPI_RESOURCE_TYPE_IRQ: 122243730Srwatson KASSERT(res->Data.Irq.InterruptCount == 1, 123243730Srwatson ("%s: multiple interrupts", __func__)); 124243730Srwatson irq = res->Data.Irq.Interrupts[0]; 125243730Srwatson trig = res->Data.Irq.Triggering; 126243730Srwatson pol = res->Data.Irq.Polarity; 127243730Srwatson break; 128243730Srwatson case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 129243730Srwatson KASSERT(res->Data.ExtendedIrq.InterruptCount == 1, 130243730Srwatson ("%s: multiple interrupts", __func__)); 131243730Srwatson irq = res->Data.ExtendedIrq.Interrupts[0]; 132243730Srwatson trig = res->Data.ExtendedIrq.Triggering; 133243730Srwatson pol = res->Data.ExtendedIrq.Polarity; 134243730Srwatson break; 135243730Srwatson default: 136243730Srwatson panic("%s: bad resource type %u", __func__, res->Type); 137243730Srwatson } 138243730Srwatson 139243730Srwatson#if defined(__amd64__) || defined(__i386__) 140243730Srwatson /* 141243730Srwatson * XXX: Certain BIOSes have buggy AML that specify an IRQ that is 142243730Srwatson * edge-sensitive and active-lo. However, edge-sensitive IRQs 143243730Srwatson * should be active-hi. Force IRQs with an ISA IRQ value to be 144243730Srwatson * active-hi instead. 145243730Srwatson */ 146243730Srwatson if (irq < 16 && trig == ACPI_EDGE_SENSITIVE && pol == ACPI_ACTIVE_LOW) 147243730Srwatson pol = ACPI_ACTIVE_HIGH; 148243730Srwatson#endif 149243730Srwatson BUS_CONFIG_INTR(dev, irq, (trig == ACPI_EDGE_SENSITIVE) ? 150243730Srwatson INTR_TRIGGER_EDGE : INTR_TRIGGER_LEVEL, (pol == ACPI_ACTIVE_HIGH) ? 151243730Srwatson INTR_POLARITY_HIGH : INTR_POLARITY_LOW); 152243730Srwatson} 153243730Srwatson 154243730Srwatsonstruct acpi_resource_context { 155243730Srwatson struct acpi_parse_resource_set *set; 156243730Srwatson device_t dev; 157243730Srwatson void *context; 158243730Srwatson}; 159243730Srwatson 160243730Srwatson#ifdef ACPI_DEBUG_OUTPUT 161243730Srwatsonstatic const char * 162243730Srwatsonacpi_address_range_name(UINT8 ResourceType) 163243730Srwatson{ 164243730Srwatson static char buf[16]; 165243730Srwatson 166243730Srwatson switch (ResourceType) { 167243730Srwatson case ACPI_MEMORY_RANGE: 168243730Srwatson return ("Memory"); 169243730Srwatson case ACPI_IO_RANGE: 170243730Srwatson return ("IO"); 171243730Srwatson case ACPI_BUS_NUMBER_RANGE: 172243730Srwatson return ("Bus Number"); 173243730Srwatson default: 174243730Srwatson snprintf(buf, sizeof(buf), "type %u", ResourceType); 175243730Srwatson return (buf); 176243730Srwatson } 177243730Srwatson} 178243730Srwatson#endif 179243730Srwatson 180243730Srwatsonstatic ACPI_STATUS 181243730Srwatsonacpi_parse_resource(ACPI_RESOURCE *res, void *context) 182243730Srwatson{ 183243730Srwatson struct acpi_parse_resource_set *set; 184243730Srwatson struct acpi_resource_context *arc; 185243730Srwatson UINT64 min, max, length, gran; 186243730Srwatson const char *name; 187243730Srwatson device_t dev; 188243730Srwatson 189243730Srwatson arc = context; 190243730Srwatson dev = arc->dev; 191243730Srwatson set = arc->set; 192243730Srwatson 193243730Srwatson switch (res->Type) { 194243730Srwatson case ACPI_RESOURCE_TYPE_END_TAG: 195243730Srwatson ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "EndTag\n")); 196243730Srwatson break; 197243730Srwatson case ACPI_RESOURCE_TYPE_FIXED_IO: 198243730Srwatson if (res->Data.FixedIo.AddressLength <= 0) 199243730Srwatson break; 200243730Srwatson ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "FixedIo 0x%x/%d\n", 201243730Srwatson res->Data.FixedIo.Address, res->Data.FixedIo.AddressLength)); 202243730Srwatson set->set_ioport(dev, arc->context, res->Data.FixedIo.Address, 203243730Srwatson res->Data.FixedIo.AddressLength); 204243730Srwatson break; 205 case ACPI_RESOURCE_TYPE_IO: 206 if (res->Data.Io.AddressLength <= 0) 207 break; 208 if (res->Data.Io.Minimum == res->Data.Io.Maximum) { 209 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Io 0x%x/%d\n", 210 res->Data.Io.Minimum, res->Data.Io.AddressLength)); 211 set->set_ioport(dev, arc->context, res->Data.Io.Minimum, 212 res->Data.Io.AddressLength); 213 } else { 214 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Io 0x%x-0x%x/%d\n", 215 res->Data.Io.Minimum, res->Data.Io.Maximum, 216 res->Data.Io.AddressLength)); 217 set->set_iorange(dev, arc->context, res->Data.Io.Minimum, 218 res->Data.Io.Maximum, res->Data.Io.AddressLength, 219 res->Data.Io.Alignment); 220 } 221 break; 222 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32: 223 if (res->Data.FixedMemory32.AddressLength <= 0) 224 break; 225 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "FixedMemory32 0x%x/%d\n", 226 res->Data.FixedMemory32.Address, 227 res->Data.FixedMemory32.AddressLength)); 228 set->set_memory(dev, arc->context, res->Data.FixedMemory32.Address, 229 res->Data.FixedMemory32.AddressLength); 230 break; 231 case ACPI_RESOURCE_TYPE_MEMORY32: 232 if (res->Data.Memory32.AddressLength <= 0) 233 break; 234 if (res->Data.Memory32.Minimum == res->Data.Memory32.Maximum) { 235 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory32 0x%x/%d\n", 236 res->Data.Memory32.Minimum, res->Data.Memory32.AddressLength)); 237 set->set_memory(dev, arc->context, res->Data.Memory32.Minimum, 238 res->Data.Memory32.AddressLength); 239 } else { 240 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory32 0x%x-0x%x/%d\n", 241 res->Data.Memory32.Minimum, res->Data.Memory32.Maximum, 242 res->Data.Memory32.AddressLength)); 243 set->set_memoryrange(dev, arc->context, res->Data.Memory32.Minimum, 244 res->Data.Memory32.Maximum, res->Data.Memory32.AddressLength, 245 res->Data.Memory32.Alignment); 246 } 247 break; 248 case ACPI_RESOURCE_TYPE_MEMORY24: 249 if (res->Data.Memory24.AddressLength <= 0) 250 break; 251 if (res->Data.Memory24.Minimum == res->Data.Memory24.Maximum) { 252 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory24 0x%x/%d\n", 253 res->Data.Memory24.Minimum, res->Data.Memory24.AddressLength)); 254 set->set_memory(dev, arc->context, res->Data.Memory24.Minimum, 255 res->Data.Memory24.AddressLength); 256 } else { 257 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory24 0x%x-0x%x/%d\n", 258 res->Data.Memory24.Minimum, res->Data.Memory24.Maximum, 259 res->Data.Memory24.AddressLength)); 260 set->set_memoryrange(dev, arc->context, res->Data.Memory24.Minimum, 261 res->Data.Memory24.Maximum, res->Data.Memory24.AddressLength, 262 res->Data.Memory24.Alignment); 263 } 264 break; 265 case ACPI_RESOURCE_TYPE_IRQ: 266 /* 267 * from 1.0b 6.4.2 268 * "This structure is repeated for each separate interrupt 269 * required" 270 */ 271 set->set_irq(dev, arc->context, res->Data.Irq.Interrupts, 272 res->Data.Irq.InterruptCount, res->Data.Irq.Triggering, 273 res->Data.Irq.Polarity); 274 break; 275 case ACPI_RESOURCE_TYPE_DMA: 276 /* 277 * from 1.0b 6.4.3 278 * "This structure is repeated for each separate DMA channel 279 * required" 280 */ 281 set->set_drq(dev, arc->context, res->Data.Dma.Channels, 282 res->Data.Dma.ChannelCount); 283 break; 284 case ACPI_RESOURCE_TYPE_START_DEPENDENT: 285 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "start dependent functions\n")); 286 set->set_start_dependent(dev, arc->context, 287 res->Data.StartDpf.CompatibilityPriority); 288 break; 289 case ACPI_RESOURCE_TYPE_END_DEPENDENT: 290 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "end dependent functions\n")); 291 set->set_end_dependent(dev, arc->context); 292 break; 293 case ACPI_RESOURCE_TYPE_ADDRESS16: 294 case ACPI_RESOURCE_TYPE_ADDRESS32: 295 case ACPI_RESOURCE_TYPE_ADDRESS64: 296 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64: 297 switch (res->Type) { 298 case ACPI_RESOURCE_TYPE_ADDRESS16: 299 gran = res->Data.Address16.Address.Granularity; 300 min = res->Data.Address16.Address.Minimum; 301 max = res->Data.Address16.Address.Maximum; 302 length = res->Data.Address16.Address.AddressLength; 303 name = "Address16"; 304 break; 305 case ACPI_RESOURCE_TYPE_ADDRESS32: 306 gran = res->Data.Address32.Address.Granularity; 307 min = res->Data.Address32.Address.Minimum; 308 max = res->Data.Address32.Address.Maximum; 309 length = res->Data.Address32.Address.AddressLength; 310 name = "Address32"; 311 break; 312 case ACPI_RESOURCE_TYPE_ADDRESS64: 313 gran = res->Data.Address64.Address.Granularity; 314 min = res->Data.Address64.Address.Minimum; 315 max = res->Data.Address64.Address.Maximum; 316 length = res->Data.Address64.Address.AddressLength; 317 name = "Address64"; 318 break; 319 default: 320 KASSERT(res->Type == ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64, 321 ("should never happen")); 322 gran = res->Data.ExtAddress64.Address.Granularity; 323 min = res->Data.ExtAddress64.Address.Minimum; 324 max = res->Data.ExtAddress64.Address.Maximum; 325 length = res->Data.ExtAddress64.Address.AddressLength; 326 name = "ExtAddress64"; 327 break; 328 } 329 if (length <= 0) 330 break; 331 if (res->Data.Address.ProducerConsumer != ACPI_CONSUMER) { 332 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, 333 "ignored %s %s producer\n", name, 334 acpi_address_range_name(res->Data.Address.ResourceType))); 335 break; 336 } 337 if (res->Data.Address.ResourceType != ACPI_MEMORY_RANGE && 338 res->Data.Address.ResourceType != ACPI_IO_RANGE) { 339 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, 340 "ignored %s for non-memory, non-I/O\n", name)); 341 break; 342 } 343 344#ifdef __i386__ 345 if (min > ULONG_MAX || (res->Data.Address.MaxAddressFixed && max > 346 ULONG_MAX)) { 347 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "ignored %s above 4G\n", 348 name)); 349 break; 350 } 351 if (max > ULONG_MAX) 352 max = ULONG_MAX; 353#endif 354 if (res->Data.Address.MinAddressFixed == ACPI_ADDRESS_FIXED && 355 res->Data.Address.MaxAddressFixed == ACPI_ADDRESS_FIXED) { 356 if (res->Data.Address.ResourceType == ACPI_MEMORY_RANGE) { 357 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "%s/Memory 0x%jx/%ju\n", 358 name, (uintmax_t)min, (uintmax_t)length)); 359 set->set_memory(dev, arc->context, min, length); 360 } else { 361 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "%s/IO 0x%jx/%ju\n", name, 362 (uintmax_t)min, (uintmax_t)length)); 363 set->set_ioport(dev, arc->context, min, length); 364 } 365 } else { 366 if (res->Data.Address32.ResourceType == ACPI_MEMORY_RANGE) { 367 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, 368 "%s/Memory 0x%jx-0x%jx/%ju\n", name, (uintmax_t)min, 369 (uintmax_t)max, (uintmax_t)length)); 370 set->set_memoryrange(dev, arc->context, min, max, length, gran); 371 } else { 372 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "%s/IO 0x%jx-0x%jx/%ju\n", 373 name, (uintmax_t)min, (uintmax_t)max, (uintmax_t)length)); 374 set->set_iorange(dev, arc->context, min, max, length, gran); 375 } 376 } 377 break; 378 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 379 if (res->Data.ExtendedIrq.ProducerConsumer != ACPI_CONSUMER) { 380 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "ignored ExtIRQ producer\n")); 381 break; 382 } 383 set->set_ext_irq(dev, arc->context, res->Data.ExtendedIrq.Interrupts, 384 res->Data.ExtendedIrq.InterruptCount, 385 res->Data.ExtendedIrq.Triggering, res->Data.ExtendedIrq.Polarity); 386 break; 387 case ACPI_RESOURCE_TYPE_VENDOR: 388 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, 389 "unimplemented VendorSpecific resource\n")); 390 break; 391 default: 392 break; 393 } 394 return (AE_OK); 395} 396 397/* 398 * Fetch a device's resources and associate them with the device. 399 * 400 * Note that it might be nice to also locate ACPI-specific resource items, such 401 * as GPE bits. 402 * 403 * We really need to split the resource-fetching code out from the 404 * resource-parsing code, since we may want to use the parsing 405 * code for _PRS someday. 406 */ 407ACPI_STATUS 408acpi_parse_resources(device_t dev, ACPI_HANDLE handle, 409 struct acpi_parse_resource_set *set, void *arg) 410{ 411 struct acpi_resource_context arc; 412 ACPI_STATUS status; 413 414 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 415 416 set->set_init(dev, arg, &arc.context); 417 arc.set = set; 418 arc.dev = dev; 419 status = AcpiWalkResources(handle, "_CRS", acpi_parse_resource, &arc); 420 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { 421 printf("can't fetch resources for %s - %s\n", 422 acpi_name(handle), AcpiFormatException(status)); 423 return_ACPI_STATUS (status); 424 } 425 set->set_done(dev, arc.context); 426 return_ACPI_STATUS (AE_OK); 427} 428 429/* 430 * Resource-set vectors used to attach _CRS-derived resources 431 * to an ACPI device. 432 */ 433static void acpi_res_set_init(device_t dev, void *arg, void **context); 434static void acpi_res_set_done(device_t dev, void *context); 435static void acpi_res_set_ioport(device_t dev, void *context, 436 uint64_t base, uint64_t length); 437static void acpi_res_set_iorange(device_t dev, void *context, 438 uint64_t low, uint64_t high, 439 uint64_t length, uint64_t align); 440static void acpi_res_set_memory(device_t dev, void *context, 441 uint64_t base, uint64_t length); 442static void acpi_res_set_memoryrange(device_t dev, void *context, 443 uint64_t low, uint64_t high, 444 uint64_t length, uint64_t align); 445static void acpi_res_set_irq(device_t dev, void *context, uint8_t *irq, 446 int count, int trig, int pol); 447static void acpi_res_set_ext_irq(device_t dev, void *context, 448 uint32_t *irq, int count, int trig, int pol); 449static void acpi_res_set_drq(device_t dev, void *context, uint8_t *drq, 450 int count); 451static void acpi_res_set_start_dependent(device_t dev, void *context, 452 int preference); 453static void acpi_res_set_end_dependent(device_t dev, void *context); 454 455struct acpi_parse_resource_set acpi_res_parse_set = { 456 acpi_res_set_init, 457 acpi_res_set_done, 458 acpi_res_set_ioport, 459 acpi_res_set_iorange, 460 acpi_res_set_memory, 461 acpi_res_set_memoryrange, 462 acpi_res_set_irq, 463 acpi_res_set_ext_irq, 464 acpi_res_set_drq, 465 acpi_res_set_start_dependent, 466 acpi_res_set_end_dependent 467}; 468 469struct acpi_res_context { 470 int ar_nio; 471 int ar_nmem; 472 int ar_nirq; 473 int ar_ndrq; 474 void *ar_parent; 475}; 476 477static void 478acpi_res_set_init(device_t dev, void *arg, void **context) 479{ 480 struct acpi_res_context *cp; 481 482 if ((cp = AcpiOsAllocate(sizeof(*cp))) != NULL) { 483 bzero(cp, sizeof(*cp)); 484 cp->ar_parent = arg; 485 *context = cp; 486 } 487} 488 489static void 490acpi_res_set_done(device_t dev, void *context) 491{ 492 struct acpi_res_context *cp = (struct acpi_res_context *)context; 493 494 if (cp == NULL) 495 return; 496 AcpiOsFree(cp); 497} 498 499static void 500acpi_res_set_ioport(device_t dev, void *context, uint64_t base, 501 uint64_t length) 502{ 503 struct acpi_res_context *cp = (struct acpi_res_context *)context; 504 505 if (cp == NULL) 506 return; 507 bus_set_resource(dev, SYS_RES_IOPORT, cp->ar_nio++, base, length); 508} 509 510static void 511acpi_res_set_iorange(device_t dev, void *context, uint64_t low, 512 uint64_t high, uint64_t length, uint64_t align) 513{ 514 struct acpi_res_context *cp = (struct acpi_res_context *)context; 515 516 if (cp == NULL) 517 return; 518 519 /* 520 * XXX: Some BIOSes contain buggy _CRS entries where fixed I/O 521 * ranges have the maximum base address (_MAX) to the end of the 522 * I/O range instead of the start. These are then treated as a 523 * relocatable I/O range rather than a fixed I/O resource. As a 524 * workaround, treat I/O resources encoded this way as fixed I/O 525 * ports. 526 */ 527 if (high == (low + length)) { 528 if (bootverbose) 529 device_printf(dev, 530 "_CRS has fixed I/O port range defined as relocatable\n"); 531 532 bus_set_resource(dev, SYS_RES_IOPORT, cp->ar_nio++, low, length); 533 return; 534 } 535 536 device_printf(dev, "I/O range not supported\n"); 537} 538 539static void 540acpi_res_set_memory(device_t dev, void *context, uint64_t base, 541 uint64_t length) 542{ 543 struct acpi_res_context *cp = (struct acpi_res_context *)context; 544 545 if (cp == NULL) 546 return; 547 548 bus_set_resource(dev, SYS_RES_MEMORY, cp->ar_nmem++, base, length); 549} 550 551static void 552acpi_res_set_memoryrange(device_t dev, void *context, uint64_t low, 553 uint64_t high, uint64_t length, uint64_t align) 554{ 555 struct acpi_res_context *cp = (struct acpi_res_context *)context; 556 557 if (cp == NULL) 558 return; 559 device_printf(dev, "memory range not supported\n"); 560} 561 562static void 563acpi_res_set_irq(device_t dev, void *context, uint8_t *irq, int count, 564 int trig, int pol) 565{ 566 struct acpi_res_context *cp = (struct acpi_res_context *)context; 567 568 if (cp == NULL || irq == NULL) 569 return; 570 571 /* This implements no resource relocation. */ 572 if (count != 1) 573 return; 574 575 bus_set_resource(dev, SYS_RES_IRQ, cp->ar_nirq++, *irq, 1); 576} 577 578static void 579acpi_res_set_ext_irq(device_t dev, void *context, uint32_t *irq, int count, 580 int trig, int pol) 581{ 582 struct acpi_res_context *cp = (struct acpi_res_context *)context; 583 584 if (cp == NULL || irq == NULL) 585 return; 586 587 /* This implements no resource relocation. */ 588 if (count != 1) 589 return; 590 591 bus_set_resource(dev, SYS_RES_IRQ, cp->ar_nirq++, *irq, 1); 592} 593 594static void 595acpi_res_set_drq(device_t dev, void *context, uint8_t *drq, int count) 596{ 597 struct acpi_res_context *cp = (struct acpi_res_context *)context; 598 599 if (cp == NULL || drq == NULL) 600 return; 601 602 /* This implements no resource relocation. */ 603 if (count != 1) 604 return; 605 606 bus_set_resource(dev, SYS_RES_DRQ, cp->ar_ndrq++, *drq, 1); 607} 608 609static void 610acpi_res_set_start_dependent(device_t dev, void *context, int preference) 611{ 612 struct acpi_res_context *cp = (struct acpi_res_context *)context; 613 614 if (cp == NULL) 615 return; 616 device_printf(dev, "dependent functions not supported\n"); 617} 618 619static void 620acpi_res_set_end_dependent(device_t dev, void *context) 621{ 622 struct acpi_res_context *cp = (struct acpi_res_context *)context; 623 624 if (cp == NULL) 625 return; 626 device_printf(dev, "dependent functions not supported\n"); 627} 628 629/* 630 * Resource-owning placeholders for IO and memory pseudo-devices. 631 * 632 * This code allocates system resources that will be used by ACPI 633 * child devices. The acpi parent manages these resources through a 634 * private rman. 635 */ 636 637static int acpi_sysres_rid = 100; 638 639static int acpi_sysres_probe(device_t dev); 640static int acpi_sysres_attach(device_t dev); 641 642static device_method_t acpi_sysres_methods[] = { 643 /* Device interface */ 644 DEVMETHOD(device_probe, acpi_sysres_probe), 645 DEVMETHOD(device_attach, acpi_sysres_attach), 646 647 DEVMETHOD_END 648}; 649 650static driver_t acpi_sysres_driver = { 651 "acpi_sysresource", 652 acpi_sysres_methods, 653 0, 654}; 655 656static devclass_t acpi_sysres_devclass; 657DRIVER_MODULE(acpi_sysresource, acpi, acpi_sysres_driver, acpi_sysres_devclass, 658 0, 0); 659MODULE_DEPEND(acpi_sysresource, acpi, 1, 1, 1); 660 661static int 662acpi_sysres_probe(device_t dev) 663{ 664 static char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL }; 665 666 if (acpi_disabled("sysresource") || 667 ACPI_ID_PROBE(device_get_parent(dev), dev, sysres_ids) == NULL) 668 return (ENXIO); 669 670 device_set_desc(dev, "System Resource"); 671 device_quiet(dev); 672 return (BUS_PROBE_DEFAULT); 673} 674 675static int 676acpi_sysres_attach(device_t dev) 677{ 678 device_t bus; 679 struct resource_list_entry *bus_rle, *dev_rle; 680 struct resource_list *bus_rl, *dev_rl; 681 int done, type; 682 u_long start, end, count; 683 684 /* 685 * Loop through all current resources to see if the new one overlaps 686 * any existing ones. If so, grow the old one up and/or down 687 * accordingly. Discard any that are wholly contained in the old. If 688 * the resource is unique, add it to the parent. It will later go into 689 * the rman pool. 690 */ 691 bus = device_get_parent(dev); 692 dev_rl = BUS_GET_RESOURCE_LIST(bus, dev); 693 bus_rl = BUS_GET_RESOURCE_LIST(device_get_parent(bus), bus); 694 STAILQ_FOREACH(dev_rle, dev_rl, link) { 695 if (dev_rle->type != SYS_RES_IOPORT && dev_rle->type != SYS_RES_MEMORY) 696 continue; 697 698 start = dev_rle->start; 699 end = dev_rle->end; 700 count = dev_rle->count; 701 type = dev_rle->type; 702 done = FALSE; 703 704 STAILQ_FOREACH(bus_rle, bus_rl, link) { 705 if (bus_rle->type != type) 706 continue; 707 708 /* New resource wholly contained in old, discard. */ 709 if (start >= bus_rle->start && end <= bus_rle->end) 710 break; 711 712 /* New tail overlaps old head, grow existing resource downward. */ 713 if (start < bus_rle->start && end >= bus_rle->start) { 714 bus_rle->count += bus_rle->start - start; 715 bus_rle->start = start; 716 done = TRUE; 717 } 718 719 /* New head overlaps old tail, grow existing resource upward. */ 720 if (start <= bus_rle->end && end > bus_rle->end) { 721 bus_rle->count += end - bus_rle->end; 722 bus_rle->end = end; 723 done = TRUE; 724 } 725 726 /* If we adjusted the old resource, we're finished. */ 727 if (done) 728 break; 729 } 730 731 /* If we didn't merge with anything, add this resource. */ 732 if (bus_rle == NULL) 733 bus_set_resource(bus, type, acpi_sysres_rid++, start, count); 734 } 735 736 /* After merging/moving resources to the parent, free the list. */ 737 resource_list_free(dev_rl); 738 739 return (0); 740} 741