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