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