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