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