2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/kernel.h>
35 #include <sys/limits.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
39 #include <machine/bus.h>
40 #include <machine/resource.h>
43 #include <contrib/dev/acpica/include/acpi.h>
44 #include <contrib/dev/acpica/include/accommon.h>
46 #include <dev/acpica/acpivar.h>
49 #include "acpi_bus_if.h"
52 /* Hooks for the ACPI CA debugging infrastructure */
53 #define _COMPONENT ACPI_BUS
54 ACPI_MODULE_NAME("RESOURCE")
56 struct lookup_irq_request {
57 ACPI_RESOURCE *acpi_res;
68 acpi_lookup_irq_handler(ACPI_RESOURCE *res, void *context)
70 struct lookup_irq_request *req;
72 u_int irqnum, irq, trig, pol;
75 case ACPI_RESOURCE_TYPE_IRQ:
76 irqnum = res->Data.Irq.InterruptCount;
77 irq = res->Data.Irq.Interrupts[0];
78 len = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
79 trig = res->Data.Irq.Triggering;
80 pol = res->Data.Irq.Polarity;
82 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
83 irqnum = res->Data.ExtendedIrq.InterruptCount;
84 irq = res->Data.ExtendedIrq.Interrupts[0];
85 len = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
86 trig = res->Data.ExtendedIrq.Triggering;
87 pol = res->Data.ExtendedIrq.Polarity;
94 req = (struct lookup_irq_request *)context;
96 if (req->counter != req->rid) {
100 KASSERT(irq == req->irq, ("IRQ resources do not match"));
108 if (req->acpi_res != NULL)
109 bcopy(res, req->acpi_res, len);
110 return (AE_CTRL_TERMINATE);
114 acpi_lookup_irq_resource(device_t dev, int rid, struct resource *res,
115 ACPI_RESOURCE *acpi_res)
117 struct lookup_irq_request req;
120 req.acpi_res = acpi_res;
121 req.irq = rman_get_start(res);
126 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
127 acpi_lookup_irq_handler, &req);
128 if (ACPI_SUCCESS(status) && req.found == 0)
129 status = AE_NOT_FOUND;
134 acpi_config_intr(device_t dev, ACPI_RESOURCE *res)
140 case ACPI_RESOURCE_TYPE_IRQ:
141 KASSERT(res->Data.Irq.InterruptCount == 1,
142 ("%s: multiple interrupts", __func__));
143 irq = res->Data.Irq.Interrupts[0];
144 trig = res->Data.Irq.Triggering;
145 pol = res->Data.Irq.Polarity;
147 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
148 KASSERT(res->Data.ExtendedIrq.InterruptCount == 1,
149 ("%s: multiple interrupts", __func__));
150 irq = res->Data.ExtendedIrq.Interrupts[0];
151 trig = res->Data.ExtendedIrq.Triggering;
152 pol = res->Data.ExtendedIrq.Polarity;
155 panic("%s: bad resource type %u", __func__, res->Type);
158 #if defined(__amd64__) || defined(__i386__)
160 * XXX: Certain BIOSes have buggy AML that specify an IRQ that is
161 * edge-sensitive and active-lo. However, edge-sensitive IRQs
162 * should be active-hi. Force IRQs with an ISA IRQ value to be
165 if (irq < 16 && trig == ACPI_EDGE_SENSITIVE && pol == ACPI_ACTIVE_LOW)
166 pol = ACPI_ACTIVE_HIGH;
168 BUS_CONFIG_INTR(dev, irq, (trig == ACPI_EDGE_SENSITIVE) ?
169 INTR_TRIGGER_EDGE : INTR_TRIGGER_LEVEL, (pol == ACPI_ACTIVE_HIGH) ?
170 INTR_POLARITY_HIGH : INTR_POLARITY_LOW);
175 acpi_map_intr(device_t dev, u_int irq, ACPI_HANDLE handle)
177 struct lookup_irq_request req;
180 trig = ACPI_LEVEL_SENSITIVE;
181 pol = ACPI_ACTIVE_HIGH;
182 if (handle != NULL) {
189 AcpiWalkResources(handle, "_CRS", acpi_lookup_irq_handler, &req);
190 if (req.found != 0) {
195 return ACPI_BUS_MAP_INTR(device_get_parent(dev), dev, irq,
196 (trig == ACPI_EDGE_SENSITIVE) ? INTR_TRIGGER_EDGE : INTR_TRIGGER_LEVEL,
197 (pol == ACPI_ACTIVE_HIGH) ? INTR_POLARITY_HIGH : INTR_POLARITY_LOW);
201 struct acpi_resource_context {
202 struct acpi_parse_resource_set *set;
205 bool ignore_producer_flag;
208 #ifdef ACPI_DEBUG_OUTPUT
210 acpi_address_range_name(UINT8 ResourceType)
214 switch (ResourceType) {
215 case ACPI_MEMORY_RANGE:
219 case ACPI_BUS_NUMBER_RANGE:
220 return ("Bus Number");
222 snprintf(buf, sizeof(buf), "type %u", ResourceType);
229 acpi_parse_resource(ACPI_RESOURCE *res, void *context)
231 struct acpi_parse_resource_set *set;
232 struct acpi_resource_context *arc;
233 UINT64 min, max, length, gran;
244 case ACPI_RESOURCE_TYPE_END_TAG:
245 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "EndTag\n"));
247 case ACPI_RESOURCE_TYPE_FIXED_IO:
248 if (res->Data.FixedIo.AddressLength <= 0)
250 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "FixedIo 0x%x/%d\n",
251 res->Data.FixedIo.Address, res->Data.FixedIo.AddressLength));
252 set->set_ioport(dev, arc->context, res->Data.FixedIo.Address,
253 res->Data.FixedIo.AddressLength);
255 case ACPI_RESOURCE_TYPE_IO:
256 if (res->Data.Io.AddressLength <= 0)
258 if (res->Data.Io.Minimum == res->Data.Io.Maximum) {
259 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Io 0x%x/%d\n",
260 res->Data.Io.Minimum, res->Data.Io.AddressLength));
261 set->set_ioport(dev, arc->context, res->Data.Io.Minimum,
262 res->Data.Io.AddressLength);
264 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Io 0x%x-0x%x/%d\n",
265 res->Data.Io.Minimum, res->Data.Io.Maximum,
266 res->Data.Io.AddressLength));
267 set->set_iorange(dev, arc->context, res->Data.Io.Minimum,
268 res->Data.Io.Maximum, res->Data.Io.AddressLength,
269 res->Data.Io.Alignment);
272 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
273 if (res->Data.FixedMemory32.AddressLength <= 0)
275 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "FixedMemory32 0x%x/%d\n",
276 res->Data.FixedMemory32.Address,
277 res->Data.FixedMemory32.AddressLength));
278 set->set_memory(dev, arc->context, res->Data.FixedMemory32.Address,
279 res->Data.FixedMemory32.AddressLength);
281 case ACPI_RESOURCE_TYPE_MEMORY32:
282 if (res->Data.Memory32.AddressLength <= 0)
284 if (res->Data.Memory32.Minimum == res->Data.Memory32.Maximum) {
285 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory32 0x%x/%d\n",
286 res->Data.Memory32.Minimum, res->Data.Memory32.AddressLength));
287 set->set_memory(dev, arc->context, res->Data.Memory32.Minimum,
288 res->Data.Memory32.AddressLength);
290 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory32 0x%x-0x%x/%d\n",
291 res->Data.Memory32.Minimum, res->Data.Memory32.Maximum,
292 res->Data.Memory32.AddressLength));
293 set->set_memoryrange(dev, arc->context, res->Data.Memory32.Minimum,
294 res->Data.Memory32.Maximum, res->Data.Memory32.AddressLength,
295 res->Data.Memory32.Alignment);
298 case ACPI_RESOURCE_TYPE_MEMORY24:
299 if (res->Data.Memory24.AddressLength <= 0)
301 if (res->Data.Memory24.Minimum == res->Data.Memory24.Maximum) {
302 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory24 0x%x/%d\n",
303 res->Data.Memory24.Minimum, res->Data.Memory24.AddressLength));
304 set->set_memory(dev, arc->context, res->Data.Memory24.Minimum,
305 res->Data.Memory24.AddressLength);
307 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory24 0x%x-0x%x/%d\n",
308 res->Data.Memory24.Minimum, res->Data.Memory24.Maximum,
309 res->Data.Memory24.AddressLength));
310 set->set_memoryrange(dev, arc->context, res->Data.Memory24.Minimum,
311 res->Data.Memory24.Maximum, res->Data.Memory24.AddressLength,
312 res->Data.Memory24.Alignment);
315 case ACPI_RESOURCE_TYPE_IRQ:
318 * "This structure is repeated for each separate interrupt
321 set->set_irq(dev, arc->context, res->Data.Irq.Interrupts,
322 res->Data.Irq.InterruptCount, res->Data.Irq.Triggering,
323 res->Data.Irq.Polarity);
325 case ACPI_RESOURCE_TYPE_DMA:
328 * "This structure is repeated for each separate DMA channel
331 set->set_drq(dev, arc->context, res->Data.Dma.Channels,
332 res->Data.Dma.ChannelCount);
334 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
335 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "start dependent functions\n"));
336 set->set_start_dependent(dev, arc->context,
337 res->Data.StartDpf.CompatibilityPriority);
339 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
340 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "end dependent functions\n"));
341 set->set_end_dependent(dev, arc->context);
343 case ACPI_RESOURCE_TYPE_ADDRESS16:
344 case ACPI_RESOURCE_TYPE_ADDRESS32:
345 case ACPI_RESOURCE_TYPE_ADDRESS64:
346 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
348 case ACPI_RESOURCE_TYPE_ADDRESS16:
349 gran = res->Data.Address16.Address.Granularity;
350 min = res->Data.Address16.Address.Minimum;
351 max = res->Data.Address16.Address.Maximum;
352 length = res->Data.Address16.Address.AddressLength;
357 case ACPI_RESOURCE_TYPE_ADDRESS32:
358 gran = res->Data.Address32.Address.Granularity;
359 min = res->Data.Address32.Address.Minimum;
360 max = res->Data.Address32.Address.Maximum;
361 length = res->Data.Address32.Address.AddressLength;
366 case ACPI_RESOURCE_TYPE_ADDRESS64:
367 gran = res->Data.Address64.Address.Granularity;
368 min = res->Data.Address64.Address.Minimum;
369 max = res->Data.Address64.Address.Maximum;
370 length = res->Data.Address64.Address.AddressLength;
376 KASSERT(res->Type == ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64,
377 ("should never happen"));
378 gran = res->Data.ExtAddress64.Address.Granularity;
379 min = res->Data.ExtAddress64.Address.Minimum;
380 max = res->Data.ExtAddress64.Address.Maximum;
381 length = res->Data.ExtAddress64.Address.AddressLength;
383 name = "ExtAddress64";
389 if (!arc->ignore_producer_flag &&
390 res->Data.Address.ProducerConsumer != ACPI_CONSUMER) {
391 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
392 "ignored %s %s producer\n", name,
393 acpi_address_range_name(res->Data.Address.ResourceType)));
396 if (res->Data.Address.ResourceType != ACPI_MEMORY_RANGE &&
397 res->Data.Address.ResourceType != ACPI_IO_RANGE) {
398 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
399 "ignored %s for non-memory, non-I/O\n", name));
404 if (min > ULONG_MAX || (res->Data.Address.MaxAddressFixed && max >
406 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "ignored %s above 4G\n",
413 if (res->Data.Address.MinAddressFixed == ACPI_ADDRESS_FIXED &&
414 res->Data.Address.MaxAddressFixed == ACPI_ADDRESS_FIXED) {
415 if (res->Data.Address.ResourceType == ACPI_MEMORY_RANGE) {
416 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "%s/Memory 0x%jx/%ju\n",
417 name, (uintmax_t)min, (uintmax_t)length));
418 set->set_memory(dev, arc->context, min, length);
420 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "%s/IO 0x%jx/%ju\n", name,
421 (uintmax_t)min, (uintmax_t)length));
422 set->set_ioport(dev, arc->context, min, length);
424 } else if (res->Data.Address.MinAddressFixed != ACPI_ADDRESS_FIXED &&
425 res->Data.Address.MaxAddressFixed != ACPI_ADDRESS_FIXED) {
426 /* Fixed size, variable location resource descriptor */
427 min = roundup(min, gran + 1);
428 if ((min + length - 1) > max) {
430 "invalid memory range: start: %jx end: %jx max: %jx\n",
431 (uintmax_t)min, (uintmax_t)(min + length - 1),
434 if (res->Data.Address.ResourceType == ACPI_MEMORY_RANGE) {
435 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
436 "%s/Memory 0x%jx/%ju\n", name, (uintmax_t)min,
438 set->set_memory(dev, arc->context, min, length);
440 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "%s/IO 0x%jx/%ju\n",
441 name, (uintmax_t)min, (uintmax_t)length));
442 set->set_ioport(dev, arc->context, min, length);
446 if (res->Data.Address32.ResourceType == ACPI_MEMORY_RANGE) {
447 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
448 "%s/Memory 0x%jx-0x%jx/%ju\n", name, (uintmax_t)min,
449 (uintmax_t)max, (uintmax_t)length));
450 set->set_memoryrange(dev, arc->context, min, max, length, gran);
452 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "%s/IO 0x%jx-0x%jx/%ju\n",
453 name, (uintmax_t)min, (uintmax_t)max, (uintmax_t)length));
454 set->set_iorange(dev, arc->context, min, max, length, gran);
458 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
459 if (res->Data.ExtendedIrq.ProducerConsumer != ACPI_CONSUMER) {
460 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "ignored ExtIRQ producer\n"));
463 set->set_ext_irq(dev, arc->context, res->Data.ExtendedIrq.Interrupts,
464 res->Data.ExtendedIrq.InterruptCount,
465 res->Data.ExtendedIrq.Triggering, res->Data.ExtendedIrq.Polarity);
467 case ACPI_RESOURCE_TYPE_VENDOR:
468 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
469 "unimplemented VendorSpecific resource\n"));
478 * Fetch a device's resources and associate them with the device.
480 * Note that it might be nice to also locate ACPI-specific resource items, such
483 * We really need to split the resource-fetching code out from the
484 * resource-parsing code, since we may want to use the parsing
485 * code for _PRS someday.
488 acpi_parse_resources(device_t dev, ACPI_HANDLE handle,
489 struct acpi_parse_resource_set *set, void *arg)
491 struct acpi_resource_context arc;
494 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
496 set->set_init(dev, arg, &arc.context);
499 arc.ignore_producer_flag = false;
502 * UARTs on ThunderX2 set ResourceProducer on memory resources, with
505 if (acpi_MatchHid(handle, "ARMH0011") != ACPI_MATCHHID_NOMATCH)
506 arc.ignore_producer_flag = true;
509 * ARM Coresight on N1SDP set ResourceProducer on memory resources.
510 * Coresight devices: ETM, STM, TPIU, ETF/ETR, REP, FUN.
512 if (acpi_MatchHid(handle, "ARMHC500") != ACPI_MATCHHID_NOMATCH ||
513 acpi_MatchHid(handle, "ARMHC502") != ACPI_MATCHHID_NOMATCH ||
514 acpi_MatchHid(handle, "ARMHC600") != ACPI_MATCHHID_NOMATCH ||
515 acpi_MatchHid(handle, "ARMHC979") != ACPI_MATCHHID_NOMATCH ||
516 acpi_MatchHid(handle, "ARMHC97C") != ACPI_MATCHHID_NOMATCH ||
517 acpi_MatchHid(handle, "ARMHC98D") != ACPI_MATCHHID_NOMATCH ||
518 acpi_MatchHid(handle, "ARMHC9FF") != ACPI_MATCHHID_NOMATCH ||
519 acpi_MatchHid(handle, "ARMHD620") != ACPI_MATCHHID_NOMATCH)
520 arc.ignore_producer_flag = true;
522 status = AcpiWalkResources(handle, "_CRS", acpi_parse_resource, &arc);
523 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
524 printf("can't fetch resources for %s - %s\n",
525 acpi_name(handle), AcpiFormatException(status));
526 return_ACPI_STATUS (status);
528 set->set_done(dev, arc.context);
529 return_ACPI_STATUS (AE_OK);
533 * Resource-set vectors used to attach _CRS-derived resources
536 static void acpi_res_set_init(device_t dev, void *arg, void **context);
537 static void acpi_res_set_done(device_t dev, void *context);
538 static void acpi_res_set_ioport(device_t dev, void *context,
539 uint64_t base, uint64_t length);
540 static void acpi_res_set_iorange(device_t dev, void *context,
541 uint64_t low, uint64_t high,
542 uint64_t length, uint64_t align);
543 static void acpi_res_set_memory(device_t dev, void *context,
544 uint64_t base, uint64_t length);
545 static void acpi_res_set_memoryrange(device_t dev, void *context,
546 uint64_t low, uint64_t high,
547 uint64_t length, uint64_t align);
548 static void acpi_res_set_irq(device_t dev, void *context, uint8_t *irq,
549 int count, int trig, int pol);
550 static void acpi_res_set_ext_irq(device_t dev, void *context,
551 uint32_t *irq, int count, int trig, int pol);
552 static void acpi_res_set_drq(device_t dev, void *context, uint8_t *drq,
554 static void acpi_res_set_start_dependent(device_t dev, void *context,
556 static void acpi_res_set_end_dependent(device_t dev, void *context);
558 struct acpi_parse_resource_set acpi_res_parse_set = {
562 acpi_res_set_iorange,
564 acpi_res_set_memoryrange,
566 acpi_res_set_ext_irq,
568 acpi_res_set_start_dependent,
569 acpi_res_set_end_dependent
572 struct acpi_res_context {
581 acpi_res_set_init(device_t dev, void *arg, void **context)
583 struct acpi_res_context *cp;
585 if ((cp = AcpiOsAllocate(sizeof(*cp))) != NULL) {
586 bzero(cp, sizeof(*cp));
593 acpi_res_set_done(device_t dev, void *context)
595 struct acpi_res_context *cp = (struct acpi_res_context *)context;
603 acpi_res_set_ioport(device_t dev, void *context, uint64_t base,
606 struct acpi_res_context *cp = (struct acpi_res_context *)context;
610 bus_set_resource(dev, SYS_RES_IOPORT, cp->ar_nio++, base, length);
614 acpi_res_set_iorange(device_t dev, void *context, uint64_t low,
615 uint64_t high, uint64_t length, uint64_t align)
617 struct acpi_res_context *cp = (struct acpi_res_context *)context;
623 * XXX: Some BIOSes contain buggy _CRS entries where fixed I/O
624 * ranges have the maximum base address (_MAX) to the end of the
625 * I/O range instead of the start. These are then treated as a
626 * relocatable I/O range rather than a fixed I/O resource. As a
627 * workaround, treat I/O resources encoded this way as fixed I/O
630 if (high == (low + length)) {
633 "_CRS has fixed I/O port range defined as relocatable\n");
635 bus_set_resource(dev, SYS_RES_IOPORT, cp->ar_nio++, low, length);
639 device_printf(dev, "I/O range not supported\n");
643 acpi_res_set_memory(device_t dev, void *context, uint64_t base,
646 struct acpi_res_context *cp = (struct acpi_res_context *)context;
650 bus_set_resource(dev, SYS_RES_MEMORY, cp->ar_nmem++, base, length);
654 acpi_res_set_memoryrange(device_t dev, void *context, uint64_t low,
655 uint64_t high, uint64_t length, uint64_t align)
657 struct acpi_res_context *cp = (struct acpi_res_context *)context;
661 device_printf(dev, "memory range not supported\n");
665 acpi_res_set_irq(device_t dev, void *context, uint8_t *irq, int count,
668 struct acpi_res_context *cp = (struct acpi_res_context *)context;
671 if (cp == NULL || irq == NULL)
674 for (i = 0; i < count; i++)
675 bus_set_resource(dev, SYS_RES_IRQ, cp->ar_nirq++, irq[i], 1);
679 acpi_res_set_ext_irq(device_t dev, void *context, uint32_t *irq, int count,
682 struct acpi_res_context *cp = (struct acpi_res_context *)context;
685 if (cp == NULL || irq == NULL)
688 for (i = 0; i < count; i++)
689 bus_set_resource(dev, SYS_RES_IRQ, cp->ar_nirq++, irq[i], 1);
693 acpi_res_set_drq(device_t dev, void *context, uint8_t *drq, int count)
695 struct acpi_res_context *cp = (struct acpi_res_context *)context;
697 if (cp == NULL || drq == NULL)
700 /* This implements no resource relocation. */
704 bus_set_resource(dev, SYS_RES_DRQ, cp->ar_ndrq++, *drq, 1);
708 acpi_res_set_start_dependent(device_t dev, void *context, int preference)
710 struct acpi_res_context *cp = (struct acpi_res_context *)context;
714 device_printf(dev, "dependent functions not supported\n");
718 acpi_res_set_end_dependent(device_t dev, void *context)
720 struct acpi_res_context *cp = (struct acpi_res_context *)context;
724 device_printf(dev, "dependent functions not supported\n");
728 * Resource-owning placeholders for IO and memory pseudo-devices.
730 * This code allocates system resources that will be used by ACPI
731 * child devices. The acpi parent manages these resources through a
735 static int acpi_sysres_rid = 100;
737 static int acpi_sysres_probe(device_t dev);
738 static int acpi_sysres_attach(device_t dev);
740 static device_method_t acpi_sysres_methods[] = {
741 /* Device interface */
742 DEVMETHOD(device_probe, acpi_sysres_probe),
743 DEVMETHOD(device_attach, acpi_sysres_attach),
748 static driver_t acpi_sysres_driver = {
754 static devclass_t acpi_sysres_devclass;
755 DRIVER_MODULE(acpi_sysresource, acpi, acpi_sysres_driver, acpi_sysres_devclass,
757 MODULE_DEPEND(acpi_sysresource, acpi, 1, 1, 1);
760 acpi_sysres_probe(device_t dev)
762 static char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL };
765 if (acpi_disabled("sysresource"))
767 rv = ACPI_ID_PROBE(device_get_parent(dev), dev, sysres_ids, NULL);
771 device_set_desc(dev, "System Resource");
777 acpi_sysres_attach(device_t dev)
780 struct resource_list_entry *bus_rle, *dev_rle;
781 struct resource_list *bus_rl, *dev_rl;
783 rman_res_t start, end, count;
786 * Loop through all current resources to see if the new one overlaps
787 * any existing ones. If so, grow the old one up and/or down
788 * accordingly. Discard any that are wholly contained in the old. If
789 * the resource is unique, add it to the parent. It will later go into
792 bus = device_get_parent(dev);
793 dev_rl = BUS_GET_RESOURCE_LIST(bus, dev);
794 bus_rl = BUS_GET_RESOURCE_LIST(device_get_parent(bus), bus);
795 STAILQ_FOREACH(dev_rle, dev_rl, link) {
796 if (dev_rle->type != SYS_RES_IOPORT && dev_rle->type != SYS_RES_MEMORY)
799 start = dev_rle->start;
801 count = dev_rle->count;
802 type = dev_rle->type;
805 STAILQ_FOREACH(bus_rle, bus_rl, link) {
806 if (bus_rle->type != type)
809 /* New resource wholly contained in old, discard. */
810 if (start >= bus_rle->start && end <= bus_rle->end)
813 /* New tail overlaps old head, grow existing resource downward. */
814 if (start < bus_rle->start && end >= bus_rle->start) {
815 bus_rle->count += bus_rle->start - start;
816 bus_rle->start = start;
820 /* New head overlaps old tail, grow existing resource upward. */
821 if (start <= bus_rle->end && end > bus_rle->end) {
822 bus_rle->count += end - bus_rle->end;
827 /* If we adjusted the old resource, we're finished. */
832 /* If we didn't merge with anything, add this resource. */
834 bus_set_resource(bus, type, acpi_sysres_rid++, start, count);
837 /* After merging/moving resources to the parent, free the list. */
838 resource_list_free(dev_rl);