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