]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/dev/acpica/acpi_resource.c
MFC r362623:
[FreeBSD/stable/8.git] / sys / dev / acpica / acpi_resource.c
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2000 BSDi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include "opt_acpi.h"
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/bus.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37
38 #include <machine/bus.h>
39 #include <machine/resource.h>
40 #include <sys/rman.h>
41
42 #include <contrib/dev/acpica/include/acpi.h>
43 #include <contrib/dev/acpica/include/accommon.h>
44
45 #include <dev/acpica/acpivar.h>
46
47 /* Hooks for the ACPI CA debugging infrastructure */
48 #define _COMPONENT      ACPI_BUS
49 ACPI_MODULE_NAME("RESOURCE")
50
51 struct lookup_irq_request {
52     ACPI_RESOURCE *acpi_res;
53     struct resource *res;
54     int         counter;
55     int         rid;
56     int         found;
57 };
58
59 static ACPI_STATUS
60 acpi_lookup_irq_handler(ACPI_RESOURCE *res, void *context)
61 {
62     struct lookup_irq_request *req;
63     size_t len;
64     u_int irqnum, irq;
65
66     switch (res->Type) {
67     case ACPI_RESOURCE_TYPE_IRQ:
68         irqnum = res->Data.Irq.InterruptCount;
69         irq = res->Data.Irq.Interrupts[0];
70         len = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
71         break;
72     case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
73         irqnum = res->Data.ExtendedIrq.InterruptCount;
74         irq = res->Data.ExtendedIrq.Interrupts[0];
75         len = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
76         break;
77     default:
78         return (AE_OK);
79     }
80     if (irqnum != 1)
81         return (AE_OK);
82     req = (struct lookup_irq_request *)context;
83     if (req->counter != req->rid) {
84         req->counter++;
85         return (AE_OK);
86     }
87     req->found = 1;
88     KASSERT(irq == rman_get_start(req->res),
89         ("IRQ resources do not match"));
90     bcopy(res, req->acpi_res, len);
91     return (AE_CTRL_TERMINATE);
92 }
93
94 ACPI_STATUS
95 acpi_lookup_irq_resource(device_t dev, int rid, struct resource *res,
96     ACPI_RESOURCE *acpi_res)
97 {
98     struct lookup_irq_request req;
99     ACPI_STATUS status;
100
101     req.acpi_res = acpi_res;
102     req.res = res;
103     req.counter = 0;
104     req.rid = rid;
105     req.found = 0;
106     status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
107         acpi_lookup_irq_handler, &req);
108     if (ACPI_SUCCESS(status) && req.found == 0)
109         status = AE_NOT_FOUND;
110     return (status);
111 }
112
113 void
114 acpi_config_intr(device_t dev, ACPI_RESOURCE *res)
115 {
116     u_int irq;
117     int pol, trig;
118
119     switch (res->Type) {
120     case ACPI_RESOURCE_TYPE_IRQ:
121         KASSERT(res->Data.Irq.InterruptCount == 1,
122             ("%s: multiple interrupts", __func__));
123         irq = res->Data.Irq.Interrupts[0];
124         trig = res->Data.Irq.Triggering;
125         pol = res->Data.Irq.Polarity;
126         break;
127     case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
128         KASSERT(res->Data.ExtendedIrq.InterruptCount == 1,
129             ("%s: multiple interrupts", __func__));
130         irq = res->Data.ExtendedIrq.Interrupts[0];
131         trig = res->Data.ExtendedIrq.Triggering;
132         pol = res->Data.ExtendedIrq.Polarity;
133         break;
134     default:
135         panic("%s: bad resource type %u", __func__, res->Type);
136     }
137
138 #if defined(__amd64__) || defined(__i386__)
139     /*
140      * XXX: Certain BIOSes have buggy AML that specify an IRQ that is
141      * edge-sensitive and active-lo.  However, edge-sensitive IRQs
142      * should be active-hi.  Force IRQs with an ISA IRQ value to be
143      * active-hi instead.
144      */
145     if (irq < 16 && trig == ACPI_EDGE_SENSITIVE && pol == ACPI_ACTIVE_LOW)
146         pol = ACPI_ACTIVE_HIGH;
147 #endif
148     BUS_CONFIG_INTR(dev, irq, (trig == ACPI_EDGE_SENSITIVE) ?
149         INTR_TRIGGER_EDGE : INTR_TRIGGER_LEVEL, (pol == ACPI_ACTIVE_HIGH) ?
150         INTR_POLARITY_HIGH : INTR_POLARITY_LOW);
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             printf("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);
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);
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 }
612
613 static void
614 acpi_res_set_ext_irq(device_t dev, void *context, u_int32_t *irq, int count,
615     int trig, int pol)
616 {
617     struct acpi_res_context     *cp = (struct acpi_res_context *)context;
618
619     if (cp == NULL || irq == NULL)
620         return;
621
622     /* This implements no resource relocation. */
623     if (count != 1)
624         return;
625
626     bus_set_resource(dev, SYS_RES_IRQ, cp->ar_nirq++, *irq, 1);
627 }
628
629 static void
630 acpi_res_set_drq(device_t dev, void *context, u_int8_t *drq, int count)
631 {
632     struct acpi_res_context     *cp = (struct acpi_res_context *)context;
633
634     if (cp == NULL || drq == NULL)
635         return;
636     
637     /* This implements no resource relocation. */
638     if (count != 1)
639         return;
640
641     bus_set_resource(dev, SYS_RES_DRQ, cp->ar_ndrq++, *drq, 1);
642 }
643
644 static void
645 acpi_res_set_start_dependent(device_t dev, void *context, int preference)
646 {
647     struct acpi_res_context     *cp = (struct acpi_res_context *)context;
648
649     if (cp == NULL)
650         return;
651     device_printf(dev, "dependent functions not supported\n");
652 }
653
654 static void
655 acpi_res_set_end_dependent(device_t dev, void *context)
656 {
657     struct acpi_res_context     *cp = (struct acpi_res_context *)context;
658
659     if (cp == NULL)
660         return;
661     device_printf(dev, "dependent functions not supported\n");
662 }
663
664 /*
665  * Resource-owning placeholders for IO and memory pseudo-devices.
666  *
667  * This code allocates system resources that will be used by ACPI
668  * child devices.  The acpi parent manages these resources through a
669  * private rman.
670  */
671
672 static int      acpi_sysres_rid = 100;
673
674 static int      acpi_sysres_probe(device_t dev);
675 static int      acpi_sysres_attach(device_t dev);
676
677 static device_method_t acpi_sysres_methods[] = {
678     /* Device interface */
679     DEVMETHOD(device_probe,     acpi_sysres_probe),
680     DEVMETHOD(device_attach,    acpi_sysres_attach),
681
682     {0, 0}
683 };
684
685 static driver_t acpi_sysres_driver = {
686     "acpi_sysresource",
687     acpi_sysres_methods,
688     0,
689 };
690
691 static devclass_t acpi_sysres_devclass;
692 DRIVER_MODULE(acpi_sysresource, acpi, acpi_sysres_driver, acpi_sysres_devclass,
693     0, 0);
694 MODULE_DEPEND(acpi_sysresource, acpi, 1, 1, 1);
695
696 static int
697 acpi_sysres_probe(device_t dev)
698 {
699     static char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL };
700
701     if (acpi_disabled("sysresource") ||
702         ACPI_ID_PROBE(device_get_parent(dev), dev, sysres_ids) == NULL)
703         return (ENXIO);
704
705     device_set_desc(dev, "System Resource");
706     device_quiet(dev);
707     return (BUS_PROBE_DEFAULT);
708 }
709
710 static int
711 acpi_sysres_attach(device_t dev)
712 {
713     device_t bus;
714     struct resource_list_entry *bus_rle, *dev_rle;
715     struct resource_list *bus_rl, *dev_rl;
716     int done, type;
717     u_long start, end, count;
718
719     /*
720      * Loop through all current resources to see if the new one overlaps
721      * any existing ones.  If so, grow the old one up and/or down
722      * accordingly.  Discard any that are wholly contained in the old.  If
723      * the resource is unique, add it to the parent.  It will later go into
724      * the rman pool.
725      */
726     bus = device_get_parent(dev);
727     dev_rl = BUS_GET_RESOURCE_LIST(bus, dev);
728     bus_rl = BUS_GET_RESOURCE_LIST(device_get_parent(bus), bus);
729     STAILQ_FOREACH(dev_rle, dev_rl, link) {
730         if (dev_rle->type != SYS_RES_IOPORT && dev_rle->type != SYS_RES_MEMORY)
731             continue;
732
733         start = dev_rle->start;
734         end = dev_rle->end;
735         count = dev_rle->count;
736         type = dev_rle->type;
737         done = FALSE;
738
739         STAILQ_FOREACH(bus_rle, bus_rl, link) {
740             if (bus_rle->type != type)
741                 continue;
742
743             /* New resource wholly contained in old, discard. */
744             if (start >= bus_rle->start && end <= bus_rle->end)
745                 break;
746
747             /* New tail overlaps old head, grow existing resource downward. */
748             if (start < bus_rle->start && end >= bus_rle->start) {
749                 bus_rle->count += bus_rle->start - start;
750                 bus_rle->start = start;
751                 done = TRUE;
752             }
753
754             /* New head overlaps old tail, grow existing resource upward. */
755             if (start <= bus_rle->end && end > bus_rle->end) {
756                 bus_rle->count += end - bus_rle->end;
757                 bus_rle->end = end;
758                 done = TRUE;
759             }
760
761             /* If we adjusted the old resource, we're finished. */
762             if (done)
763                 break;
764         }
765
766         /* If we didn't merge with anything, add this resource. */
767         if (bus_rle == NULL)
768             bus_set_resource(bus, type, acpi_sysres_rid++, start, count);
769     }
770
771     /* After merging/moving resources to the parent, free the list. */
772     resource_list_free(dev_rl);
773
774     return (0);
775 }