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