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 | 1fb01ca | 2015-07-08 15:26:39 +0800 | [diff] [blame] | 196 | u64 start, end, offset = 0; |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 197 | struct resource *res = &win->res; |
Thomas Gleixner | eb76d55 | 2015-02-02 10:42:51 +0800 | [diff] [blame] | 198 | |
Jiang Liu | a274019 | 2015-02-02 10:42:57 +0800 | [diff] [blame] | 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 | |
Jiang Liu | 2ea3d26 | 2015-02-02 10:42:59 +0800 | [diff] [blame] | 208 | /* |
| 209 | * For bridges that translate addresses across the bridge, |
| 210 | * translation_offset is the offset that must be added to the |
| 211 | * address on the secondary side to obtain the address on the |
| 212 | * primary side. Non-bridge devices must list 0 for all Address |
| 213 | * Translation offset bits. |
| 214 | */ |
Jiang Liu | 1fb01ca | 2015-07-08 15:26:39 +0800 | [diff] [blame] | 215 | if (addr->producer_consumer == ACPI_PRODUCER) |
| 216 | offset = attr->translation_offset; |
| 217 | else if (attr->translation_offset) |
Jiang Liu | 2ea3d26 | 2015-02-02 10:42:59 +0800 | [diff] [blame] | 218 | pr_debug("ACPI: translation_offset(%lld) is invalid for non-bridge device.\n", |
| 219 | attr->translation_offset); |
Jiang Liu | 1fb01ca | 2015-07-08 15:26:39 +0800 | [diff] [blame] | 220 | start = attr->minimum + offset; |
| 221 | end = attr->maximum + offset; |
| 222 | |
| 223 | win->offset = offset; |
| 224 | res->start = start; |
| 225 | res->end = end; |
| 226 | if (sizeof(resource_size_t) < sizeof(u64) && |
| 227 | (offset != win->offset || start != res->start || end != res->end)) { |
| 228 | pr_warn("acpi resource window ([%#llx-%#llx] ignored, not CPU addressable)\n", |
| 229 | attr->minimum, attr->maximum); |
| 230 | return false; |
Jiang Liu | 2ea3d26 | 2015-02-02 10:42:59 +0800 | [diff] [blame] | 231 | } |
| 232 | |
Thomas Gleixner | eb76d55 | 2015-02-02 10:42:51 +0800 | [diff] [blame] | 233 | switch (addr->resource_type) { |
| 234 | case ACPI_MEMORY_RANGE: |
Thomas Gleixner | 72e26b0 | 2015-02-02 10:42:52 +0800 | [diff] [blame] | 235 | acpi_dev_memresource_flags(res, len, wp); |
Thomas Gleixner | eb76d55 | 2015-02-02 10:42:51 +0800 | [diff] [blame] | 236 | break; |
| 237 | case ACPI_IO_RANGE: |
Thomas Gleixner | 72e26b0 | 2015-02-02 10:42:52 +0800 | [diff] [blame] | 238 | acpi_dev_ioresource_flags(res, len, iodec); |
Thomas Gleixner | eb76d55 | 2015-02-02 10:42:51 +0800 | [diff] [blame] | 239 | break; |
| 240 | case ACPI_BUS_NUMBER_RANGE: |
| 241 | res->flags = IORESOURCE_BUS; |
| 242 | break; |
| 243 | default: |
| 244 | return false; |
| 245 | } |
| 246 | |
Thomas Gleixner | 72e26b0 | 2015-02-02 10:42:52 +0800 | [diff] [blame] | 247 | if (addr->producer_consumer == ACPI_PRODUCER) |
| 248 | res->flags |= IORESOURCE_WINDOW; |
| 249 | |
Thomas Gleixner | fcb29bb | 2015-02-02 10:42:53 +0800 | [diff] [blame] | 250 | if (addr->info.mem.caching == ACPI_PREFETCHABLE_MEMORY) |
| 251 | res->flags |= IORESOURCE_PREFETCH; |
| 252 | |
Thomas Gleixner | eb76d55 | 2015-02-02 10:42:51 +0800 | [diff] [blame] | 253 | return !(res->flags & IORESOURCE_DISABLED); |
| 254 | } |
| 255 | |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 256 | /** |
| 257 | * acpi_dev_resource_address_space - Extract ACPI address space information. |
| 258 | * @ares: Input ACPI resource object. |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 259 | * @win: Output generic resource object. |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 260 | * |
| 261 | * Check if the given ACPI resource object represents an address space resource |
| 262 | * 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] | 263 | * resource object pointed to by @win. |
Jiang Liu | 581c19f | 2015-02-02 10:42:55 +0800 | [diff] [blame] | 264 | * |
| 265 | * Return: |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 266 | * 1) false with win->res.flags setting to zero: not the expected resource type |
| 267 | * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned |
| 268 | * resource |
Jiang Liu | 581c19f | 2015-02-02 10:42:55 +0800 | [diff] [blame] | 269 | * 3) true: valid assigned resource |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 270 | */ |
| 271 | bool acpi_dev_resource_address_space(struct acpi_resource *ares, |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 272 | struct resource_win *win) |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 273 | { |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 274 | struct acpi_resource_address64 addr; |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 275 | |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 276 | win->res.flags = 0; |
Thomas Gleixner | eb76d55 | 2015-02-02 10:42:51 +0800 | [diff] [blame] | 277 | if (ACPI_FAILURE(acpi_resource_to_address64(ares, &addr))) |
Jiang Liu | 6658c73 | 2014-10-27 13:21:35 +0800 | [diff] [blame] | 278 | return false; |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 279 | |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 280 | return acpi_decode_space(win, (struct acpi_resource_address *)&addr, |
Thomas Gleixner | eb76d55 | 2015-02-02 10:42:51 +0800 | [diff] [blame] | 281 | &addr.address); |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 282 | } |
| 283 | EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space); |
| 284 | |
| 285 | /** |
| 286 | * acpi_dev_resource_ext_address_space - Extract ACPI address space information. |
| 287 | * @ares: Input ACPI resource object. |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 288 | * @win: Output generic resource object. |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 289 | * |
| 290 | * Check if the given ACPI resource object represents an extended address space |
| 291 | * 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] | 292 | * generic resource object pointed to by @win. |
Jiang Liu | 581c19f | 2015-02-02 10:42:55 +0800 | [diff] [blame] | 293 | * |
| 294 | * Return: |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 295 | * 1) false with win->res.flags setting to zero: not the expected resource type |
| 296 | * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned |
| 297 | * resource |
Jiang Liu | 581c19f | 2015-02-02 10:42:55 +0800 | [diff] [blame] | 298 | * 3) true: valid assigned resource |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 299 | */ |
| 300 | bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares, |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 301 | struct resource_win *win) |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 302 | { |
| 303 | struct acpi_resource_extended_address64 *ext_addr; |
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 | win->res.flags = 0; |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 306 | if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64) |
| 307 | return false; |
| 308 | |
| 309 | ext_addr = &ares->data.ext_address64; |
| 310 | |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 311 | return acpi_decode_space(win, (struct acpi_resource_address *)ext_addr, |
Thomas Gleixner | eb76d55 | 2015-02-02 10:42:51 +0800 | [diff] [blame] | 312 | &ext_addr->address); |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 313 | } |
| 314 | EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space); |
| 315 | |
| 316 | /** |
| 317 | * acpi_dev_irq_flags - Determine IRQ resource flags. |
| 318 | * @triggering: Triggering type as provided by ACPI. |
| 319 | * @polarity: Interrupt polarity as provided by ACPI. |
| 320 | * @shareable: Whether or not the interrupt is shareable. |
| 321 | */ |
| 322 | unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable) |
| 323 | { |
| 324 | unsigned long flags; |
| 325 | |
| 326 | if (triggering == ACPI_LEVEL_SENSITIVE) |
| 327 | flags = polarity == ACPI_ACTIVE_LOW ? |
| 328 | IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL; |
| 329 | else |
| 330 | flags = polarity == ACPI_ACTIVE_LOW ? |
| 331 | IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE; |
| 332 | |
| 333 | if (shareable == ACPI_SHARED) |
| 334 | flags |= IORESOURCE_IRQ_SHAREABLE; |
| 335 | |
| 336 | return flags | IORESOURCE_IRQ; |
| 337 | } |
| 338 | EXPORT_SYMBOL_GPL(acpi_dev_irq_flags); |
| 339 | |
| 340 | static void acpi_dev_irqresource_disabled(struct resource *res, u32 gsi) |
| 341 | { |
| 342 | res->start = gsi; |
| 343 | res->end = gsi; |
Jiang Liu | c78b688 | 2015-02-02 10:42:56 +0800 | [diff] [blame] | 344 | res->flags = IORESOURCE_IRQ | IORESOURCE_DISABLED | IORESOURCE_UNSET; |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | static void acpi_dev_get_irqresource(struct resource *res, u32 gsi, |
Mika Westerberg | 204ebc0 | 2013-05-20 15:41:45 +0000 | [diff] [blame] | 348 | u8 triggering, u8 polarity, u8 shareable, |
| 349 | bool legacy) |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 350 | { |
| 351 | int irq, p, t; |
| 352 | |
| 353 | if (!valid_IRQ(gsi)) { |
| 354 | acpi_dev_irqresource_disabled(res, gsi); |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * In IO-APIC mode, use overrided attribute. Two reasons: |
| 360 | * 1. BIOS bug in DSDT |
| 361 | * 2. BIOS uses IO-APIC mode Interrupt Source Override |
Mika Westerberg | 204ebc0 | 2013-05-20 15:41:45 +0000 | [diff] [blame] | 362 | * |
| 363 | * We do this only if we are dealing with IRQ() or IRQNoFlags() |
| 364 | * resource (the legacy ISA resources). With modern ACPI 5 devices |
| 365 | * using extended IRQ descriptors we take the IRQ configuration |
| 366 | * from _CRS directly. |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 367 | */ |
Mika Westerberg | 204ebc0 | 2013-05-20 15:41:45 +0000 | [diff] [blame] | 368 | if (legacy && !acpi_get_override_irq(gsi, &t, &p)) { |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 369 | u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE; |
| 370 | u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH; |
| 371 | |
| 372 | if (triggering != trig || polarity != pol) { |
| 373 | pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi, |
Mika Westerberg | 204ebc0 | 2013-05-20 15:41:45 +0000 | [diff] [blame] | 374 | t ? "level" : "edge", p ? "low" : "high"); |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 375 | triggering = trig; |
| 376 | polarity = pol; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | res->flags = acpi_dev_irq_flags(triggering, polarity, shareable); |
| 381 | irq = acpi_register_gsi(NULL, gsi, triggering, polarity); |
| 382 | if (irq >= 0) { |
| 383 | res->start = irq; |
| 384 | res->end = irq; |
| 385 | } else { |
| 386 | acpi_dev_irqresource_disabled(res, gsi); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information. |
| 392 | * @ares: Input ACPI resource object. |
| 393 | * @index: Index into the array of GSIs represented by the resource. |
| 394 | * @res: Output generic resource object. |
| 395 | * |
| 396 | * Check if the given ACPI resource object represents an interrupt resource |
| 397 | * and @index does not exceed the resource's interrupt count (true is returned |
| 398 | * in that case regardless of the results of the other checks)). If that's the |
| 399 | * case, register the GSI corresponding to @index from the array of interrupts |
| 400 | * represented by the resource and populate the generic resource object pointed |
| 401 | * to by @res accordingly. If the registration of the GSI is not successful, |
| 402 | * IORESOURCE_DISABLED will be set it that object's flags. |
Jiang Liu | 581c19f | 2015-02-02 10:42:55 +0800 | [diff] [blame] | 403 | * |
| 404 | * Return: |
| 405 | * 1) false with res->flags setting to zero: not the expected resource type |
| 406 | * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource |
| 407 | * 3) true: valid assigned resource |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 408 | */ |
| 409 | bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index, |
| 410 | struct resource *res) |
| 411 | { |
| 412 | struct acpi_resource_irq *irq; |
| 413 | struct acpi_resource_extended_irq *ext_irq; |
| 414 | |
| 415 | switch (ares->type) { |
| 416 | case ACPI_RESOURCE_TYPE_IRQ: |
| 417 | /* |
| 418 | * Per spec, only one interrupt per descriptor is allowed in |
| 419 | * _CRS, but some firmware violates this, so parse them all. |
| 420 | */ |
| 421 | irq = &ares->data.irq; |
| 422 | if (index >= irq->interrupt_count) { |
| 423 | acpi_dev_irqresource_disabled(res, 0); |
| 424 | return false; |
| 425 | } |
| 426 | acpi_dev_get_irqresource(res, irq->interrupts[index], |
| 427 | irq->triggering, irq->polarity, |
Mika Westerberg | 204ebc0 | 2013-05-20 15:41:45 +0000 | [diff] [blame] | 428 | irq->sharable, true); |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 429 | break; |
| 430 | case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: |
| 431 | ext_irq = &ares->data.extended_irq; |
| 432 | if (index >= ext_irq->interrupt_count) { |
| 433 | acpi_dev_irqresource_disabled(res, 0); |
| 434 | return false; |
| 435 | } |
| 436 | acpi_dev_get_irqresource(res, ext_irq->interrupts[index], |
| 437 | ext_irq->triggering, ext_irq->polarity, |
Mika Westerberg | 204ebc0 | 2013-05-20 15:41:45 +0000 | [diff] [blame] | 438 | ext_irq->sharable, false); |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 439 | break; |
| 440 | default: |
Jiang Liu | 581c19f | 2015-02-02 10:42:55 +0800 | [diff] [blame] | 441 | res->flags = 0; |
Rafael J. Wysocki | 046d9ce | 2012-11-15 00:30:01 +0100 | [diff] [blame] | 442 | return false; |
| 443 | } |
| 444 | |
| 445 | return true; |
| 446 | } |
| 447 | EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt); |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 448 | |
| 449 | /** |
| 450 | * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources(). |
| 451 | * @list: The head of the resource list to free. |
| 452 | */ |
| 453 | void acpi_dev_free_resource_list(struct list_head *list) |
| 454 | { |
Jiang Liu | 90e9782 | 2015-02-05 13:44:43 +0800 | [diff] [blame] | 455 | resource_list_free(list); |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 456 | } |
| 457 | EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list); |
| 458 | |
| 459 | struct res_proc_context { |
| 460 | struct list_head *list; |
| 461 | int (*preproc)(struct acpi_resource *, void *); |
| 462 | void *preproc_data; |
| 463 | int count; |
| 464 | int error; |
| 465 | }; |
| 466 | |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 467 | 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] | 468 | struct res_proc_context *c) |
| 469 | { |
Jiang Liu | 90e9782 | 2015-02-05 13:44:43 +0800 | [diff] [blame] | 470 | struct resource_entry *rentry; |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 471 | |
Jiang Liu | 90e9782 | 2015-02-05 13:44:43 +0800 | [diff] [blame] | 472 | rentry = resource_list_create_entry(NULL, 0); |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 473 | if (!rentry) { |
| 474 | c->error = -ENOMEM; |
| 475 | return AE_NO_MEMORY; |
| 476 | } |
Jiang Liu | 90e9782 | 2015-02-05 13:44:43 +0800 | [diff] [blame] | 477 | *rentry->res = win->res; |
Jiang Liu | 93286f4 | 2015-02-02 10:43:00 +0800 | [diff] [blame] | 478 | rentry->offset = win->offset; |
Jiang Liu | 90e9782 | 2015-02-05 13:44:43 +0800 | [diff] [blame] | 479 | resource_list_add_tail(rentry, c->list); |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 480 | c->count++; |
| 481 | return AE_OK; |
| 482 | } |
| 483 | |
| 484 | static acpi_status acpi_dev_process_resource(struct acpi_resource *ares, |
| 485 | void *context) |
| 486 | { |
| 487 | struct res_proc_context *c = context; |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 488 | struct resource_win win; |
| 489 | struct resource *res = &win.res; |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 490 | int i; |
| 491 | |
| 492 | if (c->preproc) { |
| 493 | int ret; |
| 494 | |
| 495 | ret = c->preproc(ares, c->preproc_data); |
| 496 | if (ret < 0) { |
| 497 | c->error = ret; |
Rafael J. Wysocki | 8a66790 | 2012-11-16 21:55:48 +0100 | [diff] [blame] | 498 | return AE_CTRL_TERMINATE; |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 499 | } else if (ret > 0) { |
| 500 | return AE_OK; |
| 501 | } |
| 502 | } |
| 503 | |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 504 | memset(&win, 0, sizeof(win)); |
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 | if (acpi_dev_resource_memory(ares, res) |
| 507 | || acpi_dev_resource_io(ares, res) |
| 508 | || acpi_dev_resource_address_space(ares, &win) |
| 509 | || acpi_dev_resource_ext_address_space(ares, &win)) |
| 510 | return acpi_dev_new_resource_entry(&win, c); |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 511 | |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 512 | for (i = 0; acpi_dev_resource_interrupt(ares, i, res); i++) { |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 513 | acpi_status status; |
| 514 | |
Jiang Liu | a49170b | 2015-02-02 10:42:58 +0800 | [diff] [blame] | 515 | status = acpi_dev_new_resource_entry(&win, c); |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 516 | if (ACPI_FAILURE(status)) |
| 517 | return status; |
| 518 | } |
| 519 | |
| 520 | return AE_OK; |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * acpi_dev_get_resources - Get current resources of a device. |
| 525 | * @adev: ACPI device node to get the resources for. |
| 526 | * @list: Head of the resultant list of resources (must be empty). |
| 527 | * @preproc: The caller's preprocessing routine. |
| 528 | * @preproc_data: Pointer passed to the caller's preprocessing routine. |
| 529 | * |
| 530 | * Evaluate the _CRS method for the given device node and process its output by |
| 531 | * (1) executing the @preproc() rountine provided by the caller, passing the |
| 532 | * resource pointer and @preproc_data to it as arguments, for each ACPI resource |
| 533 | * returned and (2) converting all of the returned ACPI resources into struct |
| 534 | * resource objects if possible. If the return value of @preproc() in step (1) |
| 535 | * is different from 0, step (2) is not applied to the given ACPI resource and |
| 536 | * if that value is negative, the whole processing is aborted and that value is |
| 537 | * returned as the final error code. |
| 538 | * |
| 539 | * 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] | 540 | * @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] | 541 | * objects. Callers of this routine should use %acpi_dev_free_resource_list() to |
| 542 | * free that list. |
| 543 | * |
| 544 | * The number of resources in the output list is returned on success, an error |
| 545 | * code reflecting the error condition is returned otherwise. |
| 546 | */ |
| 547 | int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list, |
| 548 | int (*preproc)(struct acpi_resource *, void *), |
| 549 | void *preproc_data) |
| 550 | { |
| 551 | struct res_proc_context c; |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 552 | acpi_status status; |
| 553 | |
| 554 | if (!adev || !adev->handle || !list_empty(list)) |
| 555 | return -EINVAL; |
| 556 | |
Jiang Liu | 952c63e | 2013-06-29 00:24:38 +0800 | [diff] [blame] | 557 | if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame] | 558 | return 0; |
| 559 | |
| 560 | c.list = list; |
| 561 | c.preproc = preproc; |
| 562 | c.preproc_data = preproc_data; |
| 563 | c.count = 0; |
| 564 | c.error = 0; |
| 565 | status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS, |
| 566 | acpi_dev_process_resource, &c); |
| 567 | if (ACPI_FAILURE(status)) { |
| 568 | acpi_dev_free_resource_list(list); |
| 569 | return c.error ? c.error : -EIO; |
| 570 | } |
| 571 | |
| 572 | return c.count; |
| 573 | } |
| 574 | EXPORT_SYMBOL_GPL(acpi_dev_get_resources); |
Jiang Liu | 62d1141 | 2015-02-02 10:43:01 +0800 | [diff] [blame] | 575 | |
| 576 | /** |
| 577 | * acpi_dev_filter_resource_type - Filter ACPI resource according to resource |
| 578 | * types |
| 579 | * @ares: Input ACPI resource object. |
| 580 | * @types: Valid resource types of IORESOURCE_XXX |
| 581 | * |
Jiang Liu | 2c62e84 | 2015-04-30 12:41:28 +0800 | [diff] [blame] | 582 | * 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] | 583 | * ACPI resource objects according to resource types. |
| 584 | */ |
| 585 | int acpi_dev_filter_resource_type(struct acpi_resource *ares, |
| 586 | unsigned long types) |
| 587 | { |
| 588 | unsigned long type = 0; |
| 589 | |
| 590 | switch (ares->type) { |
| 591 | case ACPI_RESOURCE_TYPE_MEMORY24: |
| 592 | case ACPI_RESOURCE_TYPE_MEMORY32: |
| 593 | case ACPI_RESOURCE_TYPE_FIXED_MEMORY32: |
| 594 | type = IORESOURCE_MEM; |
| 595 | break; |
| 596 | case ACPI_RESOURCE_TYPE_IO: |
| 597 | case ACPI_RESOURCE_TYPE_FIXED_IO: |
| 598 | type = IORESOURCE_IO; |
| 599 | break; |
| 600 | case ACPI_RESOURCE_TYPE_IRQ: |
| 601 | case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: |
| 602 | type = IORESOURCE_IRQ; |
| 603 | break; |
| 604 | case ACPI_RESOURCE_TYPE_DMA: |
| 605 | case ACPI_RESOURCE_TYPE_FIXED_DMA: |
| 606 | type = IORESOURCE_DMA; |
| 607 | break; |
| 608 | case ACPI_RESOURCE_TYPE_GENERIC_REGISTER: |
| 609 | type = IORESOURCE_REG; |
| 610 | break; |
| 611 | case ACPI_RESOURCE_TYPE_ADDRESS16: |
| 612 | case ACPI_RESOURCE_TYPE_ADDRESS32: |
| 613 | case ACPI_RESOURCE_TYPE_ADDRESS64: |
| 614 | case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64: |
| 615 | if (ares->data.address.resource_type == ACPI_MEMORY_RANGE) |
| 616 | type = IORESOURCE_MEM; |
| 617 | else if (ares->data.address.resource_type == ACPI_IO_RANGE) |
| 618 | type = IORESOURCE_IO; |
| 619 | else if (ares->data.address.resource_type == |
| 620 | ACPI_BUS_NUMBER_RANGE) |
| 621 | type = IORESOURCE_BUS; |
| 622 | break; |
| 623 | default: |
| 624 | break; |
| 625 | } |
| 626 | |
| 627 | return (type & types) ? 0 : 1; |
| 628 | } |
| 629 | EXPORT_SYMBOL_GPL(acpi_dev_filter_resource_type); |