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