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