acpi/resource: Use legacy_intr_find to detect invalid IRQ configure
[dragonfly.git] / sys / dev / acpica5 / 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: src/sys/dev/acpica/acpi_resource.c,v 1.40.8.1 2009/04/15 03:14:26 kensmith Exp $
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/malloc.h>
35 #include <sys/module.h>
36 #include <sys/resource.h>
37 #include <sys/machintr.h>
38
39 #include <sys/rman.h>
40
41 #include "acpi.h"
42 #include <dev/acpica5/acpivar.h>
43
44 /* Hooks for the ACPI CA debugging infrastructure */
45 #define _COMPONENT      ACPI_BUS
46 ACPI_MODULE_NAME("RESOURCE")
47
48 struct lookup_irq_request {
49     ACPI_RESOURCE *acpi_res;
50     struct resource *res;
51     int         counter;
52     int         rid;
53     int         found;
54 };
55
56 static ACPI_STATUS
57 acpi_lookup_irq_handler(ACPI_RESOURCE *res, void *context)
58 {
59     struct lookup_irq_request *req;
60     size_t len;
61     u_int irqnum, irq;
62
63     switch (res->Type) {
64     case ACPI_RESOURCE_TYPE_IRQ:
65         irqnum = res->Data.Irq.InterruptCount;
66         irq = res->Data.Irq.Interrupts[0];
67         len = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
68         break;
69     case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
70         irqnum = res->Data.ExtendedIrq.InterruptCount;
71         irq = res->Data.ExtendedIrq.Interrupts[0];
72         len = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
73         break;
74     default:
75         return (AE_OK);
76     }
77     if (irqnum != 1)
78         return (AE_OK);
79     req = (struct lookup_irq_request *)context;
80     if (req->counter != req->rid) {
81         req->counter++;
82         return (AE_OK);
83     }
84     req->found = 1;
85     KASSERT(irq == rman_get_start(req->res),
86         ("IRQ resources do not match"));
87     bcopy(res, req->acpi_res, len);
88     return (AE_CTRL_TERMINATE);
89 }
90
91 ACPI_STATUS
92 acpi_lookup_irq_resource(device_t dev, int rid, struct resource *res,
93     ACPI_RESOURCE *acpi_res)
94 {
95     struct lookup_irq_request req;
96     ACPI_STATUS status;
97
98     req.acpi_res = acpi_res;
99     req.res = res;
100     req.counter = 0;
101     req.rid = rid;
102     req.found = 0;
103     status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
104         acpi_lookup_irq_handler, &req);
105     if (ACPI_SUCCESS(status) && req.found == 0)
106         status = AE_NOT_FOUND;
107     return (status);
108 }
109
110 void
111 acpi_config_intr(device_t dev, ACPI_RESOURCE *res)
112 {
113     u_int irq;
114     int pol, trig;
115     enum intr_trigger trigger;
116     enum intr_polarity polarity;
117
118     switch (res->Type) {
119     case ACPI_RESOURCE_TYPE_IRQ:
120         KASSERT(res->Data.Irq.InterruptCount == 1,
121             ("%s: multiple interrupts", __func__));
122         irq = res->Data.Irq.Interrupts[0];
123         trig = res->Data.Irq.Triggering;
124         pol = res->Data.Irq.Polarity;
125         break;
126     case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
127         KASSERT(res->Data.ExtendedIrq.InterruptCount == 1,
128             ("%s: multiple interrupts", __func__));
129         irq = res->Data.ExtendedIrq.Interrupts[0];
130         trig = res->Data.ExtendedIrq.Triggering;
131         pol = res->Data.ExtendedIrq.Polarity;
132         break;
133     default:
134         panic("%s: bad resource type %u", __func__, res->Type);
135     }
136
137     if (trig == ACPI_EDGE_SENSITIVE)
138         trigger = INTR_TRIGGER_EDGE;
139     else
140         trigger = INTR_TRIGGER_LEVEL;
141
142     if (pol == ACPI_ACTIVE_HIGH)
143         polarity = INTR_POLARITY_HIGH;
144     else
145         polarity = INTR_POLARITY_LOW;
146
147     if (machintr_legacy_intr_find(irq, trigger, polarity) < 0) {
148         if (bootverbose)
149                 kprintf("acpi_config_intr: Skip irq %d config\n", irq);
150     } else {
151         BUS_CONFIG_INTR(dev, dev, irq, trigger, polarity);
152     }
153 }
154
155 /*
156  * Fetch a device's resources and associate them with the device.
157  *
158  * Note that it might be nice to also locate ACPI-specific resource items, such
159  * as GPE bits.
160  *
161  * We really need to split the resource-fetching code out from the
162  * resource-parsing code, since we may want to use the parsing
163  * code for _PRS someday.
164  */
165 ACPI_STATUS
166 acpi_parse_resources(device_t dev, ACPI_HANDLE handle,
167                      struct acpi_parse_resource_set *set, void *arg)
168 {
169     ACPI_BUFFER         buf;
170     ACPI_RESOURCE       *res;
171     char                *curr, *last;
172     ACPI_STATUS         status;
173     void                *context;
174
175     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
176
177     /*
178      * Special-case some devices that abuse _PRS/_CRS to mean
179      * something other than "I consume this resource".
180      *
181      * XXX do we really need this?  It's only relevant once
182      *     we start always-allocating these resources, and even
183      *     then, the only special-cased device is likely to be
184      *     the PCI interrupt link.
185      */
186
187     /* Fetch the device's current resources. */
188     buf.Length = ACPI_ALLOCATE_BUFFER;
189     if (ACPI_FAILURE((status = AcpiGetCurrentResources(handle, &buf)))) {
190         if (status != AE_NOT_FOUND && status != AE_TYPE)
191             kprintf("can't fetch resources for %s - %s\n",
192                    acpi_name(handle), AcpiFormatException(status));
193         return_ACPI_STATUS (status);
194     }
195     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "%s - got %ld bytes of resources\n",
196                      acpi_name(handle), (long)buf.Length));
197     set->set_init(dev, arg, &context);
198
199     /* Iterate through the resources */
200     curr = buf.Pointer;
201     last = (char *)buf.Pointer + buf.Length;
202     while (curr < last) {
203         res = (ACPI_RESOURCE *)curr;
204         curr += res->Length;
205
206         /* Handle the individual resource types */
207         switch(res->Type) {
208         case ACPI_RESOURCE_TYPE_END_TAG:
209             ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "EndTag\n"));
210             curr = last;
211             break;
212         case ACPI_RESOURCE_TYPE_FIXED_IO:
213             if (res->Data.FixedIo.AddressLength <= 0)
214                 break;
215             ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "FixedIo 0x%x/%d\n",
216                              res->Data.FixedIo.Address,
217                              res->Data.FixedIo.AddressLength));
218             set->set_ioport(dev, context,
219                             res->Data.FixedIo.Address,
220                             res->Data.FixedIo.AddressLength);
221             break;
222         case ACPI_RESOURCE_TYPE_IO:
223             if (res->Data.Io.AddressLength <= 0)
224                 break;
225             if (res->Data.Io.Minimum == res->Data.Io.Maximum) {
226                 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Io 0x%x/%d\n",
227                                  res->Data.Io.Minimum,
228                                  res->Data.Io.AddressLength));
229                 set->set_ioport(dev, context,
230                                 res->Data.Io.Minimum,
231                                 res->Data.Io.AddressLength);
232             } else {
233                 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Io 0x%x-0x%x/%d\n",
234                                  res->Data.Io.Minimum,
235                                  res->Data.Io.Maximum, 
236                                  res->Data.Io.AddressLength));
237                 set->set_iorange(dev, context,
238                                  res->Data.Io.Minimum,
239                                  res->Data.Io.Maximum, 
240                                  res->Data.Io.AddressLength,
241                                  res->Data.Io.Alignment);
242             }
243             break;
244         case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
245             if (res->Data.FixedMemory32.AddressLength <= 0)
246                 break;
247             ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "FixedMemory32 0x%x/%d\n",
248                               res->Data.FixedMemory32.Address, 
249                               res->Data.FixedMemory32.AddressLength));
250             set->set_memory(dev, context,
251                             res->Data.FixedMemory32.Address, 
252                             res->Data.FixedMemory32.AddressLength);
253             break;
254         case ACPI_RESOURCE_TYPE_MEMORY32:
255             if (res->Data.Memory32.AddressLength <= 0)
256                 break;
257             if (res->Data.Memory32.Minimum ==
258                 res->Data.Memory32.Maximum) {
259
260                 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory32 0x%x/%d\n",
261                                   res->Data.Memory32.Minimum, 
262                                   res->Data.Memory32.AddressLength));
263                 set->set_memory(dev, context,
264                                 res->Data.Memory32.Minimum,
265                                 res->Data.Memory32.AddressLength);
266             } else {
267                 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory32 0x%x-0x%x/%d\n",
268                                  res->Data.Memory32.Minimum, 
269                                  res->Data.Memory32.Maximum,
270                                  res->Data.Memory32.AddressLength));
271                 set->set_memoryrange(dev, context,
272                                      res->Data.Memory32.Minimum,
273                                      res->Data.Memory32.Maximum,
274                                      res->Data.Memory32.AddressLength,
275                                      res->Data.Memory32.Alignment);
276             }
277             break;
278         case ACPI_RESOURCE_TYPE_MEMORY24:
279             if (res->Data.Memory24.AddressLength <= 0)
280                 break;
281             if (res->Data.Memory24.Minimum ==
282                 res->Data.Memory24.Maximum) {
283
284                 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory24 0x%x/%d\n",
285                                  res->Data.Memory24.Minimum, 
286                                  res->Data.Memory24.AddressLength));
287                 set->set_memory(dev, context, res->Data.Memory24.Minimum,
288                                 res->Data.Memory24.AddressLength);
289             } else {
290                 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory24 0x%x-0x%x/%d\n",
291                                  res->Data.Memory24.Minimum, 
292                                  res->Data.Memory24.Maximum,
293                                  res->Data.Memory24.AddressLength));
294                 set->set_memoryrange(dev, context,
295                                      res->Data.Memory24.Minimum,
296                                      res->Data.Memory24.Maximum,
297                                      res->Data.Memory24.AddressLength,
298                                      res->Data.Memory24.Alignment);
299             }
300             break;
301         case ACPI_RESOURCE_TYPE_IRQ:
302             /*
303              * from 1.0b 6.4.2 
304              * "This structure is repeated for each separate interrupt
305              * required"
306              */
307             set->set_irq(dev, context, res->Data.Irq.Interrupts,
308                 res->Data.Irq.InterruptCount, res->Data.Irq.Triggering,
309                 res->Data.Irq.Polarity);
310             break;
311         case ACPI_RESOURCE_TYPE_DMA:
312             /*
313              * from 1.0b 6.4.3 
314              * "This structure is repeated for each separate dma channel
315              * required"
316              */
317             set->set_drq(dev, context, res->Data.Dma.Channels,
318                          res->Data.Dma.ChannelCount);
319             break;
320         case ACPI_RESOURCE_TYPE_START_DEPENDENT:
321             ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "start dependent functions\n"));
322             set->set_start_dependent(dev, context,
323                                      res->Data.StartDpf.CompatibilityPriority);
324             break;
325         case ACPI_RESOURCE_TYPE_END_DEPENDENT:
326             ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "end dependent functions\n"));
327             set->set_end_dependent(dev, context);
328             break;
329         case ACPI_RESOURCE_TYPE_ADDRESS32:
330             if (res->Data.Address32.AddressLength <= 0)
331                 break;
332             if (res->Data.Address32.ProducerConsumer != ACPI_CONSUMER) {
333                 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
334                     "ignored Address32 %s producer\n",
335                     res->Data.Address32.ResourceType == ACPI_IO_RANGE ?
336                     "IO" : "Memory"));
337                 break;
338             }
339             if (res->Data.Address32.ResourceType != ACPI_MEMORY_RANGE &&
340                 res->Data.Address32.ResourceType != ACPI_IO_RANGE) {
341                 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
342                     "ignored Address32 for non-memory, non-I/O\n"));
343                 break;
344             }
345
346             if (res->Data.Address32.MinAddressFixed == ACPI_ADDRESS_FIXED &&
347                 res->Data.Address32.MaxAddressFixed == ACPI_ADDRESS_FIXED) {
348
349                 if (res->Data.Address32.ResourceType == ACPI_MEMORY_RANGE) {
350                     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
351                                      "Address32/Memory 0x%x/%d\n",
352                                      res->Data.Address32.Minimum,
353                                      res->Data.Address32.AddressLength));
354                     set->set_memory(dev, context,
355                                     res->Data.Address32.Minimum,
356                                     res->Data.Address32.AddressLength);
357                 } else {
358                     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
359                                      "Address32/IO 0x%x/%d\n",
360                                      res->Data.Address32.Minimum,
361                                      res->Data.Address32.AddressLength));
362                     set->set_ioport(dev, context,
363                                     res->Data.Address32.Minimum,
364                                     res->Data.Address32.AddressLength);
365                 }
366             } else {
367                 if (res->Data.Address32.ResourceType == ACPI_MEMORY_RANGE) {
368                     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
369                                      "Address32/Memory 0x%x-0x%x/%d\n",
370                                      res->Data.Address32.Minimum,
371                                      res->Data.Address32.Maximum,
372                                      res->Data.Address32.AddressLength));
373                     set->set_memoryrange(dev, context,
374                                           res->Data.Address32.Minimum,
375                                           res->Data.Address32.Maximum,
376                                           res->Data.Address32.AddressLength,
377                                           res->Data.Address32.Granularity);
378                 } else {
379                     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
380                                      "Address32/IO 0x%x-0x%x/%d\n",
381                                      res->Data.Address32.Minimum,
382                                      res->Data.Address32.Maximum,
383                                      res->Data.Address32.AddressLength));
384                     set->set_iorange(dev, context,
385                                      res->Data.Address32.Minimum,
386                                      res->Data.Address32.Maximum,
387                                      res->Data.Address32.AddressLength,
388                                      res->Data.Address32.Granularity);
389                 }
390             }               
391             break;
392         case ACPI_RESOURCE_TYPE_ADDRESS16:
393             if (res->Data.Address16.AddressLength <= 0)
394                 break;
395             if (res->Data.Address16.ProducerConsumer != ACPI_CONSUMER) {
396                 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
397                     "ignored Address16 %s producer\n",
398                     res->Data.Address16.ResourceType == ACPI_IO_RANGE ?
399                     "IO" : "Memory"));
400                 break;
401             }
402             if (res->Data.Address16.ResourceType != ACPI_MEMORY_RANGE &&
403                 res->Data.Address16.ResourceType != ACPI_IO_RANGE) {
404                 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
405                         "ignored Address16 for non-memory, non-I/O\n"));
406                 break;
407             }
408
409             if (res->Data.Address16.MinAddressFixed == ACPI_ADDRESS_FIXED &&
410                 res->Data.Address16.MaxAddressFixed == ACPI_ADDRESS_FIXED) {
411
412                 if (res->Data.Address16.ResourceType == ACPI_MEMORY_RANGE) {
413                     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
414                                      "Address16/Memory 0x%x/%d\n",
415                                      res->Data.Address16.Minimum,
416                                      res->Data.Address16.AddressLength));
417                     set->set_memory(dev, context,
418                                     res->Data.Address16.Minimum,
419                                     res->Data.Address16.AddressLength);
420                 } else {
421                     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
422                                      "Address16/IO 0x%x/%d\n",
423                                      res->Data.Address16.Minimum,
424                                      res->Data.Address16.AddressLength));
425                     set->set_ioport(dev, context,
426                                     res->Data.Address16.Minimum,
427                                     res->Data.Address16.AddressLength);
428                 }
429             } else {
430                 if (res->Data.Address16.ResourceType == ACPI_MEMORY_RANGE) {
431                     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
432                                      "Address16/Memory 0x%x-0x%x/%d\n",
433                                      res->Data.Address16.Minimum,
434                                      res->Data.Address16.Maximum,
435                                      res->Data.Address16.AddressLength));
436                     set->set_memoryrange(dev, context,
437                                           res->Data.Address16.Minimum,
438                                           res->Data.Address16.Maximum,
439                                           res->Data.Address16.AddressLength,
440                                           res->Data.Address16.Granularity);
441                 } else {
442                     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
443                                      "Address16/IO 0x%x-0x%x/%d\n",
444                                      res->Data.Address16.Minimum,
445                                      res->Data.Address16.Maximum,
446                                      res->Data.Address16.AddressLength));
447                     set->set_iorange(dev, context,
448                                      res->Data.Address16.Minimum,
449                                      res->Data.Address16.Maximum,
450                                      res->Data.Address16.AddressLength,
451                                      res->Data.Address16.Granularity);
452                 }
453             }               
454             break;
455         case ACPI_RESOURCE_TYPE_ADDRESS64:
456             ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
457                              "unimplemented Address64 resource\n"));
458             break;
459         case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
460             if (res->Data.ExtendedIrq.ProducerConsumer != ACPI_CONSUMER) {
461                 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
462                     "ignored ExtIRQ producer\n"));
463                 break;
464             }
465             set->set_ext_irq(dev, context, res->Data.ExtendedIrq.Interrupts,
466                 res->Data.ExtendedIrq.InterruptCount,
467                 res->Data.ExtendedIrq.Triggering,
468                 res->Data.ExtendedIrq.Polarity);
469             break;
470         case ACPI_RESOURCE_TYPE_VENDOR:
471             ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
472                              "unimplemented VendorSpecific resource\n"));
473             break;
474         default:
475             break;
476         }
477     }    
478
479     AcpiOsFree(buf.Pointer);
480     set->set_done(dev, context);
481     return_ACPI_STATUS (AE_OK);
482 }
483
484 /*
485  * Resource-set vectors used to attach _CRS-derived resources 
486  * to an ACPI device.
487  */
488 static void     acpi_res_set_init(device_t dev, void *arg, void **context);
489 static void     acpi_res_set_done(device_t dev, void *context);
490 static void     acpi_res_set_ioport(device_t dev, void *context,
491                                     u_int32_t base, u_int32_t length);
492 static void     acpi_res_set_iorange(device_t dev, void *context,
493                                      u_int32_t low, u_int32_t high, 
494                                      u_int32_t length, u_int32_t align);
495 static void     acpi_res_set_memory(device_t dev, void *context,
496                                     u_int32_t base, u_int32_t length);
497 static void     acpi_res_set_memoryrange(device_t dev, void *context,
498                                          u_int32_t low, u_int32_t high, 
499                                          u_int32_t length, u_int32_t align);
500 static void     acpi_res_set_irq(device_t dev, void *context, u_int8_t *irq,
501                                  int count, int trig, int pol);
502 static void     acpi_res_set_ext_irq(device_t dev, void *context,
503                                  u_int32_t *irq, int count, int trig, int pol);
504 static void     acpi_res_set_drq(device_t dev, void *context, u_int8_t *drq,
505                                  int count);
506 static void     acpi_res_set_start_dependent(device_t dev, void *context,
507                                              int preference);
508 static void     acpi_res_set_end_dependent(device_t dev, void *context);
509
510 struct acpi_parse_resource_set acpi_res_parse_set = {
511     acpi_res_set_init,
512     acpi_res_set_done,
513     acpi_res_set_ioport,
514     acpi_res_set_iorange,
515     acpi_res_set_memory,
516     acpi_res_set_memoryrange,
517     acpi_res_set_irq,
518     acpi_res_set_ext_irq,
519     acpi_res_set_drq,
520     acpi_res_set_start_dependent,
521     acpi_res_set_end_dependent
522 };
523
524 struct acpi_res_context {
525     int         ar_nio;
526     int         ar_nmem;
527     int         ar_nirq;
528     int         ar_ndrq;
529     void        *ar_parent;
530 };
531
532 static void
533 acpi_res_set_init(device_t dev, void *arg, void **context)
534 {
535     struct acpi_res_context     *cp;
536
537     if ((cp = AcpiOsAllocate(sizeof(*cp))) != NULL) {
538         bzero(cp, sizeof(*cp));
539         cp->ar_parent = arg;
540         *context = cp;
541     }
542 }
543
544 static void
545 acpi_res_set_done(device_t dev, void *context)
546 {
547     struct acpi_res_context     *cp = (struct acpi_res_context *)context;
548
549     if (cp == NULL)
550         return;
551     AcpiOsFree(cp);
552 }
553
554 static void
555 acpi_res_set_ioport(device_t dev, void *context, u_int32_t base,
556                     u_int32_t length)
557 {
558     struct acpi_res_context     *cp = (struct acpi_res_context *)context;
559
560     if (cp == NULL)
561         return;
562     bus_set_resource(dev, SYS_RES_IOPORT, cp->ar_nio++, base, length, -1);
563 }
564
565 static void
566 acpi_res_set_iorange(device_t dev, void *context, u_int32_t low,
567                      u_int32_t high, u_int32_t length, u_int32_t align)
568 {
569     struct acpi_res_context     *cp = (struct acpi_res_context *)context;
570
571     if (cp == NULL)
572         return;
573     device_printf(dev, "I/O range not supported\n");
574 }
575
576 static void
577 acpi_res_set_memory(device_t dev, void *context, u_int32_t base,
578                     u_int32_t length)
579 {
580     struct acpi_res_context     *cp = (struct acpi_res_context *)context;
581
582     if (cp == NULL)
583         return;
584
585     bus_set_resource(dev, SYS_RES_MEMORY, cp->ar_nmem++, base, length, -1);
586 }
587
588 static void
589 acpi_res_set_memoryrange(device_t dev, void *context, u_int32_t low,
590                          u_int32_t high, u_int32_t length, u_int32_t align)
591 {
592     struct acpi_res_context     *cp = (struct acpi_res_context *)context;
593
594     if (cp == NULL)
595         return;
596     device_printf(dev, "memory range not supported\n");
597 }
598
599 static void
600 acpi_res_set_irq(device_t dev, void *context, u_int8_t *irq, int count,
601     int trig, int pol)
602 {
603     struct acpi_res_context     *cp = (struct acpi_res_context *)context;
604
605     if (cp == NULL || irq == NULL)
606         return;
607
608     /* This implements no resource relocation. */
609     if (count != 1)
610         return;
611
612     bus_set_resource(dev, SYS_RES_IRQ, cp->ar_nirq++, *irq, 1,
613         machintr_legacy_intr_cpuid(*irq));
614 }
615
616 static void
617 acpi_res_set_ext_irq(device_t dev, void *context, u_int32_t *irq, int count,
618     int trig, int pol)
619 {
620     struct acpi_res_context     *cp = (struct acpi_res_context *)context;
621
622     if (cp == NULL || irq == NULL)
623         return;
624
625     /* This implements no resource relocation. */
626     if (count != 1)
627         return;
628
629     bus_set_resource(dev, SYS_RES_IRQ, cp->ar_nirq++, *irq, 1,
630         machintr_legacy_intr_cpuid(*irq));
631 }
632
633 static void
634 acpi_res_set_drq(device_t dev, void *context, u_int8_t *drq, int count)
635 {
636     struct acpi_res_context     *cp = (struct acpi_res_context *)context;
637
638     if (cp == NULL || drq == NULL)
639         return;
640     
641     /* This implements no resource relocation. */
642     if (count != 1)
643         return;
644
645     bus_set_resource(dev, SYS_RES_DRQ, cp->ar_ndrq++, *drq, 1, -1);
646 }
647
648 static void
649 acpi_res_set_start_dependent(device_t dev, void *context, int preference)
650 {
651     struct acpi_res_context     *cp = (struct acpi_res_context *)context;
652
653     if (cp == NULL)
654         return;
655     device_printf(dev, "dependent functions not supported\n");
656 }
657
658 static void
659 acpi_res_set_end_dependent(device_t dev, void *context)
660 {
661     struct acpi_res_context     *cp = (struct acpi_res_context *)context;
662
663     if (cp == NULL)
664         return;
665     device_printf(dev, "dependent functions not supported\n");
666 }
667
668 /*
669  * Resource-owning placeholders for IO and memory pseudo-devices.
670  *
671  * This code allocates system resources that will be used by ACPI
672  * child devices.  The acpi parent manages these resources through a
673  * private rman.
674  */
675
676 static int      acpi_sysres_rid = 100;
677
678 static int      acpi_sysres_probe(device_t dev);
679 static int      acpi_sysres_attach(device_t dev);
680
681 static device_method_t acpi_sysres_methods[] = {
682     /* Device interface */
683     DEVMETHOD(device_probe,     acpi_sysres_probe),
684     DEVMETHOD(device_attach,    acpi_sysres_attach),
685
686     {0, 0}
687 };
688
689 static driver_t acpi_sysres_driver = {
690     "acpi_sysresource",
691     acpi_sysres_methods,
692     0,
693 };
694
695 static devclass_t acpi_sysres_devclass;
696 DRIVER_MODULE(acpi_sysresource, acpi, acpi_sysres_driver, acpi_sysres_devclass,
697     0, 0);
698 MODULE_DEPEND(acpi_sysresource, acpi, 1, 1, 1);
699
700 static int
701 acpi_sysres_probe(device_t dev)
702 {
703     static char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL };
704
705     if (acpi_disabled("sysresource") ||
706         ACPI_ID_PROBE(device_get_parent(dev), dev, sysres_ids) == NULL)
707         return (ENXIO);
708
709     device_set_desc(dev, "System Resource");
710     device_quiet(dev);
711     return (BUS_PROBE_DEFAULT);
712 }
713
714 static int
715 acpi_sysres_attach(device_t dev)
716 {
717     device_t bus;
718     struct resource_list_entry *bus_rle, *dev_rle;
719     struct resource_list *bus_rl, *dev_rl;
720     int done, type;
721     u_long start, end, count;
722     /*
723      * Loop through all current resources to see if the new one overlaps
724      * any existing ones.  If so, grow the old one up and/or down
725      * accordingly.  Discard any that are wholly contained in the old.  If
726      * the resource is unique, add it to the parent.  It will later go into
727      * the rman pool.
728      */
729     bus = device_get_parent(dev);
730     dev_rl = BUS_GET_RESOURCE_LIST(bus, dev);
731     bus_rl = BUS_GET_RESOURCE_LIST(device_get_parent(bus), bus);
732     if(bus_rl)
733         kprintf("busrl is not null!\n");
734         SLIST_FOREACH(dev_rle, dev_rl, link) {
735         if (dev_rle->type != SYS_RES_IOPORT && dev_rle->type != SYS_RES_MEMORY)
736             continue;
737
738         start = dev_rle->start;
739         end = dev_rle->end;
740         count = dev_rle->count;
741         type = dev_rle->type;
742         done = FALSE;
743         if(bus_rl) {
744         SLIST_FOREACH(bus_rle, bus_rl, link) {
745             if (bus_rle->type != type)
746                 continue;
747
748             /* New resource wholly contained in old, discard. */
749             if (start >= bus_rle->start && end <= bus_rle->end)
750                 break;
751
752             /* New tail overlaps old head, grow existing resource downward. */
753             if (start < bus_rle->start && end >= bus_rle->start) {
754                 bus_rle->count += bus_rle->start - start;
755                 bus_rle->start = start;
756                 done = TRUE;
757             }
758
759             /* New head overlaps old tail, grow existing resource upward. */
760             if (start <= bus_rle->end && end > bus_rle->end) {
761                 bus_rle->count += end - bus_rle->end;
762                 bus_rle->end = end;
763                 done = TRUE;
764             }
765
766             /* If we adjusted the old resource, we're finished. */
767             if (done)
768                 break;
769         }
770         } else bus_rle = NULL;
771         /* If we didn't merge with anything, add this resource. */
772         if (bus_rle == NULL) {
773             bus_set_resource(bus, type, acpi_sysres_rid++, start, count, -1);
774         }
775     }
776
777     /* After merging/moving resources to the parent, free the list. */
778     resource_list_free(dev_rl);
779
780     return (0);
781 }