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