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