Merge branch 'vendor/LESS'
[dragonfly.git] / sys / dev / acpica / acpi_resource.c
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2000 BSDi
4  * All rights reserved.
5  *
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.
14  *
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.
26  *
27  * $FreeBSD: head/sys/dev/acpica/acpi_resource.c 263975 2014-03-31 19:37:39Z imp $
28  */
29
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>
39
40 #include <sys/rman.h>
41
42 #include "acpi.h"
43 #include <dev/acpica/acpivar.h>
44
45 /* Hooks for the ACPICA debugging infrastructure */
46 #define _COMPONENT      ACPI_BUS
47 ACPI_MODULE_NAME("RESOURCE")
48
49 struct lookup_irq_request {
50     ACPI_RESOURCE *acpi_res;
51     struct resource *res;
52     int         counter;
53     int         rid;
54     int         found;
55 };
56
57 static ACPI_STATUS
58 acpi_lookup_irq_handler(ACPI_RESOURCE *res, void *context)
59 {
60     struct lookup_irq_request *req;
61     size_t len;
62     u_int irqnum;
63     u_int irq __debugvar;
64
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);
78     }
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);
85     }
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);
91 }
92
93 ACPI_STATUS
94 acpi_lookup_irq_resource(device_t dev, int rid, struct resource *res,
95     ACPI_RESOURCE *acpi_res)
96 {
97     struct lookup_irq_request req;
98     ACPI_STATUS status;
99
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);
110 }
111
112 void
113 acpi_config_intr(device_t dev, ACPI_RESOURCE *res)
114 {
115     u_int irq;
116     int pol, trig;
117     enum intr_trigger trigger;
118     enum intr_polarity polarity;
119
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);
137     }
138
139 #if defined(__x86_64__) || defined(__i386__)
140     /*
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.
145      */
146     if (irq < 16 && trig == ACPI_EDGE_SENSITIVE && pol == ACPI_ACTIVE_LOW)
147         pol = ACPI_ACTIVE_HIGH;
148 #endif
149
150     if (trig == ACPI_EDGE_SENSITIVE)
151         trigger = INTR_TRIGGER_EDGE;
152     else
153         trigger = INTR_TRIGGER_LEVEL;
154
155     if (pol == ACPI_ACTIVE_HIGH)
156         polarity = INTR_POLARITY_HIGH;
157     else
158         polarity = INTR_POLARITY_LOW;
159
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);
164 }
165
166 struct acpi_resource_context {
167     struct acpi_parse_resource_set *set;
168     device_t    dev;
169     void        *context;
170 };
171
172 #ifdef ACPI_DEBUG_OUTPUT
173 static const char *
174 acpi_address_range_name(UINT8 ResourceType)
175 {
176     static char buf[16];
177
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);
188     }
189 }
190 #endif
191             
192 static ACPI_STATUS
193 acpi_parse_resource(ACPI_RESOURCE *res, void *context)
194 {
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;
202
203     arc = context;
204     dev = arc->dev;
205     set = arc->set;
206
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);
234         }
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);
260         }
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);
277         }
278         break;
279     case ACPI_RESOURCE_TYPE_IRQ:
280         /*
281          * from 1.0b 6.4.2 
282          * "This structure is repeated for each separate interrupt
283          * required"
284          */
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:
290         /*
291          * from 1.0b 6.4.3 
292          * "This structure is repeated for each separate DMA channel
293          * required"
294          */
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.Granularity;
314             min = res->Data.Address16.Minimum;
315             max = res->Data.Address16.Maximum;
316             length = res->Data.Address16.AddressLength;
317 #ifdef ACPI_DEBUG
318             name = "Address16";
319 #endif
320             break;
321         case ACPI_RESOURCE_TYPE_ADDRESS32:
322             gran = res->Data.Address32.Granularity;
323             min = res->Data.Address32.Minimum;
324             max = res->Data.Address32.Maximum;
325             length = res->Data.Address32.AddressLength;
326 #ifdef ACPI_DEBUG
327             name = "Address32";
328 #endif
329             break;
330         case ACPI_RESOURCE_TYPE_ADDRESS64:
331             gran = res->Data.Address64.Granularity;
332             min = res->Data.Address64.Minimum;
333             max = res->Data.Address64.Maximum;
334             length = res->Data.Address64.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.Granularity;
343             min = res->Data.ExtAddress64.Minimum;
344             max = res->Data.ExtAddress64.Maximum;
345             length = res->Data.ExtAddress64.AddressLength;
346 #ifdef ACPI_DEBUG
347             name = "ExtAddress64";
348 #endif
349             break;
350         }
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;
358         }
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;
364         }
365
366 #ifdef __i386__
367         if (min > ULONG_MAX || (res->Data.Address.MaxAddressFixed && max >
368             ULONG_MAX)) {
369             ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "ignored %s above 4G\n",
370                 name));
371             break;
372         }
373         if (max > ULONG_MAX)
374                 max = ULONG_MAX;
375 #endif
376         if (res->Data.Address.MinAddressFixed == ACPI_ADDRESS_FIXED &&
377             res->Data.Address.MaxAddressFixed == ACPI_ADDRESS_FIXED) {
378             if (res->Data.Address.ResourceType == ACPI_MEMORY_RANGE) {
379                 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "%s/Memory 0x%jx/%ju\n",
380                     name, (uintmax_t)min, (uintmax_t)length));
381                 set->set_memory(dev, arc->context, min, length);
382             } else {
383                 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "%s/IO 0x%jx/%ju\n", name,
384                     (uintmax_t)min, (uintmax_t)length));
385                 set->set_ioport(dev, arc->context, min, length);
386             }
387         } else {
388             if (res->Data.Address32.ResourceType == ACPI_MEMORY_RANGE) {
389                 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
390                     "%s/Memory 0x%jx-0x%jx/%ju\n", name, (uintmax_t)min,
391                     (uintmax_t)max, (uintmax_t)length));
392                 set->set_memoryrange(dev, arc->context, min, max, length, gran);
393             } else {
394                 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "%s/IO 0x%jx-0x%jx/%ju\n",
395                     name, (uintmax_t)min, (uintmax_t)max, (uintmax_t)length));
396                 set->set_iorange(dev, arc->context, min, max, length, gran);
397             }
398         }                   
399         break;
400     case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
401         if (res->Data.ExtendedIrq.ProducerConsumer != ACPI_CONSUMER) {
402             ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "ignored ExtIRQ producer\n"));
403             break;
404         }
405         set->set_ext_irq(dev, arc->context, res->Data.ExtendedIrq.Interrupts,
406             res->Data.ExtendedIrq.InterruptCount,
407             res->Data.ExtendedIrq.Triggering, res->Data.ExtendedIrq.Polarity);
408         break;
409     case ACPI_RESOURCE_TYPE_VENDOR:
410         ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
411             "unimplemented VendorSpecific resource\n"));
412         break;
413     default:
414         break;
415     }
416     return (AE_OK);
417 }
418
419 /*
420  * Fetch a device's resources and associate them with the device.
421  *
422  * Note that it might be nice to also locate ACPI-specific resource items, such
423  * as GPE bits.
424  *
425  * We really need to split the resource-fetching code out from the
426  * resource-parsing code, since we may want to use the parsing
427  * code for _PRS someday.
428  */
429 ACPI_STATUS
430 acpi_parse_resources(device_t dev, ACPI_HANDLE handle,
431                      struct acpi_parse_resource_set *set, void *arg)
432 {
433     struct acpi_resource_context arc;
434     ACPI_STATUS         status;
435
436     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
437
438     set->set_init(dev, arg, &arc.context);
439     arc.set = set;
440     arc.dev = dev;
441     status = AcpiWalkResources(handle, "_CRS", acpi_parse_resource, &arc);
442     if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
443         kprintf("can't fetch resources for %s - %s\n",
444             acpi_name(handle), AcpiFormatException(status));
445         return_ACPI_STATUS (status);
446     }
447     set->set_done(dev, arc.context);
448     return_ACPI_STATUS (AE_OK);
449 }
450
451 /*
452  * Resource-set vectors used to attach _CRS-derived resources 
453  * to an ACPI device.
454  */
455 static void     acpi_res_set_init(device_t dev, void *arg, void **context);
456 static void     acpi_res_set_done(device_t dev, void *context);
457 static void     acpi_res_set_ioport(device_t dev, void *context,
458                                     uint64_t base, uint64_t length);
459 static void     acpi_res_set_iorange(device_t dev, void *context,
460                                      uint64_t low, uint64_t high, 
461                                      uint64_t length, uint64_t align);
462 static void     acpi_res_set_memory(device_t dev, void *context,
463                                     uint64_t base, uint64_t length);
464 static void     acpi_res_set_memoryrange(device_t dev, void *context,
465                                          uint64_t low, uint64_t high, 
466                                          uint64_t length, uint64_t align);
467 static void     acpi_res_set_irq(device_t dev, void *context, uint8_t *irq,
468                                  int count, int trig, int pol);
469 static void     acpi_res_set_ext_irq(device_t dev, void *context,
470                                  uint32_t *irq, int count, int trig, int pol);
471 static void     acpi_res_set_drq(device_t dev, void *context, uint8_t *drq,
472                                  int count);
473 static void     acpi_res_set_start_dependent(device_t dev, void *context,
474                                              int preference);
475 static void     acpi_res_set_end_dependent(device_t dev, void *context);
476
477 struct acpi_parse_resource_set acpi_res_parse_set = {
478     acpi_res_set_init,
479     acpi_res_set_done,
480     acpi_res_set_ioport,
481     acpi_res_set_iorange,
482     acpi_res_set_memory,
483     acpi_res_set_memoryrange,
484     acpi_res_set_irq,
485     acpi_res_set_ext_irq,
486     acpi_res_set_drq,
487     acpi_res_set_start_dependent,
488     acpi_res_set_end_dependent
489 };
490
491 struct acpi_res_context {
492     int         ar_nio;
493     int         ar_nmem;
494     int         ar_nirq;
495     int         ar_ndrq;
496     void        *ar_parent;
497 };
498
499 static void
500 acpi_res_set_init(device_t dev, void *arg, void **context)
501 {
502     struct acpi_res_context     *cp;
503
504     if ((cp = AcpiOsAllocate(sizeof(*cp))) != NULL) {
505         bzero(cp, sizeof(*cp));
506         cp->ar_parent = arg;
507         *context = cp;
508     }
509 }
510
511 static void
512 acpi_res_set_done(device_t dev, void *context)
513 {
514     struct acpi_res_context     *cp = (struct acpi_res_context *)context;
515
516     if (cp == NULL)
517         return;
518     AcpiOsFree(cp);
519 }
520
521 static void
522 acpi_res_set_ioport(device_t dev, void *context, uint64_t base,
523                     uint64_t length)
524 {
525     struct acpi_res_context     *cp = (struct acpi_res_context *)context;
526
527     if (cp == NULL)
528         return;
529     bus_set_resource(dev, SYS_RES_IOPORT, cp->ar_nio++, base, length, -1);
530 }
531
532 static void
533 acpi_res_set_iorange(device_t dev, void *context, uint64_t low,
534                      uint64_t high, uint64_t length, uint64_t align)
535 {
536     struct acpi_res_context     *cp = (struct acpi_res_context *)context;
537
538     if (cp == NULL)
539         return;
540     device_printf(dev, "I/O range not supported\n");
541 }
542
543 static void
544 acpi_res_set_memory(device_t dev, void *context, uint64_t base,
545                     uint64_t length)
546 {
547     struct acpi_res_context     *cp = (struct acpi_res_context *)context;
548
549     if (cp == NULL)
550         return;
551
552     bus_set_resource(dev, SYS_RES_MEMORY, cp->ar_nmem++, base, length, -1);
553 }
554
555 static void
556 acpi_res_set_memoryrange(device_t dev, void *context, uint64_t low,
557                          uint64_t high, uint64_t length, uint64_t align)
558 {
559     struct acpi_res_context     *cp = (struct acpi_res_context *)context;
560
561     if (cp == NULL)
562         return;
563     device_printf(dev, "memory range not supported\n");
564 }
565
566 static void
567 acpi_res_set_irq(device_t dev, void *context, uint8_t *irq, int count,
568     int trig, int pol)
569 {
570     struct acpi_res_context     *cp = (struct acpi_res_context *)context;
571
572     if (cp == NULL || irq == NULL)
573         return;
574
575     /* This implements no resource relocation. */
576     if (count != 1)
577         return;
578
579     bus_set_resource(dev, SYS_RES_IRQ, cp->ar_nirq++, *irq, 1,
580         machintr_legacy_intr_cpuid(*irq));
581 }
582
583 static void
584 acpi_res_set_ext_irq(device_t dev, void *context, uint32_t *irq, int count,
585     int trig, int pol)
586 {
587     struct acpi_res_context     *cp = (struct acpi_res_context *)context;
588
589     if (cp == NULL || irq == NULL)
590         return;
591
592     /* This implements no resource relocation. */
593     if (count != 1)
594         return;
595
596     /* There is no such IRQ at all */
597     if (machintr_legacy_intr_find(*irq,
598         INTR_TRIGGER_CONFORM, INTR_POLARITY_CONFORM) < 0)
599         return;
600
601     bus_set_resource(dev, SYS_RES_IRQ, cp->ar_nirq++, *irq, 1,
602         machintr_legacy_intr_cpuid(*irq));
603 }
604
605 static void
606 acpi_res_set_drq(device_t dev, void *context, uint8_t *drq, int count)
607 {
608     struct acpi_res_context     *cp = (struct acpi_res_context *)context;
609
610     if (cp == NULL || drq == NULL)
611         return;
612     
613     /* This implements no resource relocation. */
614     if (count != 1)
615         return;
616
617     bus_set_resource(dev, SYS_RES_DRQ, cp->ar_ndrq++, *drq, 1, -1);
618 }
619
620 static void
621 acpi_res_set_start_dependent(device_t dev, void *context, int preference)
622 {
623     struct acpi_res_context     *cp = (struct acpi_res_context *)context;
624
625     if (cp == NULL)
626         return;
627     device_printf(dev, "dependent functions not supported\n");
628 }
629
630 static void
631 acpi_res_set_end_dependent(device_t dev, void *context)
632 {
633     struct acpi_res_context     *cp = (struct acpi_res_context *)context;
634
635     if (cp == NULL)
636         return;
637     device_printf(dev, "dependent functions not supported\n");
638 }
639
640 /*
641  * Resource-owning placeholders for IO and memory pseudo-devices.
642  *
643  * This code allocates system resources that will be used by ACPI
644  * child devices.  The acpi parent manages these resources through a
645  * private rman.
646  */
647
648 static int      acpi_sysres_rid = 100;
649
650 static int      acpi_sysres_probe(device_t dev);
651 static int      acpi_sysres_attach(device_t dev);
652
653 static device_method_t acpi_sysres_methods[] = {
654     /* Device interface */
655     DEVMETHOD(device_probe,     acpi_sysres_probe),
656     DEVMETHOD(device_attach,    acpi_sysres_attach),
657
658     DEVMETHOD_END
659 };
660
661 static driver_t acpi_sysres_driver = {
662     "acpi_sysresource",
663     acpi_sysres_methods,
664     0,
665 };
666
667 static devclass_t acpi_sysres_devclass;
668 DRIVER_MODULE(acpi_sysresource, acpi, acpi_sysres_driver, acpi_sysres_devclass,
669     NULL, NULL);
670 MODULE_DEPEND(acpi_sysresource, acpi, 1, 1, 1);
671
672 static int
673 acpi_sysres_probe(device_t dev)
674 {
675     static char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL };
676
677     if (acpi_disabled("sysresource") ||
678         ACPI_ID_PROBE(device_get_parent(dev), dev, sysres_ids) == NULL)
679         return (ENXIO);
680
681     device_set_desc(dev, "System Resource");
682     device_quiet(dev);
683     return (BUS_PROBE_DEFAULT);
684 }
685
686 static int
687 acpi_sysres_attach(device_t dev)
688 {
689     device_t bus;
690     struct resource_list_entry *bus_rle, *dev_rle;
691     struct resource_list *bus_rl, *dev_rl;
692     int done, type;
693     u_long start, end, count;
694
695     /*
696      * Loop through all current resources to see if the new one overlaps
697      * any existing ones.  If so, grow the old one up and/or down
698      * accordingly.  Discard any that are wholly contained in the old.  If
699      * the resource is unique, add it to the parent.  It will later go into
700      * the rman pool.
701      */
702     bus = device_get_parent(dev);
703     dev_rl = BUS_GET_RESOURCE_LIST(bus, dev);
704     bus_rl = BUS_GET_RESOURCE_LIST(device_get_parent(bus), bus);
705     if (bus_rl)
706         kprintf("busrl is not null!\n");
707     SLIST_FOREACH(dev_rle, dev_rl, link) {
708         if (dev_rle->type != SYS_RES_IOPORT && dev_rle->type != SYS_RES_MEMORY)
709             continue;
710
711         start = dev_rle->start;
712         end = dev_rle->end;
713         count = dev_rle->count;
714         type = dev_rle->type;
715         done = FALSE;
716         if (bus_rl) {
717             SLIST_FOREACH(bus_rle, bus_rl, link) {
718                 if (bus_rle->type != type)
719                     continue;
720
721                 /* New resource wholly contained in old, discard. */
722                 if (start >= bus_rle->start && end <= bus_rle->end)
723                     break;
724
725                 /* New tail overlaps old head, grow existing resource downward. */
726                 if (start < bus_rle->start && end >= bus_rle->start) {
727                     bus_rle->count += bus_rle->start - start;
728                     bus_rle->start = start;
729                     done = TRUE;
730                 }
731
732                 /* New head overlaps old tail, grow existing resource upward. */
733                 if (start <= bus_rle->end && end > bus_rle->end) {
734                     bus_rle->count += end - bus_rle->end;
735                     bus_rle->end = end;
736                     done = TRUE;
737                 }
738
739                 /* If we adjusted the old resource, we're finished. */
740                 if (done)
741                     break;
742             }
743         } else {
744             bus_rle = NULL;
745         }
746         /* If we didn't merge with anything, add this resource. */
747         if (bus_rle == NULL)
748             bus_set_resource(bus, type, acpi_sysres_rid++, start, count, -1);
749     }
750
751     /* After merging/moving resources to the parent, free the list. */
752     resource_list_free(dev_rl);
753
754     return (0);
755 }