kernel - Fix bugs in recent RSS/swap commits
[dragonfly.git] / sys / dev / acpica / acpi_resource.c
blobc9b94cabd33963f93e5aaf79128f68f657950b76
1 /*-
2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
25 * SUCH DAMAGE.
27 * $FreeBSD: head/sys/dev/acpica/acpi_resource.c 263975 2014-03-31 19:37:39Z imp $
30 #include "opt_acpi.h"
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/bus.h>
34 #include <sys/limits.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/resource.h>
38 #include <sys/machintr.h>
40 #include <sys/rman.h>
42 #include "acpi.h"
43 #include <dev/acpica/acpivar.h>
45 /* Hooks for the ACPICA debugging infrastructure */
46 #define _COMPONENT ACPI_BUS
47 ACPI_MODULE_NAME("RESOURCE")
49 struct lookup_irq_request {
50 ACPI_RESOURCE *acpi_res;
51 struct resource *res;
52 int counter;
53 int rid;
54 int found;
57 static ACPI_STATUS
58 acpi_lookup_irq_handler(ACPI_RESOURCE *res, void *context)
60 struct lookup_irq_request *req;
61 size_t len;
62 u_int irqnum;
63 u_int irq __debugvar;
65 switch (res->Type) {
66 case ACPI_RESOURCE_TYPE_IRQ:
67 irqnum = res->Data.Irq.InterruptCount;
68 irq = res->Data.Irq.Interrupts[0];
69 len = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
70 break;
71 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
72 irqnum = res->Data.ExtendedIrq.InterruptCount;
73 irq = res->Data.ExtendedIrq.Interrupts[0];
74 len = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
75 break;
76 default:
77 return (AE_OK);
79 if (irqnum != 1)
80 return (AE_OK);
81 req = (struct lookup_irq_request *)context;
82 if (req->counter != req->rid) {
83 req->counter++;
84 return (AE_OK);
86 req->found = 1;
87 KASSERT(irq == rman_get_start(req->res),
88 ("IRQ resources do not match"));
89 bcopy(res, req->acpi_res, len);
90 return (AE_CTRL_TERMINATE);
93 ACPI_STATUS
94 acpi_lookup_irq_resource(device_t dev, int rid, struct resource *res,
95 ACPI_RESOURCE *acpi_res)
97 struct lookup_irq_request req;
98 ACPI_STATUS status;
100 req.acpi_res = acpi_res;
101 req.res = res;
102 req.counter = 0;
103 req.rid = rid;
104 req.found = 0;
105 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
106 acpi_lookup_irq_handler, &req);
107 if (ACPI_SUCCESS(status) && req.found == 0)
108 status = AE_NOT_FOUND;
109 return (status);
112 void
113 acpi_config_intr(device_t dev, ACPI_RESOURCE *res)
115 u_int irq;
116 int pol, trig;
117 enum intr_trigger trigger;
118 enum intr_polarity polarity;
120 switch (res->Type) {
121 case ACPI_RESOURCE_TYPE_IRQ:
122 KASSERT(res->Data.Irq.InterruptCount == 1,
123 ("%s: multiple interrupts", __func__));
124 irq = res->Data.Irq.Interrupts[0];
125 trig = res->Data.Irq.Triggering;
126 pol = res->Data.Irq.Polarity;
127 break;
128 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
129 KASSERT(res->Data.ExtendedIrq.InterruptCount == 1,
130 ("%s: multiple interrupts", __func__));
131 irq = res->Data.ExtendedIrq.Interrupts[0];
132 trig = res->Data.ExtendedIrq.Triggering;
133 pol = res->Data.ExtendedIrq.Polarity;
134 break;
135 default:
136 panic("%s: bad resource type %u", __func__, res->Type);
139 #if defined(__x86_64__)
141 * XXX: Certain BIOSes have buggy AML that specify an IRQ that is
142 * edge-sensitive and active-lo. However, edge-sensitive IRQs
143 * should be active-hi. Force IRQs with an ISA IRQ value to be
144 * active-hi instead.
146 if (irq < 16 && trig == ACPI_EDGE_SENSITIVE && pol == ACPI_ACTIVE_LOW)
147 pol = ACPI_ACTIVE_HIGH;
148 #endif
150 if (trig == ACPI_EDGE_SENSITIVE)
151 trigger = INTR_TRIGGER_EDGE;
152 else
153 trigger = INTR_TRIGGER_LEVEL;
155 if (pol == ACPI_ACTIVE_HIGH)
156 polarity = INTR_POLARITY_HIGH;
157 else
158 polarity = INTR_POLARITY_LOW;
160 if (machintr_legacy_intr_find(irq, trigger, polarity) < 0)
161 kprintf("%s: Skip irq %d config\n", __func__, irq);
162 else
163 BUS_CONFIG_INTR(dev, dev, irq, trigger, polarity);
166 struct acpi_resource_context {
167 struct acpi_parse_resource_set *set;
168 device_t dev;
169 void *context;
172 #ifdef ACPI_DEBUG_OUTPUT
173 static const char *
174 acpi_address_range_name(UINT8 ResourceType)
176 static char buf[16];
178 switch (ResourceType) {
179 case ACPI_MEMORY_RANGE:
180 return ("Memory");
181 case ACPI_IO_RANGE:
182 return ("IO");
183 case ACPI_BUS_NUMBER_RANGE:
184 return ("Bus Number");
185 default:
186 ksnprintf(buf, sizeof(buf), "type %u", ResourceType);
187 return (buf);
190 #endif
192 static ACPI_STATUS
193 acpi_parse_resource(ACPI_RESOURCE *res, void *context)
195 struct acpi_parse_resource_set *set;
196 struct acpi_resource_context *arc;
197 UINT64 min, max, length, gran;
198 #ifdef ACPI_DEBUG
199 const char *name;
200 #endif
201 device_t dev;
203 arc = context;
204 dev = arc->dev;
205 set = arc->set;
207 switch (res->Type) {
208 case ACPI_RESOURCE_TYPE_END_TAG:
209 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "EndTag\n"));
210 break;
211 case ACPI_RESOURCE_TYPE_FIXED_IO:
212 if (res->Data.FixedIo.AddressLength <= 0)
213 break;
214 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "FixedIo 0x%x/%d\n",
215 res->Data.FixedIo.Address, res->Data.FixedIo.AddressLength));
216 set->set_ioport(dev, arc->context, res->Data.FixedIo.Address,
217 res->Data.FixedIo.AddressLength);
218 break;
219 case ACPI_RESOURCE_TYPE_IO:
220 if (res->Data.Io.AddressLength <= 0)
221 break;
222 if (res->Data.Io.Minimum == res->Data.Io.Maximum) {
223 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Io 0x%x/%d\n",
224 res->Data.Io.Minimum, res->Data.Io.AddressLength));
225 set->set_ioport(dev, arc->context, res->Data.Io.Minimum,
226 res->Data.Io.AddressLength);
227 } else {
228 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Io 0x%x-0x%x/%d\n",
229 res->Data.Io.Minimum, res->Data.Io.Maximum,
230 res->Data.Io.AddressLength));
231 set->set_iorange(dev, arc->context, res->Data.Io.Minimum,
232 res->Data.Io.Maximum, res->Data.Io.AddressLength,
233 res->Data.Io.Alignment);
235 break;
236 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
237 if (res->Data.FixedMemory32.AddressLength <= 0)
238 break;
239 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "FixedMemory32 0x%x/%d\n",
240 res->Data.FixedMemory32.Address,
241 res->Data.FixedMemory32.AddressLength));
242 set->set_memory(dev, arc->context, res->Data.FixedMemory32.Address,
243 res->Data.FixedMemory32.AddressLength);
244 break;
245 case ACPI_RESOURCE_TYPE_MEMORY32:
246 if (res->Data.Memory32.AddressLength <= 0)
247 break;
248 if (res->Data.Memory32.Minimum == res->Data.Memory32.Maximum) {
249 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory32 0x%x/%d\n",
250 res->Data.Memory32.Minimum, res->Data.Memory32.AddressLength));
251 set->set_memory(dev, arc->context, res->Data.Memory32.Minimum,
252 res->Data.Memory32.AddressLength);
253 } else {
254 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory32 0x%x-0x%x/%d\n",
255 res->Data.Memory32.Minimum, res->Data.Memory32.Maximum,
256 res->Data.Memory32.AddressLength));
257 set->set_memoryrange(dev, arc->context, res->Data.Memory32.Minimum,
258 res->Data.Memory32.Maximum, res->Data.Memory32.AddressLength,
259 res->Data.Memory32.Alignment);
261 break;
262 case ACPI_RESOURCE_TYPE_MEMORY24:
263 if (res->Data.Memory24.AddressLength <= 0)
264 break;
265 if (res->Data.Memory24.Minimum == res->Data.Memory24.Maximum) {
266 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory24 0x%x/%d\n",
267 res->Data.Memory24.Minimum, res->Data.Memory24.AddressLength));
268 set->set_memory(dev, arc->context, res->Data.Memory24.Minimum,
269 res->Data.Memory24.AddressLength);
270 } else {
271 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory24 0x%x-0x%x/%d\n",
272 res->Data.Memory24.Minimum, res->Data.Memory24.Maximum,
273 res->Data.Memory24.AddressLength));
274 set->set_memoryrange(dev, arc->context, res->Data.Memory24.Minimum,
275 res->Data.Memory24.Maximum, res->Data.Memory24.AddressLength,
276 res->Data.Memory24.Alignment);
278 break;
279 case ACPI_RESOURCE_TYPE_IRQ:
281 * from 1.0b 6.4.2
282 * "This structure is repeated for each separate interrupt
283 * required"
285 set->set_irq(dev, arc->context, res->Data.Irq.Interrupts,
286 res->Data.Irq.InterruptCount, res->Data.Irq.Triggering,
287 res->Data.Irq.Polarity);
288 break;
289 case ACPI_RESOURCE_TYPE_DMA:
291 * from 1.0b 6.4.3
292 * "This structure is repeated for each separate DMA channel
293 * required"
295 set->set_drq(dev, arc->context, res->Data.Dma.Channels,
296 res->Data.Dma.ChannelCount);
297 break;
298 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
299 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "start dependent functions\n"));
300 set->set_start_dependent(dev, arc->context,
301 res->Data.StartDpf.CompatibilityPriority);
302 break;
303 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
304 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "end dependent functions\n"));
305 set->set_end_dependent(dev, arc->context);
306 break;
307 case ACPI_RESOURCE_TYPE_ADDRESS16:
308 case ACPI_RESOURCE_TYPE_ADDRESS32:
309 case ACPI_RESOURCE_TYPE_ADDRESS64:
310 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
311 switch (res->Type) {
312 case ACPI_RESOURCE_TYPE_ADDRESS16:
313 gran = res->Data.Address16.Address.Granularity;
314 min = res->Data.Address16.Address.Minimum;
315 max = res->Data.Address16.Address.Maximum;
316 length = res->Data.Address16.Address.AddressLength;
317 #ifdef ACPI_DEBUG
318 name = "Address16";
319 #endif
320 break;
321 case ACPI_RESOURCE_TYPE_ADDRESS32:
322 gran = res->Data.Address32.Address.Granularity;
323 min = res->Data.Address32.Address.Minimum;
324 max = res->Data.Address32.Address.Maximum;
325 length = res->Data.Address32.Address.AddressLength;
326 #ifdef ACPI_DEBUG
327 name = "Address32";
328 #endif
329 break;
330 case ACPI_RESOURCE_TYPE_ADDRESS64:
331 gran = res->Data.Address64.Address.Granularity;
332 min = res->Data.Address64.Address.Minimum;
333 max = res->Data.Address64.Address.Maximum;
334 length = res->Data.Address64.Address.AddressLength;
335 #ifdef ACPI_DEBUG
336 name = "Address64";
337 #endif
338 break;
339 default:
340 KASSERT(res->Type == ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64,
341 ("should never happen"));
342 gran = res->Data.ExtAddress64.Address.Granularity;
343 min = res->Data.ExtAddress64.Address.Minimum;
344 max = res->Data.ExtAddress64.Address.Maximum;
345 length = res->Data.ExtAddress64.Address.AddressLength;
346 #ifdef ACPI_DEBUG
347 name = "ExtAddress64";
348 #endif
349 break;
351 if (length <= 0)
352 break;
353 if (res->Data.Address.ProducerConsumer != ACPI_CONSUMER) {
354 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
355 "ignored %s %s producer\n", name,
356 acpi_address_range_name(res->Data.Address.ResourceType)));
357 break;
359 if (res->Data.Address.ResourceType != ACPI_MEMORY_RANGE &&
360 res->Data.Address.ResourceType != ACPI_IO_RANGE) {
361 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
362 "ignored %s for non-memory, non-I/O\n", name));
363 break;
365 if (res->Data.Address.MinAddressFixed == ACPI_ADDRESS_FIXED &&
366 res->Data.Address.MaxAddressFixed == ACPI_ADDRESS_FIXED) {
367 if (res->Data.Address.ResourceType == ACPI_MEMORY_RANGE) {
368 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "%s/Memory 0x%jx/%ju\n",
369 name, (uintmax_t)min, (uintmax_t)length));
370 set->set_memory(dev, arc->context, min, length);
371 } else {
372 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "%s/IO 0x%jx/%ju\n", name,
373 (uintmax_t)min, (uintmax_t)length));
374 set->set_ioport(dev, arc->context, min, length);
376 } else {
377 if (res->Data.Address32.ResourceType == ACPI_MEMORY_RANGE) {
378 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
379 "%s/Memory 0x%jx-0x%jx/%ju\n", name, (uintmax_t)min,
380 (uintmax_t)max, (uintmax_t)length));
381 set->set_memoryrange(dev, arc->context, min, max, length, gran);
382 } else {
383 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "%s/IO 0x%jx-0x%jx/%ju\n",
384 name, (uintmax_t)min, (uintmax_t)max, (uintmax_t)length));
385 set->set_iorange(dev, arc->context, min, max, length, gran);
388 break;
389 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
390 if (res->Data.ExtendedIrq.ProducerConsumer != ACPI_CONSUMER) {
391 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "ignored ExtIRQ producer\n"));
392 break;
394 set->set_ext_irq(dev, arc->context, res->Data.ExtendedIrq.Interrupts,
395 res->Data.ExtendedIrq.InterruptCount,
396 res->Data.ExtendedIrq.Triggering, res->Data.ExtendedIrq.Polarity);
397 break;
398 case ACPI_RESOURCE_TYPE_VENDOR:
399 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
400 "unimplemented VendorSpecific resource\n"));
401 break;
402 default:
403 break;
405 return (AE_OK);
409 * Fetch a device's resources and associate them with the device.
411 * Note that it might be nice to also locate ACPI-specific resource items, such
412 * as GPE bits.
414 * We really need to split the resource-fetching code out from the
415 * resource-parsing code, since we may want to use the parsing
416 * code for _PRS someday.
418 ACPI_STATUS
419 acpi_parse_resources(device_t dev, ACPI_HANDLE handle,
420 struct acpi_parse_resource_set *set, void *arg)
422 struct acpi_resource_context arc;
423 ACPI_STATUS status;
425 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
427 set->set_init(dev, arg, &arc.context);
428 arc.set = set;
429 arc.dev = dev;
430 status = AcpiWalkResources(handle, "_CRS", acpi_parse_resource, &arc);
431 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
432 kprintf("can't fetch resources for %s - %s\n",
433 acpi_name(handle), AcpiFormatException(status));
434 return_ACPI_STATUS (status);
436 set->set_done(dev, arc.context);
437 return_ACPI_STATUS (AE_OK);
441 * Resource-set vectors used to attach _CRS-derived resources
442 * to an ACPI device.
444 static void acpi_res_set_init(device_t dev, void *arg, void **context);
445 static void acpi_res_set_done(device_t dev, void *context);
446 static void acpi_res_set_ioport(device_t dev, void *context,
447 uint64_t base, uint64_t length);
448 static void acpi_res_set_iorange(device_t dev, void *context,
449 uint64_t low, uint64_t high,
450 uint64_t length, uint64_t align);
451 static void acpi_res_set_memory(device_t dev, void *context,
452 uint64_t base, uint64_t length);
453 static void acpi_res_set_memoryrange(device_t dev, void *context,
454 uint64_t low, uint64_t high,
455 uint64_t length, uint64_t align);
456 static void acpi_res_set_irq(device_t dev, void *context, uint8_t *irq,
457 int count, int trig, int pol);
458 static void acpi_res_set_ext_irq(device_t dev, void *context,
459 uint32_t *irq, int count, int trig, int pol);
460 static void acpi_res_set_drq(device_t dev, void *context, uint8_t *drq,
461 int count);
462 static void acpi_res_set_start_dependent(device_t dev, void *context,
463 int preference);
464 static void acpi_res_set_end_dependent(device_t dev, void *context);
466 struct acpi_parse_resource_set acpi_res_parse_set = {
467 acpi_res_set_init,
468 acpi_res_set_done,
469 acpi_res_set_ioport,
470 acpi_res_set_iorange,
471 acpi_res_set_memory,
472 acpi_res_set_memoryrange,
473 acpi_res_set_irq,
474 acpi_res_set_ext_irq,
475 acpi_res_set_drq,
476 acpi_res_set_start_dependent,
477 acpi_res_set_end_dependent
480 struct acpi_res_context {
481 int ar_nio;
482 int ar_nmem;
483 int ar_nirq;
484 int ar_ndrq;
485 void *ar_parent;
488 static void
489 acpi_res_set_init(device_t dev, void *arg, void **context)
491 struct acpi_res_context *cp;
493 if ((cp = AcpiOsAllocate(sizeof(*cp))) != NULL) {
494 bzero(cp, sizeof(*cp));
495 cp->ar_parent = arg;
496 *context = cp;
500 static void
501 acpi_res_set_done(device_t dev, void *context)
503 struct acpi_res_context *cp = (struct acpi_res_context *)context;
505 if (cp == NULL)
506 return;
507 AcpiOsFree(cp);
510 static void
511 acpi_res_set_ioport(device_t dev, void *context, uint64_t base,
512 uint64_t length)
514 struct acpi_res_context *cp = (struct acpi_res_context *)context;
516 if (cp == NULL)
517 return;
518 bus_set_resource(dev, SYS_RES_IOPORT, cp->ar_nio++, base, length, -1);
521 static void
522 acpi_res_set_iorange(device_t dev, void *context, uint64_t low,
523 uint64_t high, uint64_t length, uint64_t align)
525 struct acpi_res_context *cp = (struct acpi_res_context *)context;
526 uint64_t base;
528 if (cp == NULL)
529 return;
531 base = roundup(low, align);
532 if (base + length - 1 <= high) {
533 acpi_res_set_ioport(dev, context, base, length);
534 return;
536 device_printf(dev, "I/O range [0x%jx,0x%jx,0x%jx,0x%jx] not supported\n",
537 (uintmax_t)low, (uintmax_t)high, (uintmax_t)length, (uintmax_t)align);
540 static void
541 acpi_res_set_memory(device_t dev, void *context, uint64_t base,
542 uint64_t length)
544 struct acpi_res_context *cp = (struct acpi_res_context *)context;
546 if (cp == NULL)
547 return;
549 bus_set_resource(dev, SYS_RES_MEMORY, cp->ar_nmem++, base, length, -1);
552 static void
553 acpi_res_set_memoryrange(device_t dev, void *context, uint64_t low,
554 uint64_t high, uint64_t length, uint64_t align)
556 struct acpi_res_context *cp = (struct acpi_res_context *)context;
558 if (cp == NULL)
559 return;
560 device_printf(dev, "memory range [0x%jx,0x%jx,0x%jx,0x%jx] "
561 "not supported\n",
562 (uintmax_t)low, (uintmax_t)high, (uintmax_t)length, (uintmax_t)align);
565 static void
566 acpi_res_set_irq(device_t dev, void *context, uint8_t *irq, int count,
567 int trig, int pol)
569 struct acpi_res_context *cp = (struct acpi_res_context *)context;
571 if (cp == NULL || irq == NULL)
572 return;
574 /* This implements no resource relocation. */
575 if (count != 1)
576 return;
578 bus_set_resource(dev, SYS_RES_IRQ, cp->ar_nirq++, *irq, 1,
579 machintr_legacy_intr_cpuid(*irq));
582 static void
583 acpi_res_set_ext_irq(device_t dev, void *context, uint32_t *irq, int count,
584 int trig, int pol)
586 struct acpi_res_context *cp = (struct acpi_res_context *)context;
588 if (cp == NULL || irq == NULL)
589 return;
591 /* This implements no resource relocation. */
592 if (count != 1)
593 return;
595 /* There is no such IRQ at all */
596 if (machintr_legacy_intr_find(*irq,
597 INTR_TRIGGER_CONFORM, INTR_POLARITY_CONFORM) < 0)
598 return;
600 bus_set_resource(dev, SYS_RES_IRQ, cp->ar_nirq++, *irq, 1,
601 machintr_legacy_intr_cpuid(*irq));
604 static void
605 acpi_res_set_drq(device_t dev, void *context, uint8_t *drq, int count)
607 struct acpi_res_context *cp = (struct acpi_res_context *)context;
609 if (cp == NULL || drq == NULL)
610 return;
612 /* This implements no resource relocation. */
613 if (count != 1)
614 return;
616 bus_set_resource(dev, SYS_RES_DRQ, cp->ar_ndrq++, *drq, 1, -1);
619 static void
620 acpi_res_set_start_dependent(device_t dev, void *context, int preference)
622 struct acpi_res_context *cp = (struct acpi_res_context *)context;
624 if (cp == NULL)
625 return;
626 device_printf(dev, "dependent functions not supported\n");
629 static void
630 acpi_res_set_end_dependent(device_t dev, void *context)
632 struct acpi_res_context *cp = (struct acpi_res_context *)context;
634 if (cp == NULL)
635 return;
636 device_printf(dev, "dependent functions not supported\n");
640 * Resource-owning placeholders for IO and memory pseudo-devices.
642 * This code allocates system resources that will be used by ACPI
643 * child devices. The acpi parent manages these resources through a
644 * private rman.
647 static int acpi_sysres_rid = 100;
649 static int acpi_sysres_probe(device_t dev);
650 static int acpi_sysres_attach(device_t dev);
652 static device_method_t acpi_sysres_methods[] = {
653 /* Device interface */
654 DEVMETHOD(device_probe, acpi_sysres_probe),
655 DEVMETHOD(device_attach, acpi_sysres_attach),
657 DEVMETHOD_END
660 static driver_t acpi_sysres_driver = {
661 "acpi_sysresource",
662 acpi_sysres_methods,
666 static devclass_t acpi_sysres_devclass;
667 DRIVER_MODULE(acpi_sysresource, acpi, acpi_sysres_driver, acpi_sysres_devclass,
668 NULL, NULL);
669 MODULE_DEPEND(acpi_sysresource, acpi, 1, 1, 1);
671 static int
672 acpi_sysres_probe(device_t dev)
674 static char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL };
676 if (acpi_disabled("sysresource") ||
677 ACPI_ID_PROBE(device_get_parent(dev), dev, sysres_ids) == NULL)
678 return (ENXIO);
680 device_set_desc(dev, "System Resource");
681 device_quiet(dev);
682 return (BUS_PROBE_DEFAULT);
685 static int
686 acpi_sysres_attach(device_t dev)
688 device_t bus;
689 struct resource_list_entry *bus_rle, *dev_rle;
690 struct resource_list *bus_rl, *dev_rl;
691 int done, type;
692 u_long start, end, count;
695 * Loop through all current resources to see if the new one overlaps
696 * any existing ones. If so, grow the old one up and/or down
697 * accordingly. Discard any that are wholly contained in the old. If
698 * the resource is unique, add it to the parent. It will later go into
699 * the rman pool.
701 bus = device_get_parent(dev);
702 dev_rl = BUS_GET_RESOURCE_LIST(bus, dev);
703 bus_rl = BUS_GET_RESOURCE_LIST(device_get_parent(bus), bus);
704 if (bus_rl)
705 kprintf("busrl is not null!\n");
706 SLIST_FOREACH(dev_rle, dev_rl, link) {
707 if (dev_rle->type != SYS_RES_IOPORT && dev_rle->type != SYS_RES_MEMORY)
708 continue;
710 start = dev_rle->start;
711 end = dev_rle->end;
712 count = dev_rle->count;
713 type = dev_rle->type;
714 done = FALSE;
715 if (bus_rl) {
716 SLIST_FOREACH(bus_rle, bus_rl, link) {
717 if (bus_rle->type != type)
718 continue;
720 /* New resource wholly contained in old, discard. */
721 if (start >= bus_rle->start && end <= bus_rle->end)
722 break;
724 /* New tail overlaps old head, grow existing resource downward. */
725 if (start < bus_rle->start && end >= bus_rle->start) {
726 bus_rle->count += bus_rle->start - start;
727 bus_rle->start = start;
728 done = TRUE;
731 /* New head overlaps old tail, grow existing resource upward. */
732 if (start <= bus_rle->end && end > bus_rle->end) {
733 bus_rle->count += end - bus_rle->end;
734 bus_rle->end = end;
735 done = TRUE;
738 /* If we adjusted the old resource, we're finished. */
739 if (done)
740 break;
742 } else {
743 bus_rle = NULL;
745 /* If we didn't merge with anything, add this resource. */
746 if (bus_rle == NULL)
747 bus_set_resource(bus, type, acpi_sysres_rid++, start, count, -1);
750 /* After merging/moving resources to the parent, free the list. */
751 resource_list_free(dev_rl);
753 return (0);