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