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