1 /* 2 * drivers/acpi/resource.c - ACPI device resources interpretation. 3 * 4 * Copyright (C) 2012, Intel Corp. 5 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com> 6 * 7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License version 2 as published 11 * by the Free Software Foundation. 12 * 13 * This program is distributed in the hope that it will be useful, but 14 * WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License along 19 * with this program; if not, write to the Free Software Foundation, Inc., 20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 21 * 22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 23 */ 24 25 #include <linux/acpi.h> 26 #include <linux/device.h> 27 #include <linux/export.h> 28 #include <linux/ioport.h> 29 #include <linux/list.h> 30 #include <linux/slab.h> 31 32 #ifdef CONFIG_X86 33 #define valid_IRQ(i) (((i) != 0) && ((i) != 2)) 34 #else 35 #define valid_IRQ(i) (true) 36 #endif 37 38 static bool acpi_dev_resource_len_valid(u64 start, u64 end, u64 len, bool io) 39 { 40 u64 reslen = end - start + 1; 41 42 /* 43 * CHECKME: len might be required to check versus a minimum 44 * length as well. 1 for io is fine, but for memory it does 45 * not make any sense at all. 46 * Note: some BIOSes report incorrect length for ACPI address space 47 * descriptor, so remove check of 'reslen == len' to avoid regression. 48 */ 49 if (len && reslen && start <= end) 50 return true; 51 52 pr_debug("ACPI: invalid or unassigned resource %s [%016llx - %016llx] length [%016llx]\n", 53 io ? "io" : "mem", start, end, len); 54 55 return false; 56 } 57 58 static void acpi_dev_memresource_flags(struct resource *res, u64 len, 59 u8 write_protect) 60 { 61 res->flags = IORESOURCE_MEM; 62 63 if (!acpi_dev_resource_len_valid(res->start, res->end, len, false)) 64 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET; 65 66 if (write_protect == ACPI_READ_WRITE_MEMORY) 67 res->flags |= IORESOURCE_MEM_WRITEABLE; 68 } 69 70 static void acpi_dev_get_memresource(struct resource *res, u64 start, u64 len, 71 u8 write_protect) 72 { 73 res->start = start; 74 res->end = start + len - 1; 75 acpi_dev_memresource_flags(res, len, write_protect); 76 } 77 78 /** 79 * acpi_dev_resource_memory - Extract ACPI memory resource information. 80 * @ares: Input ACPI resource object. 81 * @res: Output generic resource object. 82 * 83 * Check if the given ACPI resource object represents a memory resource and 84 * if that's the case, use the information in it to populate the generic 85 * resource object pointed to by @res. 86 * 87 * Return: 88 * 1) false with res->flags setting to zero: not the expected resource type 89 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource 90 * 3) true: valid assigned resource 91 */ 92 bool acpi_dev_resource_memory(struct acpi_resource *ares, struct resource *res) 93 { 94 struct acpi_resource_memory24 *memory24; 95 struct acpi_resource_memory32 *memory32; 96 struct acpi_resource_fixed_memory32 *fixed_memory32; 97 98 switch (ares->type) { 99 case ACPI_RESOURCE_TYPE_MEMORY24: 100 memory24 = &ares->data.memory24; 101 acpi_dev_get_memresource(res, memory24->minimum << 8, 102 memory24->address_length << 8, 103 memory24->write_protect); 104 break; 105 case ACPI_RESOURCE_TYPE_MEMORY32: 106 memory32 = &ares->data.memory32; 107 acpi_dev_get_memresource(res, memory32->minimum, 108 memory32->address_length, 109 memory32->write_protect); 110 break; 111 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32: 112 fixed_memory32 = &ares->data.fixed_memory32; 113 acpi_dev_get_memresource(res, fixed_memory32->address, 114 fixed_memory32->address_length, 115 fixed_memory32->write_protect); 116 break; 117 default: 118 res->flags = 0; 119 return false; 120 } 121 122 return !(res->flags & IORESOURCE_DISABLED); 123 } 124 EXPORT_SYMBOL_GPL(acpi_dev_resource_memory); 125 126 static void acpi_dev_ioresource_flags(struct resource *res, u64 len, 127 u8 io_decode) 128 { 129 res->flags = IORESOURCE_IO; 130 131 if (!acpi_dev_resource_len_valid(res->start, res->end, len, true)) 132 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET; 133 134 if (res->end >= 0x10003) 135 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET; 136 137 if (io_decode == ACPI_DECODE_16) 138 res->flags |= IORESOURCE_IO_16BIT_ADDR; 139 } 140 141 static void acpi_dev_get_ioresource(struct resource *res, u64 start, u64 len, 142 u8 io_decode) 143 { 144 res->start = start; 145 res->end = start + len - 1; 146 acpi_dev_ioresource_flags(res, len, io_decode); 147 } 148 149 /** 150 * acpi_dev_resource_io - Extract ACPI I/O resource information. 151 * @ares: Input ACPI resource object. 152 * @res: Output generic resource object. 153 * 154 * Check if the given ACPI resource object represents an I/O resource and 155 * if that's the case, use the information in it to populate the generic 156 * resource object pointed to by @res. 157 * 158 * Return: 159 * 1) false with res->flags setting to zero: not the expected resource type 160 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource 161 * 3) true: valid assigned resource 162 */ 163 bool acpi_dev_resource_io(struct acpi_resource *ares, struct resource *res) 164 { 165 struct acpi_resource_io *io; 166 struct acpi_resource_fixed_io *fixed_io; 167 168 switch (ares->type) { 169 case ACPI_RESOURCE_TYPE_IO: 170 io = &ares->data.io; 171 acpi_dev_get_ioresource(res, io->minimum, 172 io->address_length, 173 io->io_decode); 174 break; 175 case ACPI_RESOURCE_TYPE_FIXED_IO: 176 fixed_io = &ares->data.fixed_io; 177 acpi_dev_get_ioresource(res, fixed_io->address, 178 fixed_io->address_length, 179 ACPI_DECODE_10); 180 break; 181 default: 182 res->flags = 0; 183 return false; 184 } 185 186 return !(res->flags & IORESOURCE_DISABLED); 187 } 188 EXPORT_SYMBOL_GPL(acpi_dev_resource_io); 189 190 static bool acpi_decode_space(struct resource_win *win, 191 struct acpi_resource_address *addr, 192 struct acpi_address64_attribute *attr) 193 { 194 u8 iodec = attr->granularity == 0xfff ? ACPI_DECODE_10 : ACPI_DECODE_16; 195 bool wp = addr->info.mem.write_protect; 196 u64 len = attr->address_length; 197 struct resource *res = &win->res; 198 199 /* 200 * Filter out invalid descriptor according to ACPI Spec 5.0, section 201 * 6.4.3.5 Address Space Resource Descriptors. 202 */ 203 if ((addr->min_address_fixed != addr->max_address_fixed && len) || 204 (addr->min_address_fixed && addr->max_address_fixed && !len)) 205 pr_debug("ACPI: Invalid address space min_addr_fix %d, max_addr_fix %d, len %llx\n", 206 addr->min_address_fixed, addr->max_address_fixed, len); 207 208 res->start = attr->minimum; 209 res->end = attr->maximum; 210 211 /* 212 * For bridges that translate addresses across the bridge, 213 * translation_offset is the offset that must be added to the 214 * address on the secondary side to obtain the address on the 215 * primary side. Non-bridge devices must list 0 for all Address 216 * Translation offset bits. 217 */ 218 if (addr->producer_consumer == ACPI_PRODUCER) { 219 res->start += attr->translation_offset; 220 res->end += attr->translation_offset; 221 } else if (attr->translation_offset) { 222 pr_debug("ACPI: translation_offset(%lld) is invalid for non-bridge device.\n", 223 attr->translation_offset); 224 } 225 226 switch (addr->resource_type) { 227 case ACPI_MEMORY_RANGE: 228 acpi_dev_memresource_flags(res, len, wp); 229 break; 230 case ACPI_IO_RANGE: 231 acpi_dev_ioresource_flags(res, len, iodec); 232 break; 233 case ACPI_BUS_NUMBER_RANGE: 234 res->flags = IORESOURCE_BUS; 235 break; 236 default: 237 return false; 238 } 239 240 win->offset = attr->translation_offset; 241 242 if (addr->producer_consumer == ACPI_PRODUCER) 243 res->flags |= IORESOURCE_WINDOW; 244 245 if (addr->info.mem.caching == ACPI_PREFETCHABLE_MEMORY) 246 res->flags |= IORESOURCE_PREFETCH; 247 248 return !(res->flags & IORESOURCE_DISABLED); 249 } 250 251 /** 252 * acpi_dev_resource_address_space - Extract ACPI address space information. 253 * @ares: Input ACPI resource object. 254 * @win: Output generic resource object. 255 * 256 * Check if the given ACPI resource object represents an address space resource 257 * and if that's the case, use the information in it to populate the generic 258 * resource object pointed to by @win. 259 * 260 * Return: 261 * 1) false with win->res.flags setting to zero: not the expected resource type 262 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned 263 * resource 264 * 3) true: valid assigned resource 265 */ 266 bool acpi_dev_resource_address_space(struct acpi_resource *ares, 267 struct resource_win *win) 268 { 269 struct acpi_resource_address64 addr; 270 271 win->res.flags = 0; 272 if (ACPI_FAILURE(acpi_resource_to_address64(ares, &addr))) 273 return false; 274 275 return acpi_decode_space(win, (struct acpi_resource_address *)&addr, 276 &addr.address); 277 } 278 EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space); 279 280 /** 281 * acpi_dev_resource_ext_address_space - Extract ACPI address space information. 282 * @ares: Input ACPI resource object. 283 * @win: Output generic resource object. 284 * 285 * Check if the given ACPI resource object represents an extended address space 286 * resource and if that's the case, use the information in it to populate the 287 * generic resource object pointed to by @win. 288 * 289 * Return: 290 * 1) false with win->res.flags setting to zero: not the expected resource type 291 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned 292 * resource 293 * 3) true: valid assigned resource 294 */ 295 bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares, 296 struct resource_win *win) 297 { 298 struct acpi_resource_extended_address64 *ext_addr; 299 300 win->res.flags = 0; 301 if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64) 302 return false; 303 304 ext_addr = &ares->data.ext_address64; 305 306 return acpi_decode_space(win, (struct acpi_resource_address *)ext_addr, 307 &ext_addr->address); 308 } 309 EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space); 310 311 /** 312 * acpi_dev_irq_flags - Determine IRQ resource flags. 313 * @triggering: Triggering type as provided by ACPI. 314 * @polarity: Interrupt polarity as provided by ACPI. 315 * @shareable: Whether or not the interrupt is shareable. 316 */ 317 unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable) 318 { 319 unsigned long flags; 320 321 if (triggering == ACPI_LEVEL_SENSITIVE) 322 flags = polarity == ACPI_ACTIVE_LOW ? 323 IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL; 324 else 325 flags = polarity == ACPI_ACTIVE_LOW ? 326 IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE; 327 328 if (shareable == ACPI_SHARED) 329 flags |= IORESOURCE_IRQ_SHAREABLE; 330 331 return flags | IORESOURCE_IRQ; 332 } 333 EXPORT_SYMBOL_GPL(acpi_dev_irq_flags); 334 335 static void acpi_dev_irqresource_disabled(struct resource *res, u32 gsi) 336 { 337 res->start = gsi; 338 res->end = gsi; 339 res->flags = IORESOURCE_IRQ | IORESOURCE_DISABLED | IORESOURCE_UNSET; 340 } 341 342 static void acpi_dev_get_irqresource(struct resource *res, u32 gsi, 343 u8 triggering, u8 polarity, u8 shareable, 344 bool legacy) 345 { 346 int irq, p, t; 347 348 if (!valid_IRQ(gsi)) { 349 acpi_dev_irqresource_disabled(res, gsi); 350 return; 351 } 352 353 /* 354 * In IO-APIC mode, use overrided attribute. Two reasons: 355 * 1. BIOS bug in DSDT 356 * 2. BIOS uses IO-APIC mode Interrupt Source Override 357 * 358 * We do this only if we are dealing with IRQ() or IRQNoFlags() 359 * resource (the legacy ISA resources). With modern ACPI 5 devices 360 * using extended IRQ descriptors we take the IRQ configuration 361 * from _CRS directly. 362 */ 363 if (legacy && !acpi_get_override_irq(gsi, &t, &p)) { 364 u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE; 365 u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH; 366 367 if (triggering != trig || polarity != pol) { 368 pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi, 369 t ? "level" : "edge", p ? "low" : "high"); 370 triggering = trig; 371 polarity = pol; 372 } 373 } 374 375 res->flags = acpi_dev_irq_flags(triggering, polarity, shareable); 376 irq = acpi_register_gsi(NULL, gsi, triggering, polarity); 377 if (irq >= 0) { 378 res->start = irq; 379 res->end = irq; 380 } else { 381 acpi_dev_irqresource_disabled(res, gsi); 382 } 383 } 384 385 /** 386 * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information. 387 * @ares: Input ACPI resource object. 388 * @index: Index into the array of GSIs represented by the resource. 389 * @res: Output generic resource object. 390 * 391 * Check if the given ACPI resource object represents an interrupt resource 392 * and @index does not exceed the resource's interrupt count (true is returned 393 * in that case regardless of the results of the other checks)). If that's the 394 * case, register the GSI corresponding to @index from the array of interrupts 395 * represented by the resource and populate the generic resource object pointed 396 * to by @res accordingly. If the registration of the GSI is not successful, 397 * IORESOURCE_DISABLED will be set it that object's flags. 398 * 399 * Return: 400 * 1) false with res->flags setting to zero: not the expected resource type 401 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource 402 * 3) true: valid assigned resource 403 */ 404 bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index, 405 struct resource *res) 406 { 407 struct acpi_resource_irq *irq; 408 struct acpi_resource_extended_irq *ext_irq; 409 410 switch (ares->type) { 411 case ACPI_RESOURCE_TYPE_IRQ: 412 /* 413 * Per spec, only one interrupt per descriptor is allowed in 414 * _CRS, but some firmware violates this, so parse them all. 415 */ 416 irq = &ares->data.irq; 417 if (index >= irq->interrupt_count) { 418 acpi_dev_irqresource_disabled(res, 0); 419 return false; 420 } 421 acpi_dev_get_irqresource(res, irq->interrupts[index], 422 irq->triggering, irq->polarity, 423 irq->sharable, true); 424 break; 425 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 426 ext_irq = &ares->data.extended_irq; 427 if (index >= ext_irq->interrupt_count) { 428 acpi_dev_irqresource_disabled(res, 0); 429 return false; 430 } 431 acpi_dev_get_irqresource(res, ext_irq->interrupts[index], 432 ext_irq->triggering, ext_irq->polarity, 433 ext_irq->sharable, false); 434 break; 435 default: 436 res->flags = 0; 437 return false; 438 } 439 440 return true; 441 } 442 EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt); 443 444 /** 445 * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources(). 446 * @list: The head of the resource list to free. 447 */ 448 void acpi_dev_free_resource_list(struct list_head *list) 449 { 450 resource_list_free(list); 451 } 452 EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list); 453 454 struct res_proc_context { 455 struct list_head *list; 456 int (*preproc)(struct acpi_resource *, void *); 457 void *preproc_data; 458 int count; 459 int error; 460 }; 461 462 static acpi_status acpi_dev_new_resource_entry(struct resource_win *win, 463 struct res_proc_context *c) 464 { 465 struct resource_entry *rentry; 466 467 rentry = resource_list_create_entry(NULL, 0); 468 if (!rentry) { 469 c->error = -ENOMEM; 470 return AE_NO_MEMORY; 471 } 472 *rentry->res = win->res; 473 rentry->offset = win->offset; 474 resource_list_add_tail(rentry, c->list); 475 c->count++; 476 return AE_OK; 477 } 478 479 static acpi_status acpi_dev_process_resource(struct acpi_resource *ares, 480 void *context) 481 { 482 struct res_proc_context *c = context; 483 struct resource_win win; 484 struct resource *res = &win.res; 485 int i; 486 487 if (c->preproc) { 488 int ret; 489 490 ret = c->preproc(ares, c->preproc_data); 491 if (ret < 0) { 492 c->error = ret; 493 return AE_CTRL_TERMINATE; 494 } else if (ret > 0) { 495 return AE_OK; 496 } 497 } 498 499 memset(&win, 0, sizeof(win)); 500 501 if (acpi_dev_resource_memory(ares, res) 502 || acpi_dev_resource_io(ares, res) 503 || acpi_dev_resource_address_space(ares, &win) 504 || acpi_dev_resource_ext_address_space(ares, &win)) 505 return acpi_dev_new_resource_entry(&win, c); 506 507 for (i = 0; acpi_dev_resource_interrupt(ares, i, res); i++) { 508 acpi_status status; 509 510 status = acpi_dev_new_resource_entry(&win, c); 511 if (ACPI_FAILURE(status)) 512 return status; 513 } 514 515 return AE_OK; 516 } 517 518 /** 519 * acpi_dev_get_resources - Get current resources of a device. 520 * @adev: ACPI device node to get the resources for. 521 * @list: Head of the resultant list of resources (must be empty). 522 * @preproc: The caller's preprocessing routine. 523 * @preproc_data: Pointer passed to the caller's preprocessing routine. 524 * 525 * Evaluate the _CRS method for the given device node and process its output by 526 * (1) executing the @preproc() rountine provided by the caller, passing the 527 * resource pointer and @preproc_data to it as arguments, for each ACPI resource 528 * returned and (2) converting all of the returned ACPI resources into struct 529 * resource objects if possible. If the return value of @preproc() in step (1) 530 * is different from 0, step (2) is not applied to the given ACPI resource and 531 * if that value is negative, the whole processing is aborted and that value is 532 * returned as the final error code. 533 * 534 * The resultant struct resource objects are put on the list pointed to by 535 * @list, that must be empty initially, as members of struct resource_entry 536 * objects. Callers of this routine should use %acpi_dev_free_resource_list() to 537 * free that list. 538 * 539 * The number of resources in the output list is returned on success, an error 540 * code reflecting the error condition is returned otherwise. 541 */ 542 int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list, 543 int (*preproc)(struct acpi_resource *, void *), 544 void *preproc_data) 545 { 546 struct res_proc_context c; 547 acpi_status status; 548 549 if (!adev || !adev->handle || !list_empty(list)) 550 return -EINVAL; 551 552 if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) 553 return 0; 554 555 c.list = list; 556 c.preproc = preproc; 557 c.preproc_data = preproc_data; 558 c.count = 0; 559 c.error = 0; 560 status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS, 561 acpi_dev_process_resource, &c); 562 if (ACPI_FAILURE(status)) { 563 acpi_dev_free_resource_list(list); 564 return c.error ? c.error : -EIO; 565 } 566 567 return c.count; 568 } 569 EXPORT_SYMBOL_GPL(acpi_dev_get_resources); 570 571 /** 572 * acpi_dev_filter_resource_type - Filter ACPI resource according to resource 573 * types 574 * @ares: Input ACPI resource object. 575 * @types: Valid resource types of IORESOURCE_XXX 576 * 577 * This is a helper function to support acpi_dev_get_resources(), which filters 578 * ACPI resource objects according to resource types. 579 */ 580 int acpi_dev_filter_resource_type(struct acpi_resource *ares, 581 unsigned long types) 582 { 583 unsigned long type = 0; 584 585 switch (ares->type) { 586 case ACPI_RESOURCE_TYPE_MEMORY24: 587 case ACPI_RESOURCE_TYPE_MEMORY32: 588 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32: 589 type = IORESOURCE_MEM; 590 break; 591 case ACPI_RESOURCE_TYPE_IO: 592 case ACPI_RESOURCE_TYPE_FIXED_IO: 593 type = IORESOURCE_IO; 594 break; 595 case ACPI_RESOURCE_TYPE_IRQ: 596 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 597 type = IORESOURCE_IRQ; 598 break; 599 case ACPI_RESOURCE_TYPE_DMA: 600 case ACPI_RESOURCE_TYPE_FIXED_DMA: 601 type = IORESOURCE_DMA; 602 break; 603 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER: 604 type = IORESOURCE_REG; 605 break; 606 case ACPI_RESOURCE_TYPE_ADDRESS16: 607 case ACPI_RESOURCE_TYPE_ADDRESS32: 608 case ACPI_RESOURCE_TYPE_ADDRESS64: 609 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64: 610 if (ares->data.address.resource_type == ACPI_MEMORY_RANGE) 611 type = IORESOURCE_MEM; 612 else if (ares->data.address.resource_type == ACPI_IO_RANGE) 613 type = IORESOURCE_IO; 614 else if (ares->data.address.resource_type == 615 ACPI_BUS_NUMBER_RANGE) 616 type = IORESOURCE_BUS; 617 break; 618 default: 619 break; 620 } 621 622 return (type & types) ? 0 : 1; 623 } 624 EXPORT_SYMBOL_GPL(acpi_dev_filter_resource_type); 625 626 struct reserved_region { 627 struct list_head node; 628 u64 start; 629 u64 end; 630 }; 631 632 static LIST_HEAD(reserved_io_regions); 633 static LIST_HEAD(reserved_mem_regions); 634 635 static int request_range(u64 start, u64 end, u8 space_id, unsigned long flags, 636 char *desc) 637 { 638 unsigned int length = end - start + 1; 639 struct resource *res; 640 641 res = space_id == ACPI_ADR_SPACE_SYSTEM_IO ? 642 request_region(start, length, desc) : 643 request_mem_region(start, length, desc); 644 if (!res) 645 return -EIO; 646 647 res->flags &= ~flags; 648 return 0; 649 } 650 651 static int add_region_before(u64 start, u64 end, u8 space_id, 652 unsigned long flags, char *desc, 653 struct list_head *head) 654 { 655 struct reserved_region *reg; 656 int error; 657 658 reg = kmalloc(sizeof(*reg), GFP_KERNEL); 659 if (!reg) 660 return -ENOMEM; 661 662 error = request_range(start, end, space_id, flags, desc); 663 if (error) { 664 kfree(reg); 665 return error; 666 } 667 668 reg->start = start; 669 reg->end = end; 670 list_add_tail(®->node, head); 671 return 0; 672 } 673 674 /** 675 * acpi_reserve_region - Reserve an I/O or memory region as a system resource. 676 * @start: Starting address of the region. 677 * @length: Length of the region. 678 * @space_id: Identifier of address space to reserve the region from. 679 * @flags: Resource flags to clear for the region after requesting it. 680 * @desc: Region description (for messages). 681 * 682 * Reserve an I/O or memory region as a system resource to prevent others from 683 * using it. If the new region overlaps with one of the regions (in the given 684 * address space) already reserved by this routine, only the non-overlapping 685 * parts of it will be reserved. 686 * 687 * Returned is either 0 (success) or a negative error code indicating a resource 688 * reservation problem. It is the code of the first encountered error, but the 689 * routine doesn't abort until it has attempted to request all of the parts of 690 * the new region that don't overlap with other regions reserved previously. 691 * 692 * The resources requested by this routine are never released. 693 */ 694 int acpi_reserve_region(u64 start, unsigned int length, u8 space_id, 695 unsigned long flags, char *desc) 696 { 697 struct list_head *regions; 698 struct reserved_region *reg; 699 u64 end = start + length - 1; 700 int ret = 0, error = 0; 701 702 if (space_id == ACPI_ADR_SPACE_SYSTEM_IO) 703 regions = &reserved_io_regions; 704 else if (space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) 705 regions = &reserved_mem_regions; 706 else 707 return -EINVAL; 708 709 if (list_empty(regions)) 710 return add_region_before(start, end, space_id, flags, desc, regions); 711 712 list_for_each_entry(reg, regions, node) 713 if (reg->start == end + 1) { 714 /* The new region can be prepended to this one. */ 715 ret = request_range(start, end, space_id, flags, desc); 716 if (!ret) 717 reg->start = start; 718 719 return ret; 720 } else if (reg->start > end) { 721 /* No overlap. Add the new region here and get out. */ 722 return add_region_before(start, end, space_id, flags, 723 desc, ®->node); 724 } else if (reg->end == start - 1) { 725 goto combine; 726 } else if (reg->end >= start) { 727 goto overlap; 728 } 729 730 /* The new region goes after the last existing one. */ 731 return add_region_before(start, end, space_id, flags, desc, regions); 732 733 overlap: 734 /* 735 * The new region overlaps an existing one. 736 * 737 * The head part of the new region immediately preceding the existing 738 * overlapping one can be combined with it right away. 739 */ 740 if (reg->start > start) { 741 error = request_range(start, reg->start - 1, space_id, flags, desc); 742 if (error) 743 ret = error; 744 else 745 reg->start = start; 746 } 747 748 combine: 749 /* 750 * The new region is adjacent to an existing one. If it extends beyond 751 * that region all the way to the next one, it is possible to combine 752 * all three of them. 753 */ 754 while (reg->end < end) { 755 struct reserved_region *next = NULL; 756 u64 a = reg->end + 1, b = end; 757 758 if (!list_is_last(®->node, regions)) { 759 next = list_next_entry(reg, node); 760 if (next->start <= end) 761 b = next->start - 1; 762 } 763 error = request_range(a, b, space_id, flags, desc); 764 if (!error) { 765 if (next && next->start == b + 1) { 766 reg->end = next->end; 767 list_del(&next->node); 768 kfree(next); 769 } else { 770 reg->end = end; 771 break; 772 } 773 } else if (next) { 774 if (!ret) 775 ret = error; 776 777 reg = next; 778 } else { 779 break; 780 } 781 } 782 783 return ret ? ret : error; 784 } 785 EXPORT_SYMBOL_GPL(acpi_reserve_region); 786