Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 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 | * |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 18 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 19 | */ |
| 20 | |
| 21 | #include <linux/acpi.h> |
| 22 | #include <linux/device.h> |
| 23 | #include <linux/export.h> |
| 24 | #include <linux/ioport.h> |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 25 | #include <linux/slab.h> |
Christophe RICARD | 55a9341 | 2015-12-23 23:25:33 +0100 | [diff] [blame] | 26 | #include <linux/irq.h> |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 27 | |
| 28 | #ifdef CONFIG_X86 |
| 29 | #define valid_IRQ(i) (((i) != 0) && ((i) != 2)) |
Lorenzo Pieralisi | 4a2e7aa | 2016-03-21 11:12:55 +0000 | [diff] [blame] | 30 | static inline bool acpi_iospace_resource_valid(struct resource *res) |
| 31 | { |
| 32 | /* On X86 IO space is limited to the [0 - 64K] IO port range */ |
| 33 | return res->end < 0x10003; |
| 34 | } |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 35 | #else |
| 36 | #define valid_IRQ(i) (true) |
Lorenzo Pieralisi | 4a2e7aa | 2016-03-21 11:12:55 +0000 | [diff] [blame] | 37 | /* |
| 38 | * ACPI IO descriptors on arches other than X86 contain MMIO CPU physical |
| 39 | * addresses mapping IO space in CPU physical address space, IO space |
| 40 | * resources can be placed anywhere in the 64-bit physical address space. |
| 41 | */ |
| 42 | static inline bool |
| 43 | acpi_iospace_resource_valid(struct resource *res) { return true; } |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 44 | #endif |
| 45 | |
Agustin Vega-Frias | fa20b17 | 2017-02-02 18:23:57 -0500 | [diff] [blame] | 46 | #if IS_ENABLED(CONFIG_ACPI_GENERIC_GSI) |
| 47 | static inline bool is_gsi(struct acpi_resource_extended_irq *ext_irq) |
| 48 | { |
| 49 | return ext_irq->resource_source.string_length == 0 && |
| 50 | ext_irq->producer_consumer == ACPI_CONSUMER; |
| 51 | } |
| 52 | #else |
| 53 | static inline bool is_gsi(struct acpi_resource_extended_irq *ext_irq) |
| 54 | { |
| 55 | return true; |
| 56 | } |
| 57 | #endif |
| 58 | |
Thomas Gleixner | c420dbd | 2015-02-02 10:42:48 +0800 | [diff] [blame] | 59 | static bool acpi_dev_resource_len_valid(u64 start, u64 end, u64 len, bool io) |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 60 | { |
Thomas Gleixner | c420dbd | 2015-02-02 10:42:48 +0800 | [diff] [blame] | 61 | u64 reslen = end - start + 1; |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 62 | |
Thomas Gleixner | c420dbd | 2015-02-02 10:42:48 +0800 | [diff] [blame] | 63 | /* |
| 64 | * CHECKME: len might be required to check versus a minimum |
| 65 | * length as well. 1 for io is fine, but for memory it does |
| 66 | * not make any sense at all. |
Jiang Liu | aa714d2 | 2015-03-04 16:47:12 +0800 | [diff] [blame] | 67 | * Note: some BIOSes report incorrect length for ACPI address space |
| 68 | * descriptor, so remove check of 'reslen == len' to avoid regression. |
Thomas Gleixner | c420dbd | 2015-02-02 10:42:48 +0800 | [diff] [blame] | 69 | */ |
Jiang Liu | aa714d2 | 2015-03-04 16:47:12 +0800 | [diff] [blame] | 70 | if (len && reslen && start <= end) |
Thomas Gleixner | c420dbd | 2015-02-02 10:42:48 +0800 | [diff] [blame] | 71 | return true; |
| 72 | |
Rafael J. Wysocki | 6a239af | 2015-02-18 08:05:43 +0100 | [diff] [blame] | 73 | pr_debug("ACPI: invalid or unassigned resource %s [%016llx - %016llx] length [%016llx]\n", |
Thomas Gleixner | c420dbd | 2015-02-02 10:42:48 +0800 | [diff] [blame] | 74 | io ? "io" : "mem", start, end, len); |
| 75 | |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | static void acpi_dev_memresource_flags(struct resource *res, u64 len, |
Thomas Gleixner | 72e26b0 | 2015-02-02 10:42:52 +0800 | [diff] [blame] | 80 | u8 write_protect) |
Thomas Gleixner | c420dbd | 2015-02-02 10:42:48 +0800 | [diff] [blame] | 81 | { |
| 82 | res->flags = IORESOURCE_MEM; |
| 83 | |
| 84 | if (!acpi_dev_resource_len_valid(res->start, res->end, len, false)) |
Jiang Liu | c78b688 | 2015-02-02 10:42:56 +0800 | [diff] [blame] | 85 | res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET; |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 86 | |
| 87 | if (write_protect == ACPI_READ_WRITE_MEMORY) |
Thomas Gleixner | c420dbd | 2015-02-02 10:42:48 +0800 | [diff] [blame] | 88 | res->flags |= IORESOURCE_MEM_WRITEABLE; |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static void acpi_dev_get_memresource(struct resource *res, u64 start, u64 len, |
| 92 | u8 write_protect) |
| 93 | { |
| 94 | res->start = start; |
| 95 | res->end = start + len - 1; |
Thomas Gleixner | 72e26b0 | 2015-02-02 10:42:52 +0800 | [diff] [blame] | 96 | acpi_dev_memresource_flags(res, len, write_protect); |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
| 100 | * acpi_dev_resource_memory - Extract ACPI memory resource information. |
| 101 | * @ares: Input ACPI resource object. |
| 102 | * @res: Output generic resource object. |
| 103 | * |
| 104 | * Check if the given ACPI resource object represents a memory resource and |
| 105 | * if that's the case, use the information in it to populate the generic |
| 106 | * resource object pointed to by @res. |
Jiang Liu | 581c19f | 2015-02-02 10:42:55 +0800 | [diff] [blame] | 107 | * |
| 108 | * Return: |
| 109 | * 1) false with res->flags setting to zero: not the expected resource type |
| 110 | * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource |
| 111 | * 3) true: valid assigned resource |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 112 | */ |
| 113 | bool acpi_dev_resource_memory(struct acpi_resource *ares, struct resource *res) |
| 114 | { |
| 115 | struct acpi_resource_memory24 *memory24; |
| 116 | struct acpi_resource_memory32 *memory32; |
| 117 | struct acpi_resource_fixed_memory32 *fixed_memory32; |
| 118 | |
| 119 | switch (ares->type) { |
| 120 | case ACPI_RESOURCE_TYPE_MEMORY24: |
| 121 | memory24 = &ares->data.memory24; |
Jiang Liu | 8515f93 | 2015-02-02 10:42:54 +0800 | [diff] [blame] | 122 | acpi_dev_get_memresource(res, memory24->minimum << 8, |
| 123 | memory24->address_length << 8, |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 124 | memory24->write_protect); |
| 125 | break; |
| 126 | case ACPI_RESOURCE_TYPE_MEMORY32: |
| 127 | memory32 = &ares->data.memory32; |
| 128 | acpi_dev_get_memresource(res, memory32->minimum, |
| 129 | memory32->address_length, |
| 130 | memory32->write_protect); |
| 131 | break; |
| 132 | case ACPI_RESOURCE_TYPE_FIXED_MEMORY32: |
| 133 | fixed_memory32 = &ares->data.fixed_memory32; |
| 134 | acpi_dev_get_memresource(res, fixed_memory32->address, |
| 135 | fixed_memory32->address_length, |
| 136 | fixed_memory32->write_protect); |
| 137 | break; |
| 138 | default: |
Jiang Liu | 581c19f | 2015-02-02 10:42:55 +0800 | [diff] [blame] | 139 | res->flags = 0; |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 140 | return false; |
| 141 | } |
Thomas Gleixner | c420dbd | 2015-02-02 10:42:48 +0800 | [diff] [blame] | 142 | |
| 143 | return !(res->flags & IORESOURCE_DISABLED); |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 144 | } |
| 145 | EXPORT_SYMBOL_GPL(acpi_dev_resource_memory); |
| 146 | |
Thomas Gleixner | 1d0420f | 2015-02-02 10:42:49 +0800 | [diff] [blame] | 147 | static void acpi_dev_ioresource_flags(struct resource *res, u64 len, |
Jiang Liu | 91236ec | 2015-10-14 14:29:36 +0800 | [diff] [blame] | 148 | u8 io_decode, u8 translation_type) |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 149 | { |
Thomas Gleixner | 1d0420f | 2015-02-02 10:42:49 +0800 | [diff] [blame] | 150 | res->flags = IORESOURCE_IO; |
| 151 | |
| 152 | if (!acpi_dev_resource_len_valid(res->start, res->end, len, true)) |
Jiang Liu | c78b688 | 2015-02-02 10:42:56 +0800 | [diff] [blame] | 153 | res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET; |
Thomas Gleixner | 1d0420f | 2015-02-02 10:42:49 +0800 | [diff] [blame] | 154 | |
Lorenzo Pieralisi | 4a2e7aa | 2016-03-21 11:12:55 +0000 | [diff] [blame] | 155 | if (!acpi_iospace_resource_valid(res)) |
Jiang Liu | c78b688 | 2015-02-02 10:42:56 +0800 | [diff] [blame] | 156 | res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET; |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 157 | |
| 158 | if (io_decode == ACPI_DECODE_16) |
Thomas Gleixner | 1d0420f | 2015-02-02 10:42:49 +0800 | [diff] [blame] | 159 | res->flags |= IORESOURCE_IO_16BIT_ADDR; |
Jiang Liu | 91236ec | 2015-10-14 14:29:36 +0800 | [diff] [blame] | 160 | if (translation_type == ACPI_SPARSE_TRANSLATION) |
| 161 | res->flags |= IORESOURCE_IO_SPARSE; |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static void acpi_dev_get_ioresource(struct resource *res, u64 start, u64 len, |
| 165 | u8 io_decode) |
| 166 | { |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 167 | res->start = start; |
Thomas Gleixner | 1d0420f | 2015-02-02 10:42:49 +0800 | [diff] [blame] | 168 | res->end = start + len - 1; |
Jiang Liu | 91236ec | 2015-10-14 14:29:36 +0800 | [diff] [blame] | 169 | acpi_dev_ioresource_flags(res, len, io_decode, 0); |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /** |
| 173 | * acpi_dev_resource_io - Extract ACPI I/O resource information. |
| 174 | * @ares: Input ACPI resource object. |
| 175 | * @res: Output generic resource object. |
| 176 | * |
| 177 | * Check if the given ACPI resource object represents an I/O resource and |
| 178 | * if that's the case, use the information in it to populate the generic |
| 179 | * resource object pointed to by @res. |
Jiang Liu | 581c19f | 2015-02-02 10:42:55 +0800 | [diff] [blame] | 180 | * |
| 181 | * Return: |
| 182 | * 1) false with res->flags setting to zero: not the expected resource type |
| 183 | * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource |
| 184 | * 3) true: valid assigned resource |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 185 | */ |
| 186 | bool acpi_dev_resource_io(struct acpi_resource *ares, struct resource *res) |
| 187 | { |
| 188 | struct acpi_resource_io *io; |
| 189 | struct acpi_resource_fixed_io *fixed_io; |
| 190 | |
| 191 | switch (ares->type) { |
| 192 | case ACPI_RESOURCE_TYPE_IO: |
| 193 | io = &ares->data.io; |
| 194 | acpi_dev_get_ioresource(res, io->minimum, |
| 195 | io->address_length, |
| 196 | io->io_decode); |
| 197 | break; |
| 198 | case ACPI_RESOURCE_TYPE_FIXED_IO: |
| 199 | fixed_io = &ares->data.fixed_io; |
| 200 | acpi_dev_get_ioresource(res, fixed_io->address, |
| 201 | fixed_io->address_length, |
| 202 | ACPI_DECODE_10); |
| 203 | break; |
| 204 | default: |
Jiang Liu | 581c19f | 2015-02-02 10:42:55 +0800 | [diff] [blame] | 205 | res->flags = 0; |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 206 | return false; |
| 207 | } |
Thomas Gleixner | 1d0420f | 2015-02-02 10:42:49 +0800 | [diff] [blame] | 208 | |
| 209 | return !(res->flags & IORESOURCE_DISABLED); |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 210 | } |
| 211 | EXPORT_SYMBOL_GPL(acpi_dev_resource_io); |
| 212 | |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 213 | static bool acpi_decode_space(struct resource_win *win, |
Thomas Gleixner | eb76d55 | 2015-02-02 10:42:51 +0800 | [diff] [blame] | 214 | struct acpi_resource_address *addr, |
| 215 | struct acpi_address64_attribute *attr) |
| 216 | { |
| 217 | u8 iodec = attr->granularity == 0xfff ? ACPI_DECODE_10 : ACPI_DECODE_16; |
Thomas Gleixner | eb76d55 | 2015-02-02 10:42:51 +0800 | [diff] [blame] | 218 | bool wp = addr->info.mem.write_protect; |
| 219 | u64 len = attr->address_length; |
Jiang Liu | 1fb01ca | 2015-07-08 15:26:39 +0800 | [diff] [blame] | 220 | u64 start, end, offset = 0; |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 221 | struct resource *res = &win->res; |
Thomas Gleixner | eb76d55 | 2015-02-02 10:42:51 +0800 | [diff] [blame] | 222 | |
Jiang Liu | a274019 | 2015-02-02 10:42:57 +0800 | [diff] [blame] | 223 | /* |
| 224 | * Filter out invalid descriptor according to ACPI Spec 5.0, section |
| 225 | * 6.4.3.5 Address Space Resource Descriptors. |
| 226 | */ |
| 227 | if ((addr->min_address_fixed != addr->max_address_fixed && len) || |
| 228 | (addr->min_address_fixed && addr->max_address_fixed && !len)) |
| 229 | pr_debug("ACPI: Invalid address space min_addr_fix %d, max_addr_fix %d, len %llx\n", |
| 230 | addr->min_address_fixed, addr->max_address_fixed, len); |
| 231 | |
Jiang Liu | 2ea3d26 | 2015-02-02 10:42:59 +0800 | [diff] [blame] | 232 | /* |
| 233 | * For bridges that translate addresses across the bridge, |
| 234 | * translation_offset is the offset that must be added to the |
| 235 | * address on the secondary side to obtain the address on the |
| 236 | * primary side. Non-bridge devices must list 0 for all Address |
| 237 | * Translation offset bits. |
| 238 | */ |
Jiang Liu | 1fb01ca | 2015-07-08 15:26:39 +0800 | [diff] [blame] | 239 | if (addr->producer_consumer == ACPI_PRODUCER) |
| 240 | offset = attr->translation_offset; |
| 241 | else if (attr->translation_offset) |
Jiang Liu | 2ea3d26 | 2015-02-02 10:42:59 +0800 | [diff] [blame] | 242 | pr_debug("ACPI: translation_offset(%lld) is invalid for non-bridge device.\n", |
| 243 | attr->translation_offset); |
Jiang Liu | 1fb01ca | 2015-07-08 15:26:39 +0800 | [diff] [blame] | 244 | start = attr->minimum + offset; |
| 245 | end = attr->maximum + offset; |
| 246 | |
| 247 | win->offset = offset; |
| 248 | res->start = start; |
| 249 | res->end = end; |
| 250 | if (sizeof(resource_size_t) < sizeof(u64) && |
| 251 | (offset != win->offset || start != res->start || end != res->end)) { |
| 252 | pr_warn("acpi resource window ([%#llx-%#llx] ignored, not CPU addressable)\n", |
| 253 | attr->minimum, attr->maximum); |
| 254 | return false; |
Jiang Liu | 2ea3d26 | 2015-02-02 10:42:59 +0800 | [diff] [blame] | 255 | } |
| 256 | |
Thomas Gleixner | eb76d55 | 2015-02-02 10:42:51 +0800 | [diff] [blame] | 257 | switch (addr->resource_type) { |
| 258 | case ACPI_MEMORY_RANGE: |
Thomas Gleixner | 72e26b0 | 2015-02-02 10:42:52 +0800 | [diff] [blame] | 259 | acpi_dev_memresource_flags(res, len, wp); |
Thomas Gleixner | eb76d55 | 2015-02-02 10:42:51 +0800 | [diff] [blame] | 260 | break; |
| 261 | case ACPI_IO_RANGE: |
Jiang Liu | 91236ec | 2015-10-14 14:29:36 +0800 | [diff] [blame] | 262 | acpi_dev_ioresource_flags(res, len, iodec, |
| 263 | addr->info.io.translation_type); |
Thomas Gleixner | eb76d55 | 2015-02-02 10:42:51 +0800 | [diff] [blame] | 264 | break; |
| 265 | case ACPI_BUS_NUMBER_RANGE: |
| 266 | res->flags = IORESOURCE_BUS; |
| 267 | break; |
| 268 | default: |
| 269 | return false; |
| 270 | } |
| 271 | |
Thomas Gleixner | 72e26b0 | 2015-02-02 10:42:52 +0800 | [diff] [blame] | 272 | if (addr->producer_consumer == ACPI_PRODUCER) |
| 273 | res->flags |= IORESOURCE_WINDOW; |
| 274 | |
Thomas Gleixner | fcb29bb | 2015-02-02 10:42:53 +0800 | [diff] [blame] | 275 | if (addr->info.mem.caching == ACPI_PREFETCHABLE_MEMORY) |
| 276 | res->flags |= IORESOURCE_PREFETCH; |
| 277 | |
Thomas Gleixner | eb76d55 | 2015-02-02 10:42:51 +0800 | [diff] [blame] | 278 | return !(res->flags & IORESOURCE_DISABLED); |
| 279 | } |
| 280 | |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 281 | /** |
| 282 | * acpi_dev_resource_address_space - Extract ACPI address space information. |
| 283 | * @ares: Input ACPI resource object. |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 284 | * @win: Output generic resource object. |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 285 | * |
| 286 | * Check if the given ACPI resource object represents an address space resource |
| 287 | * and if that's the case, use the information in it to populate the generic |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 288 | * resource object pointed to by @win. |
Jiang Liu | 581c19f | 2015-02-02 10:42:55 +0800 | [diff] [blame] | 289 | * |
| 290 | * Return: |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 291 | * 1) false with win->res.flags setting to zero: not the expected resource type |
| 292 | * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned |
| 293 | * resource |
Jiang Liu | 581c19f | 2015-02-02 10:42:55 +0800 | [diff] [blame] | 294 | * 3) true: valid assigned resource |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 295 | */ |
| 296 | bool acpi_dev_resource_address_space(struct acpi_resource *ares, |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 297 | struct resource_win *win) |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 298 | { |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 299 | struct acpi_resource_address64 addr; |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 300 | |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 301 | win->res.flags = 0; |
Thomas Gleixner | eb76d55 | 2015-02-02 10:42:51 +0800 | [diff] [blame] | 302 | if (ACPI_FAILURE(acpi_resource_to_address64(ares, &addr))) |
Jiang Liu | 6658c73 | 2014-10-27 13:21:35 +0800 | [diff] [blame] | 303 | return false; |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 304 | |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 305 | return acpi_decode_space(win, (struct acpi_resource_address *)&addr, |
Thomas Gleixner | eb76d55 | 2015-02-02 10:42:51 +0800 | [diff] [blame] | 306 | &addr.address); |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 307 | } |
| 308 | EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space); |
| 309 | |
| 310 | /** |
| 311 | * acpi_dev_resource_ext_address_space - Extract ACPI address space information. |
| 312 | * @ares: Input ACPI resource object. |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 313 | * @win: Output generic resource object. |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 314 | * |
| 315 | * Check if the given ACPI resource object represents an extended address space |
| 316 | * resource and if that's the case, use the information in it to populate the |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 317 | * generic resource object pointed to by @win. |
Jiang Liu | 581c19f | 2015-02-02 10:42:55 +0800 | [diff] [blame] | 318 | * |
| 319 | * Return: |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 320 | * 1) false with win->res.flags setting to zero: not the expected resource type |
| 321 | * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned |
| 322 | * resource |
Jiang Liu | 581c19f | 2015-02-02 10:42:55 +0800 | [diff] [blame] | 323 | * 3) true: valid assigned resource |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 324 | */ |
| 325 | bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares, |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 326 | struct resource_win *win) |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 327 | { |
| 328 | struct acpi_resource_extended_address64 *ext_addr; |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 329 | |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 330 | win->res.flags = 0; |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 331 | if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64) |
| 332 | return false; |
| 333 | |
| 334 | ext_addr = &ares->data.ext_address64; |
| 335 | |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 336 | return acpi_decode_space(win, (struct acpi_resource_address *)ext_addr, |
Thomas Gleixner | eb76d55 | 2015-02-02 10:42:51 +0800 | [diff] [blame] | 337 | &ext_addr->address); |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 338 | } |
| 339 | EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space); |
| 340 | |
| 341 | /** |
| 342 | * acpi_dev_irq_flags - Determine IRQ resource flags. |
| 343 | * @triggering: Triggering type as provided by ACPI. |
| 344 | * @polarity: Interrupt polarity as provided by ACPI. |
| 345 | * @shareable: Whether or not the interrupt is shareable. |
| 346 | */ |
| 347 | unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable) |
| 348 | { |
| 349 | unsigned long flags; |
| 350 | |
| 351 | if (triggering == ACPI_LEVEL_SENSITIVE) |
| 352 | flags = polarity == ACPI_ACTIVE_LOW ? |
| 353 | IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL; |
| 354 | else |
| 355 | flags = polarity == ACPI_ACTIVE_LOW ? |
| 356 | IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE; |
| 357 | |
| 358 | if (shareable == ACPI_SHARED) |
| 359 | flags |= IORESOURCE_IRQ_SHAREABLE; |
| 360 | |
| 361 | return flags | IORESOURCE_IRQ; |
| 362 | } |
| 363 | EXPORT_SYMBOL_GPL(acpi_dev_irq_flags); |
| 364 | |
Christophe RICARD | 55a9341 | 2015-12-23 23:25:33 +0100 | [diff] [blame] | 365 | /** |
| 366 | * acpi_dev_get_irq_type - Determine irq type. |
| 367 | * @triggering: Triggering type as provided by ACPI. |
| 368 | * @polarity: Interrupt polarity as provided by ACPI. |
| 369 | */ |
| 370 | unsigned int acpi_dev_get_irq_type(int triggering, int polarity) |
| 371 | { |
| 372 | switch (polarity) { |
| 373 | case ACPI_ACTIVE_LOW: |
| 374 | return triggering == ACPI_EDGE_SENSITIVE ? |
| 375 | IRQ_TYPE_EDGE_FALLING : |
| 376 | IRQ_TYPE_LEVEL_LOW; |
| 377 | case ACPI_ACTIVE_HIGH: |
| 378 | return triggering == ACPI_EDGE_SENSITIVE ? |
| 379 | IRQ_TYPE_EDGE_RISING : |
| 380 | IRQ_TYPE_LEVEL_HIGH; |
| 381 | case ACPI_ACTIVE_BOTH: |
| 382 | if (triggering == ACPI_EDGE_SENSITIVE) |
| 383 | return IRQ_TYPE_EDGE_BOTH; |
| 384 | default: |
| 385 | return IRQ_TYPE_NONE; |
| 386 | } |
| 387 | } |
| 388 | EXPORT_SYMBOL_GPL(acpi_dev_get_irq_type); |
| 389 | |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 390 | static void acpi_dev_irqresource_disabled(struct resource *res, u32 gsi) |
| 391 | { |
| 392 | res->start = gsi; |
| 393 | res->end = gsi; |
Jiang Liu | c78b688 | 2015-02-02 10:42:56 +0800 | [diff] [blame] | 394 | res->flags = IORESOURCE_IRQ | IORESOURCE_DISABLED | IORESOURCE_UNSET; |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | static void acpi_dev_get_irqresource(struct resource *res, u32 gsi, |
Mika Westerberg | 204ebc0 | 2013-05-20 15:41:45 +0000 | [diff] [blame] | 398 | u8 triggering, u8 polarity, u8 shareable, |
| 399 | bool legacy) |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 400 | { |
| 401 | int irq, p, t; |
| 402 | |
| 403 | if (!valid_IRQ(gsi)) { |
| 404 | acpi_dev_irqresource_disabled(res, gsi); |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | /* |
Masahiro Yamada | 0367105 | 2017-02-27 14:29:28 -0800 | [diff] [blame^] | 409 | * In IO-APIC mode, use overridden attribute. Two reasons: |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 410 | * 1. BIOS bug in DSDT |
| 411 | * 2. BIOS uses IO-APIC mode Interrupt Source Override |
Mika Westerberg | 204ebc0 | 2013-05-20 15:41:45 +0000 | [diff] [blame] | 412 | * |
| 413 | * We do this only if we are dealing with IRQ() or IRQNoFlags() |
| 414 | * resource (the legacy ISA resources). With modern ACPI 5 devices |
| 415 | * using extended IRQ descriptors we take the IRQ configuration |
| 416 | * from _CRS directly. |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 417 | */ |
Mika Westerberg | 204ebc0 | 2013-05-20 15:41:45 +0000 | [diff] [blame] | 418 | if (legacy && !acpi_get_override_irq(gsi, &t, &p)) { |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 419 | u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE; |
| 420 | u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH; |
| 421 | |
| 422 | if (triggering != trig || polarity != pol) { |
| 423 | pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi, |
Mika Westerberg | 204ebc0 | 2013-05-20 15:41:45 +0000 | [diff] [blame] | 424 | t ? "level" : "edge", p ? "low" : "high"); |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 425 | triggering = trig; |
| 426 | polarity = pol; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | res->flags = acpi_dev_irq_flags(triggering, polarity, shareable); |
| 431 | irq = acpi_register_gsi(NULL, gsi, triggering, polarity); |
| 432 | if (irq >= 0) { |
| 433 | res->start = irq; |
| 434 | res->end = irq; |
| 435 | } else { |
| 436 | acpi_dev_irqresource_disabled(res, gsi); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information. |
| 442 | * @ares: Input ACPI resource object. |
| 443 | * @index: Index into the array of GSIs represented by the resource. |
| 444 | * @res: Output generic resource object. |
| 445 | * |
| 446 | * Check if the given ACPI resource object represents an interrupt resource |
| 447 | * and @index does not exceed the resource's interrupt count (true is returned |
| 448 | * in that case regardless of the results of the other checks)). If that's the |
| 449 | * case, register the GSI corresponding to @index from the array of interrupts |
| 450 | * represented by the resource and populate the generic resource object pointed |
| 451 | * to by @res accordingly. If the registration of the GSI is not successful, |
| 452 | * IORESOURCE_DISABLED will be set it that object's flags. |
Jiang Liu | 581c19f | 2015-02-02 10:42:55 +0800 | [diff] [blame] | 453 | * |
| 454 | * Return: |
| 455 | * 1) false with res->flags setting to zero: not the expected resource type |
| 456 | * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource |
| 457 | * 3) true: valid assigned resource |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 458 | */ |
| 459 | bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index, |
| 460 | struct resource *res) |
| 461 | { |
| 462 | struct acpi_resource_irq *irq; |
| 463 | struct acpi_resource_extended_irq *ext_irq; |
| 464 | |
| 465 | switch (ares->type) { |
| 466 | case ACPI_RESOURCE_TYPE_IRQ: |
| 467 | /* |
| 468 | * Per spec, only one interrupt per descriptor is allowed in |
| 469 | * _CRS, but some firmware violates this, so parse them all. |
| 470 | */ |
| 471 | irq = &ares->data.irq; |
| 472 | if (index >= irq->interrupt_count) { |
| 473 | acpi_dev_irqresource_disabled(res, 0); |
| 474 | return false; |
| 475 | } |
| 476 | acpi_dev_get_irqresource(res, irq->interrupts[index], |
| 477 | irq->triggering, irq->polarity, |
Mika Westerberg | 204ebc0 | 2013-05-20 15:41:45 +0000 | [diff] [blame] | 478 | irq->sharable, true); |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 479 | break; |
| 480 | case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: |
| 481 | ext_irq = &ares->data.extended_irq; |
| 482 | if (index >= ext_irq->interrupt_count) { |
| 483 | acpi_dev_irqresource_disabled(res, 0); |
| 484 | return false; |
| 485 | } |
Agustin Vega-Frias | fa20b17 | 2017-02-02 18:23:57 -0500 | [diff] [blame] | 486 | if (is_gsi(ext_irq)) |
| 487 | acpi_dev_get_irqresource(res, ext_irq->interrupts[index], |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 488 | ext_irq->triggering, ext_irq->polarity, |
Mika Westerberg | 204ebc0 | 2013-05-20 15:41:45 +0000 | [diff] [blame] | 489 | ext_irq->sharable, false); |
Agustin Vega-Frias | fa20b17 | 2017-02-02 18:23:57 -0500 | [diff] [blame] | 490 | else |
| 491 | acpi_dev_irqresource_disabled(res, 0); |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 492 | break; |
| 493 | default: |
Jiang Liu | 581c19f | 2015-02-02 10:42:55 +0800 | [diff] [blame] | 494 | res->flags = 0; |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 495 | return false; |
| 496 | } |
| 497 | |
| 498 | return true; |
| 499 | } |
| 500 | EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt); |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 501 | |
| 502 | /** |
| 503 | * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources(). |
| 504 | * @list: The head of the resource list to free. |
| 505 | */ |
| 506 | void acpi_dev_free_resource_list(struct list_head *list) |
| 507 | { |
Jiang Liu | 90e9782 | 2015-02-05 13:44:43 +0800 | [diff] [blame] | 508 | resource_list_free(list); |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 509 | } |
| 510 | EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list); |
| 511 | |
| 512 | struct res_proc_context { |
| 513 | struct list_head *list; |
| 514 | int (*preproc)(struct acpi_resource *, void *); |
| 515 | void *preproc_data; |
| 516 | int count; |
| 517 | int error; |
| 518 | }; |
| 519 | |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 520 | static acpi_status acpi_dev_new_resource_entry(struct resource_win *win, |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 521 | struct res_proc_context *c) |
| 522 | { |
Jiang Liu | 90e9782 | 2015-02-05 13:44:43 +0800 | [diff] [blame] | 523 | struct resource_entry *rentry; |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 524 | |
Jiang Liu | 90e9782 | 2015-02-05 13:44:43 +0800 | [diff] [blame] | 525 | rentry = resource_list_create_entry(NULL, 0); |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 526 | if (!rentry) { |
| 527 | c->error = -ENOMEM; |
| 528 | return AE_NO_MEMORY; |
| 529 | } |
Jiang Liu | 90e9782 | 2015-02-05 13:44:43 +0800 | [diff] [blame] | 530 | *rentry->res = win->res; |
Jiang Liu | 93286f4 | 2015-02-02 10:43:00 +0800 | [diff] [blame] | 531 | rentry->offset = win->offset; |
Jiang Liu | 90e9782 | 2015-02-05 13:44:43 +0800 | [diff] [blame] | 532 | resource_list_add_tail(rentry, c->list); |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 533 | c->count++; |
| 534 | return AE_OK; |
| 535 | } |
| 536 | |
| 537 | static acpi_status acpi_dev_process_resource(struct acpi_resource *ares, |
| 538 | void *context) |
| 539 | { |
| 540 | struct res_proc_context *c = context; |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 541 | struct resource_win win; |
| 542 | struct resource *res = &win.res; |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 543 | int i; |
| 544 | |
| 545 | if (c->preproc) { |
| 546 | int ret; |
| 547 | |
| 548 | ret = c->preproc(ares, c->preproc_data); |
| 549 | if (ret < 0) { |
| 550 | c->error = ret; |
Rafael J. Wysocki | 8a66790 | 2012-11-16 21:55:48 +0100 | [diff] [blame] | 551 | return AE_CTRL_TERMINATE; |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 552 | } else if (ret > 0) { |
| 553 | return AE_OK; |
| 554 | } |
| 555 | } |
| 556 | |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 557 | memset(&win, 0, sizeof(win)); |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 558 | |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 559 | if (acpi_dev_resource_memory(ares, res) |
| 560 | || acpi_dev_resource_io(ares, res) |
| 561 | || acpi_dev_resource_address_space(ares, &win) |
| 562 | || acpi_dev_resource_ext_address_space(ares, &win)) |
| 563 | return acpi_dev_new_resource_entry(&win, c); |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 564 | |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 565 | for (i = 0; acpi_dev_resource_interrupt(ares, i, res); i++) { |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 566 | acpi_status status; |
| 567 | |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 568 | status = acpi_dev_new_resource_entry(&win, c); |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 569 | if (ACPI_FAILURE(status)) |
| 570 | return status; |
| 571 | } |
| 572 | |
| 573 | return AE_OK; |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * acpi_dev_get_resources - Get current resources of a device. |
| 578 | * @adev: ACPI device node to get the resources for. |
| 579 | * @list: Head of the resultant list of resources (must be empty). |
| 580 | * @preproc: The caller's preprocessing routine. |
| 581 | * @preproc_data: Pointer passed to the caller's preprocessing routine. |
| 582 | * |
| 583 | * Evaluate the _CRS method for the given device node and process its output by |
| 584 | * (1) executing the @preproc() rountine provided by the caller, passing the |
| 585 | * resource pointer and @preproc_data to it as arguments, for each ACPI resource |
| 586 | * returned and (2) converting all of the returned ACPI resources into struct |
| 587 | * resource objects if possible. If the return value of @preproc() in step (1) |
| 588 | * is different from 0, step (2) is not applied to the given ACPI resource and |
| 589 | * if that value is negative, the whole processing is aborted and that value is |
| 590 | * returned as the final error code. |
| 591 | * |
| 592 | * The resultant struct resource objects are put on the list pointed to by |
Jiang Liu | 90e9782 | 2015-02-05 13:44:43 +0800 | [diff] [blame] | 593 | * @list, that must be empty initially, as members of struct resource_entry |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 594 | * objects. Callers of this routine should use %acpi_dev_free_resource_list() to |
| 595 | * free that list. |
| 596 | * |
| 597 | * The number of resources in the output list is returned on success, an error |
| 598 | * code reflecting the error condition is returned otherwise. |
| 599 | */ |
| 600 | int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list, |
| 601 | int (*preproc)(struct acpi_resource *, void *), |
| 602 | void *preproc_data) |
| 603 | { |
| 604 | struct res_proc_context c; |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 605 | acpi_status status; |
| 606 | |
| 607 | if (!adev || !adev->handle || !list_empty(list)) |
| 608 | return -EINVAL; |
| 609 | |
Jiang Liu | 952c63e | 2013-06-29 00:24:38 +0800 | [diff] [blame] | 610 | if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 611 | return 0; |
| 612 | |
| 613 | c.list = list; |
| 614 | c.preproc = preproc; |
| 615 | c.preproc_data = preproc_data; |
| 616 | c.count = 0; |
| 617 | c.error = 0; |
| 618 | status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS, |
| 619 | acpi_dev_process_resource, &c); |
| 620 | if (ACPI_FAILURE(status)) { |
| 621 | acpi_dev_free_resource_list(list); |
| 622 | return c.error ? c.error : -EIO; |
| 623 | } |
| 624 | |
| 625 | return c.count; |
| 626 | } |
| 627 | EXPORT_SYMBOL_GPL(acpi_dev_get_resources); |
Jiang Liu | 62d1141 | 2015-02-02 10:43:01 +0800 | [diff] [blame] | 628 | |
| 629 | /** |
| 630 | * acpi_dev_filter_resource_type - Filter ACPI resource according to resource |
| 631 | * types |
| 632 | * @ares: Input ACPI resource object. |
| 633 | * @types: Valid resource types of IORESOURCE_XXX |
| 634 | * |
Jiang Liu | 2c62e84 | 2015-04-30 12:41:28 +0800 | [diff] [blame] | 635 | * This is a helper function to support acpi_dev_get_resources(), which filters |
Jiang Liu | 62d1141 | 2015-02-02 10:43:01 +0800 | [diff] [blame] | 636 | * ACPI resource objects according to resource types. |
| 637 | */ |
| 638 | int acpi_dev_filter_resource_type(struct acpi_resource *ares, |
| 639 | unsigned long types) |
| 640 | { |
| 641 | unsigned long type = 0; |
| 642 | |
| 643 | switch (ares->type) { |
| 644 | case ACPI_RESOURCE_TYPE_MEMORY24: |
| 645 | case ACPI_RESOURCE_TYPE_MEMORY32: |
| 646 | case ACPI_RESOURCE_TYPE_FIXED_MEMORY32: |
| 647 | type = IORESOURCE_MEM; |
| 648 | break; |
| 649 | case ACPI_RESOURCE_TYPE_IO: |
| 650 | case ACPI_RESOURCE_TYPE_FIXED_IO: |
| 651 | type = IORESOURCE_IO; |
| 652 | break; |
| 653 | case ACPI_RESOURCE_TYPE_IRQ: |
| 654 | case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: |
| 655 | type = IORESOURCE_IRQ; |
| 656 | break; |
| 657 | case ACPI_RESOURCE_TYPE_DMA: |
| 658 | case ACPI_RESOURCE_TYPE_FIXED_DMA: |
| 659 | type = IORESOURCE_DMA; |
| 660 | break; |
| 661 | case ACPI_RESOURCE_TYPE_GENERIC_REGISTER: |
| 662 | type = IORESOURCE_REG; |
| 663 | break; |
| 664 | case ACPI_RESOURCE_TYPE_ADDRESS16: |
| 665 | case ACPI_RESOURCE_TYPE_ADDRESS32: |
| 666 | case ACPI_RESOURCE_TYPE_ADDRESS64: |
| 667 | case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64: |
| 668 | if (ares->data.address.resource_type == ACPI_MEMORY_RANGE) |
| 669 | type = IORESOURCE_MEM; |
| 670 | else if (ares->data.address.resource_type == ACPI_IO_RANGE) |
| 671 | type = IORESOURCE_IO; |
| 672 | else if (ares->data.address.resource_type == |
| 673 | ACPI_BUS_NUMBER_RANGE) |
| 674 | type = IORESOURCE_BUS; |
| 675 | break; |
| 676 | default: |
| 677 | break; |
| 678 | } |
| 679 | |
| 680 | return (type & types) ? 0 : 1; |
| 681 | } |
| 682 | EXPORT_SYMBOL_GPL(acpi_dev_filter_resource_type); |
Bjorn Helgaas | 0071098 | 2016-11-30 14:47:13 -0600 | [diff] [blame] | 683 | |
| 684 | static int acpi_dev_consumes_res(struct acpi_device *adev, struct resource *res) |
| 685 | { |
| 686 | struct list_head resource_list; |
| 687 | struct resource_entry *rentry; |
| 688 | int ret, found = 0; |
| 689 | |
| 690 | INIT_LIST_HEAD(&resource_list); |
| 691 | ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL); |
| 692 | if (ret < 0) |
| 693 | return 0; |
| 694 | |
| 695 | list_for_each_entry(rentry, &resource_list, node) { |
| 696 | if (resource_contains(rentry->res, res)) { |
| 697 | found = 1; |
| 698 | break; |
| 699 | } |
| 700 | |
| 701 | } |
| 702 | |
| 703 | acpi_dev_free_resource_list(&resource_list); |
| 704 | return found; |
| 705 | } |
| 706 | |
| 707 | static acpi_status acpi_res_consumer_cb(acpi_handle handle, u32 depth, |
| 708 | void *context, void **ret) |
| 709 | { |
| 710 | struct resource *res = context; |
| 711 | struct acpi_device **consumer = (struct acpi_device **) ret; |
| 712 | struct acpi_device *adev; |
| 713 | |
| 714 | if (acpi_bus_get_device(handle, &adev)) |
| 715 | return AE_OK; |
| 716 | |
| 717 | if (acpi_dev_consumes_res(adev, res)) { |
| 718 | *consumer = adev; |
| 719 | return AE_CTRL_TERMINATE; |
| 720 | } |
| 721 | |
| 722 | return AE_OK; |
| 723 | } |
| 724 | |
| 725 | /** |
| 726 | * acpi_resource_consumer - Find the ACPI device that consumes @res. |
| 727 | * @res: Resource to search for. |
| 728 | * |
| 729 | * Search the current resource settings (_CRS) of every ACPI device node |
| 730 | * for @res. If we find an ACPI device whose _CRS includes @res, return |
| 731 | * it. Otherwise, return NULL. |
| 732 | */ |
| 733 | struct acpi_device *acpi_resource_consumer(struct resource *res) |
| 734 | { |
| 735 | struct acpi_device *consumer = NULL; |
| 736 | |
| 737 | acpi_get_devices(NULL, acpi_res_consumer_cb, res, (void **) &consumer); |
| 738 | return consumer; |
| 739 | } |