blob: 100e4d9372f1fa24c30d280efa4c105bc96da6f8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * pnpacpi -- PnP ACPI driver
3 *
4 * Copyright (c) 2004 Matthieu Castet <castet.matthieu@free.fr>
5 * Copyright (c) 2004 Li Shaohua <shaohua.li@intel.com>
Bjorn Helgaas40ab4f42008-06-27 16:57:19 -06006 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
7 * Bjorn Helgaas <bjorn.helgaas@hp.com>
Bob Moore50eca3e2005-09-30 19:03:00 -04008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
12 * later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23#include <linux/kernel.h>
24#include <linux/acpi.h>
25#include <linux/pci.h>
Bjorn Helgaas02d83b52008-04-28 16:34:28 -060026#include <linux/pnp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Bjorn Helgaas02d83b52008-04-28 16:34:28 -060028#include "../base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "pnpacpi.h"
30
31#ifdef CONFIG_IA64
32#define valid_IRQ(i) (1)
33#else
34#define valid_IRQ(i) (((i) != 0) && ((i) != 2))
35#endif
36
37/*
38 * Allocated Resources
39 */
Bjorn Helgaascd7ec922008-04-28 16:34:02 -060040static int irq_flags(int triggering, int polarity, int shareable)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Bjorn Helgaascd7ec922008-04-28 16:34:02 -060042 int flags;
43
Bob Moore50eca3e2005-09-30 19:03:00 -040044 if (triggering == ACPI_LEVEL_SENSITIVE) {
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -050045 if (polarity == ACPI_ACTIVE_LOW)
Bjorn Helgaascd7ec922008-04-28 16:34:02 -060046 flags = IORESOURCE_IRQ_LOWLEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 else
Bjorn Helgaascd7ec922008-04-28 16:34:02 -060048 flags = IORESOURCE_IRQ_HIGHLEVEL;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070049 } else {
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -050050 if (polarity == ACPI_ACTIVE_LOW)
Bjorn Helgaascd7ec922008-04-28 16:34:02 -060051 flags = IORESOURCE_IRQ_LOWEDGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 else
Bjorn Helgaascd7ec922008-04-28 16:34:02 -060053 flags = IORESOURCE_IRQ_HIGHEDGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 }
Bjorn Helgaascd7ec922008-04-28 16:34:02 -060055
Bjorn Helgaasa9932732008-06-09 16:52:05 -070056 if (shareable == ACPI_SHARED)
Bjorn Helgaascd7ec922008-04-28 16:34:02 -060057 flags |= IORESOURCE_IRQ_SHAREABLE;
58
59 return flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
Bjorn Helgaase9fe9e12008-06-09 16:52:04 -070062static void decode_irq_flags(struct pnp_dev *dev, int flags, int *triggering,
Bjorn Helgaasa9932732008-06-09 16:52:05 -070063 int *polarity, int *shareable)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Bjorn Helgaase9fe9e12008-06-09 16:52:04 -070065 switch (flags & (IORESOURCE_IRQ_LOWLEVEL | IORESOURCE_IRQ_HIGHLEVEL |
66 IORESOURCE_IRQ_LOWEDGE | IORESOURCE_IRQ_HIGHEDGE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 case IORESOURCE_IRQ_LOWLEVEL:
Bob Moore50eca3e2005-09-30 19:03:00 -040068 *triggering = ACPI_LEVEL_SENSITIVE;
69 *polarity = ACPI_ACTIVE_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 break;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -050071 case IORESOURCE_IRQ_HIGHLEVEL:
Bob Moore50eca3e2005-09-30 19:03:00 -040072 *triggering = ACPI_LEVEL_SENSITIVE;
73 *polarity = ACPI_ACTIVE_HIGH;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 break;
75 case IORESOURCE_IRQ_LOWEDGE:
Bob Moore50eca3e2005-09-30 19:03:00 -040076 *triggering = ACPI_EDGE_SENSITIVE;
77 *polarity = ACPI_ACTIVE_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 break;
79 case IORESOURCE_IRQ_HIGHEDGE:
Bob Moore50eca3e2005-09-30 19:03:00 -040080 *triggering = ACPI_EDGE_SENSITIVE;
81 *polarity = ACPI_ACTIVE_HIGH;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 break;
Bjorn Helgaase9fe9e12008-06-09 16:52:04 -070083 default:
84 dev_err(&dev->dev, "can't encode invalid IRQ mode %#x\n",
85 flags);
86 *triggering = ACPI_EDGE_SENSITIVE;
87 *polarity = ACPI_ACTIVE_HIGH;
88 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
Bjorn Helgaasa9932732008-06-09 16:52:05 -070090
91 if (flags & IORESOURCE_IRQ_SHAREABLE)
92 *shareable = ACPI_SHARED;
93 else
94 *shareable = ACPI_EXCLUSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -060097static void pnpacpi_parse_allocated_irqresource(struct pnp_dev *dev,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070098 u32 gsi, int triggering,
99 int polarity, int shareable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600101 int irq, flags;
Shaohua Li61fd47e2007-11-17 01:05:28 -0500102 int p, t;
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400103
Bjorn Helgaas5acf9142008-06-27 16:56:58 -0600104 if (!valid_IRQ(gsi)) {
105 pnp_add_irq_resource(dev, gsi, IORESOURCE_DISABLED);
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400106 return;
Bjorn Helgaas5acf9142008-06-27 16:56:58 -0600107 }
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400108
Shaohua Li61fd47e2007-11-17 01:05:28 -0500109 /*
110 * in IO-APIC mode, use overrided attribute. Two reasons:
111 * 1. BIOS bug in DSDT
112 * 2. BIOS uses IO-APIC mode Interrupt Source Override
113 */
114 if (!acpi_get_override_irq(gsi, &t, &p)) {
115 t = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
116 p = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
117
118 if (triggering != t || polarity != p) {
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600119 dev_warn(&dev->dev, "IRQ %d override to %s, %s\n",
Shaohua Li61fd47e2007-11-17 01:05:28 -0500120 gsi, t ? "edge":"level", p ? "low":"high");
121 triggering = t;
122 polarity = p;
123 }
124 }
125
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600126 flags = irq_flags(triggering, polarity, shareable);
Yinghai Lua2f809b2009-04-27 18:01:20 -0700127 irq = acpi_register_gsi(&dev->dev, gsi, triggering, polarity);
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600128 if (irq >= 0)
129 pcibios_penalize_isa_irq(irq, 1);
130 else
131 flags |= IORESOURCE_DISABLED;
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400132
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600133 pnp_add_irq_resource(dev, irq, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Bjorn Helgaas958a1fd2008-08-19 16:53:21 -0600136static int dma_flags(struct pnp_dev *dev, int type, int bus_master,
137 int transfer)
Michael Karcher362ea082007-03-08 23:29:29 -0500138{
139 int flags = 0;
140
141 if (bus_master)
142 flags |= IORESOURCE_DMA_MASTER;
143 switch (type) {
144 case ACPI_COMPATIBILITY:
145 flags |= IORESOURCE_DMA_COMPATIBLE;
146 break;
147 case ACPI_TYPE_A:
148 flags |= IORESOURCE_DMA_TYPEA;
149 break;
150 case ACPI_TYPE_B:
151 flags |= IORESOURCE_DMA_TYPEB;
152 break;
153 case ACPI_TYPE_F:
154 flags |= IORESOURCE_DMA_TYPEF;
155 break;
156 default:
157 /* Set a default value ? */
158 flags |= IORESOURCE_DMA_COMPATIBLE;
Bjorn Helgaas958a1fd2008-08-19 16:53:21 -0600159 dev_err(&dev->dev, "invalid DMA type %d\n", type);
Michael Karcher362ea082007-03-08 23:29:29 -0500160 }
161 switch (transfer) {
162 case ACPI_TRANSFER_8:
163 flags |= IORESOURCE_DMA_8BIT;
164 break;
165 case ACPI_TRANSFER_8_16:
166 flags |= IORESOURCE_DMA_8AND16BIT;
167 break;
168 case ACPI_TRANSFER_16:
169 flags |= IORESOURCE_DMA_16BIT;
170 break;
171 default:
172 /* Set a default value ? */
173 flags |= IORESOURCE_DMA_8AND16BIT;
Bjorn Helgaas958a1fd2008-08-19 16:53:21 -0600174 dev_err(&dev->dev, "invalid DMA transfer type %d\n", transfer);
Michael Karcher362ea082007-03-08 23:29:29 -0500175 }
176
177 return flags;
178}
179
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600180static void pnpacpi_parse_allocated_ioresource(struct pnp_dev *dev, u64 start,
Bjorn Helgaasfa35b4922010-03-05 10:47:52 -0700181 u64 len, int io_decode,
182 int window)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600184 int flags = 0;
185 u64 end = start + len - 1;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700186
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600187 if (io_decode == ACPI_DECODE_16)
Bjorn Helgaas08c9f262008-06-27 16:57:03 -0600188 flags |= IORESOURCE_IO_16BIT_ADDR;
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600189 if (len == 0 || end >= 0x10003)
190 flags |= IORESOURCE_DISABLED;
Bjorn Helgaasfa35b4922010-03-05 10:47:52 -0700191 if (window)
192 flags |= IORESOURCE_WINDOW;
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600193
194 pnp_add_io_resource(dev, start, end, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Bjorn Helgaas40ab4f42008-06-27 16:57:19 -0600197/*
198 * Device CSRs that do not appear in PCI config space should be described
199 * via ACPI. This would normally be done with Address Space Descriptors
200 * marked as "consumer-only," but old versions of Windows and Linux ignore
201 * the producer/consumer flag, so HP invented a vendor-defined resource to
202 * describe the location and size of CSR space.
203 */
204static struct acpi_vendor_uuid hp_ccsr_uuid = {
205 .subtype = 2,
206 .data = { 0xf9, 0xad, 0xe9, 0x69, 0x4f, 0x92, 0x5f, 0xab, 0xf6, 0x4a,
207 0x24, 0xd2, 0x01, 0x37, 0x0e, 0xad },
208};
209
210static int vendor_resource_matches(struct pnp_dev *dev,
211 struct acpi_resource_vendor_typed *vendor,
212 struct acpi_vendor_uuid *match,
213 int expected_len)
214{
215 int uuid_len = sizeof(vendor->uuid);
216 u8 uuid_subtype = vendor->uuid_subtype;
217 u8 *uuid = vendor->uuid;
218 int actual_len;
219
220 /* byte_length includes uuid_subtype and uuid */
221 actual_len = vendor->byte_length - uuid_len - 1;
222
223 if (uuid_subtype == match->subtype &&
224 uuid_len == sizeof(match->data) &&
225 memcmp(uuid, match->data, uuid_len) == 0) {
226 if (expected_len && expected_len != actual_len) {
227 dev_err(&dev->dev, "wrong vendor descriptor size; "
228 "expected %d, found %d bytes\n",
229 expected_len, actual_len);
230 return 0;
231 }
232
233 return 1;
234 }
235
236 return 0;
237}
238
239static void pnpacpi_parse_allocated_vendor(struct pnp_dev *dev,
240 struct acpi_resource_vendor_typed *vendor)
241{
242 if (vendor_resource_matches(dev, vendor, &hp_ccsr_uuid, 16)) {
243 u64 start, length;
244
245 memcpy(&start, vendor->byte_data, sizeof(start));
246 memcpy(&length, vendor->byte_data + 8, sizeof(length));
247
248 pnp_add_mem_resource(dev, start, start + length - 1, 0);
249 }
250}
251
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600252static void pnpacpi_parse_allocated_memresource(struct pnp_dev *dev,
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600253 u64 start, u64 len,
Bjorn Helgaasfa35b4922010-03-05 10:47:52 -0700254 int write_protect, int window)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600256 int flags = 0;
257 u64 end = start + len - 1;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700258
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600259 if (len == 0)
260 flags |= IORESOURCE_DISABLED;
261 if (write_protect == ACPI_READ_WRITE_MEMORY)
262 flags |= IORESOURCE_MEM_WRITEABLE;
Bjorn Helgaasfa35b4922010-03-05 10:47:52 -0700263 if (window)
264 flags |= IORESOURCE_WINDOW;
Michael Karcher362ea082007-03-08 23:29:29 -0500265
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600266 pnp_add_mem_resource(dev, start, end, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
Bjorn Helgaas7e0e9c02010-03-05 10:47:57 -0700269static void pnpacpi_parse_allocated_busresource(struct pnp_dev *dev,
270 u64 start, u64 len)
271{
272 u64 end = start + len - 1;
273
274 pnp_add_bus_resource(dev, start, end);
275}
276
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600277static void pnpacpi_parse_allocated_address_space(struct pnp_dev *dev,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700278 struct acpi_resource *res)
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500279{
280 struct acpi_resource_address64 addr, *p = &addr;
281 acpi_status status;
Bjorn Helgaasfa35b4922010-03-05 10:47:52 -0700282 int window;
Bjorn Helgaas3162b6f2010-03-25 10:32:49 -0600283 u64 len;
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500284
285 status = acpi_resource_to_address64(res, p);
286 if (!ACPI_SUCCESS(status)) {
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600287 dev_warn(&dev->dev, "failed to convert resource type %d\n",
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700288 res->type);
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500289 return;
290 }
291
Bjorn Helgaasf238b412010-04-27 14:45:38 -0600292 /* Windows apparently computes length rather than using _LEN */
293 len = p->maximum - p->minimum + 1;
Bjorn Helgaasfa35b4922010-03-05 10:47:52 -0700294 window = (p->producer_consumer == ACPI_PRODUCER) ? 1 : 0;
matthieu castet2b8de5f2006-08-05 12:15:12 -0700295
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500296 if (p->resource_type == ACPI_MEMORY_RANGE)
Bjorn Helgaas3162b6f2010-03-25 10:32:49 -0600297 pnpacpi_parse_allocated_memresource(dev, p->minimum, len,
Bjorn Helgaasfa35b4922010-03-05 10:47:52 -0700298 p->info.mem.write_protect, window);
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500299 else if (p->resource_type == ACPI_IO_RANGE)
Bjorn Helgaas3162b6f2010-03-25 10:32:49 -0600300 pnpacpi_parse_allocated_ioresource(dev, p->minimum, len,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700301 p->granularity == 0xfff ? ACPI_DECODE_10 :
Bjorn Helgaasfa35b4922010-03-05 10:47:52 -0700302 ACPI_DECODE_16, window);
Bjorn Helgaas7e0e9c02010-03-05 10:47:57 -0700303 else if (p->resource_type == ACPI_BUS_NUMBER_RANGE)
Bjorn Helgaas3162b6f2010-03-25 10:32:49 -0600304 pnpacpi_parse_allocated_busresource(dev, p->minimum, len);
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500305}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600307static void pnpacpi_parse_allocated_ext_address_space(struct pnp_dev *dev,
308 struct acpi_resource *res)
309{
310 struct acpi_resource_extended_address64 *p = &res->data.ext_address64;
Bjorn Helgaasfa35b4922010-03-05 10:47:52 -0700311 int window;
Bjorn Helgaas3162b6f2010-03-25 10:32:49 -0600312 u64 len;
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600313
Bjorn Helgaasf238b412010-04-27 14:45:38 -0600314 /* Windows apparently computes length rather than using _LEN */
315 len = p->maximum - p->minimum + 1;
Bjorn Helgaasfa35b4922010-03-05 10:47:52 -0700316 window = (p->producer_consumer == ACPI_PRODUCER) ? 1 : 0;
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600317
318 if (p->resource_type == ACPI_MEMORY_RANGE)
Bjorn Helgaas3162b6f2010-03-25 10:32:49 -0600319 pnpacpi_parse_allocated_memresource(dev, p->minimum, len,
Bjorn Helgaasfa35b4922010-03-05 10:47:52 -0700320 p->info.mem.write_protect, window);
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600321 else if (p->resource_type == ACPI_IO_RANGE)
Bjorn Helgaas3162b6f2010-03-25 10:32:49 -0600322 pnpacpi_parse_allocated_ioresource(dev, p->minimum, len,
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600323 p->granularity == 0xfff ? ACPI_DECODE_10 :
Bjorn Helgaasfa35b4922010-03-05 10:47:52 -0700324 ACPI_DECODE_16, window);
Bjorn Helgaas7e0e9c02010-03-05 10:47:57 -0700325 else if (p->resource_type == ACPI_BUS_NUMBER_RANGE)
Bjorn Helgaas3162b6f2010-03-25 10:32:49 -0600326 pnpacpi_parse_allocated_busresource(dev, p->minimum, len);
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600327}
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700330 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600332 struct pnp_dev *dev = data;
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600333 struct acpi_resource_irq *irq;
334 struct acpi_resource_dma *dma;
335 struct acpi_resource_io *io;
336 struct acpi_resource_fixed_io *fixed_io;
Bjorn Helgaas40ab4f42008-06-27 16:57:19 -0600337 struct acpi_resource_vendor_typed *vendor_typed;
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600338 struct acpi_resource_memory24 *memory24;
339 struct acpi_resource_memory32 *memory32;
340 struct acpi_resource_fixed_memory32 *fixed_memory32;
341 struct acpi_resource_extended_irq *extended_irq;
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600342 int i, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Len Browneca008c2005-09-22 00:25:18 -0400344 switch (res->type) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400345 case ACPI_RESOURCE_TYPE_IRQ:
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400346 /*
347 * Per spec, only one interrupt per descriptor is allowed in
348 * _CRS, but some firmware violates this, so parse them all.
349 */
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600350 irq = &res->data.irq;
Bjorn Helgaas5acf9142008-06-27 16:56:58 -0600351 if (irq->interrupt_count == 0)
352 pnp_add_irq_resource(dev, 0, IORESOURCE_DISABLED);
353 else {
354 for (i = 0; i < irq->interrupt_count; i++) {
355 pnpacpi_parse_allocated_irqresource(dev,
356 irq->interrupts[i],
357 irq->triggering,
358 irq->polarity,
359 irq->sharable);
360 }
361
362 /*
363 * The IRQ encoder puts a single interrupt in each
364 * descriptor, so if a _CRS descriptor has more than
365 * one interrupt, we won't be able to re-encode it.
366 */
367 if (pnp_can_write(dev) && irq->interrupt_count > 1) {
368 dev_warn(&dev->dev, "multiple interrupts in "
369 "_CRS descriptor; configuration can't "
370 "be changed\n");
371 dev->capabilities &= ~PNP_WRITE;
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374 break;
375
Bob Moore50eca3e2005-09-30 19:03:00 -0400376 case ACPI_RESOURCE_TYPE_DMA:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600377 dma = &res->data.dma;
Bjorn Helgaas5acf9142008-06-27 16:56:58 -0600378 if (dma->channel_count > 0 && dma->channels[0] != (u8) -1)
Bjorn Helgaas958a1fd2008-08-19 16:53:21 -0600379 flags = dma_flags(dev, dma->type, dma->bus_master,
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600380 dma->transfer);
Bjorn Helgaas5acf9142008-06-27 16:56:58 -0600381 else
382 flags = IORESOURCE_DISABLED;
383 pnp_add_dma_resource(dev, dma->channels[0], flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 break;
Len Brown0af58532006-01-20 01:11:37 -0500385
Bob Moore50eca3e2005-09-30 19:03:00 -0400386 case ACPI_RESOURCE_TYPE_IO:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600387 io = &res->data.io;
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600388 pnpacpi_parse_allocated_ioresource(dev,
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600389 io->minimum,
390 io->address_length,
Bjorn Helgaasfa35b4922010-03-05 10:47:52 -0700391 io->io_decode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 break;
Len Brown0af58532006-01-20 01:11:37 -0500393
394 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
395 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
396 break;
397
Bob Moore50eca3e2005-09-30 19:03:00 -0400398 case ACPI_RESOURCE_TYPE_FIXED_IO:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600399 fixed_io = &res->data.fixed_io;
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600400 pnpacpi_parse_allocated_ioresource(dev,
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600401 fixed_io->address,
402 fixed_io->address_length,
Bjorn Helgaasfa35b4922010-03-05 10:47:52 -0700403 ACPI_DECODE_10, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 break;
Len Brown0af58532006-01-20 01:11:37 -0500405
406 case ACPI_RESOURCE_TYPE_VENDOR:
Bjorn Helgaas40ab4f42008-06-27 16:57:19 -0600407 vendor_typed = &res->data.vendor_typed;
408 pnpacpi_parse_allocated_vendor(dev, vendor_typed);
Len Brown0af58532006-01-20 01:11:37 -0500409 break;
410
411 case ACPI_RESOURCE_TYPE_END_TAG:
412 break;
413
Bob Moore50eca3e2005-09-30 19:03:00 -0400414 case ACPI_RESOURCE_TYPE_MEMORY24:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600415 memory24 = &res->data.memory24;
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600416 pnpacpi_parse_allocated_memresource(dev,
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600417 memory24->minimum,
418 memory24->address_length,
Bjorn Helgaasfa35b4922010-03-05 10:47:52 -0700419 memory24->write_protect, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400421 case ACPI_RESOURCE_TYPE_MEMORY32:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600422 memory32 = &res->data.memory32;
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600423 pnpacpi_parse_allocated_memresource(dev,
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600424 memory32->minimum,
425 memory32->address_length,
Bjorn Helgaasfa35b4922010-03-05 10:47:52 -0700426 memory32->write_protect, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400428 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600429 fixed_memory32 = &res->data.fixed_memory32;
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600430 pnpacpi_parse_allocated_memresource(dev,
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600431 fixed_memory32->address,
432 fixed_memory32->address_length,
Bjorn Helgaasfa35b4922010-03-05 10:47:52 -0700433 fixed_memory32->write_protect, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400435 case ACPI_RESOURCE_TYPE_ADDRESS16:
Bob Moore50eca3e2005-09-30 19:03:00 -0400436 case ACPI_RESOURCE_TYPE_ADDRESS32:
Bob Moore50eca3e2005-09-30 19:03:00 -0400437 case ACPI_RESOURCE_TYPE_ADDRESS64:
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600438 pnpacpi_parse_allocated_address_space(dev, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 break;
Len Brown0af58532006-01-20 01:11:37 -0500440
441 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600442 pnpacpi_parse_allocated_ext_address_space(dev, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 break;
Len Brown0af58532006-01-20 01:11:37 -0500444
445 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600446 extended_irq = &res->data.extended_irq;
matthieu castet2b8de5f2006-08-05 12:15:12 -0700447
Bjorn Helgaas5acf9142008-06-27 16:56:58 -0600448 if (extended_irq->interrupt_count == 0)
449 pnp_add_irq_resource(dev, 0, IORESOURCE_DISABLED);
450 else {
451 for (i = 0; i < extended_irq->interrupt_count; i++) {
452 pnpacpi_parse_allocated_irqresource(dev,
453 extended_irq->interrupts[i],
454 extended_irq->triggering,
455 extended_irq->polarity,
456 extended_irq->sharable);
457 }
458
459 /*
460 * The IRQ encoder puts a single interrupt in each
461 * descriptor, so if a _CRS descriptor has more than
462 * one interrupt, we won't be able to re-encode it.
463 */
464 if (pnp_can_write(dev) &&
465 extended_irq->interrupt_count > 1) {
466 dev_warn(&dev->dev, "multiple interrupts in "
467 "_CRS descriptor; configuration can't "
468 "be changed\n");
469 dev->capabilities &= ~PNP_WRITE;
470 }
Len Brown0af58532006-01-20 01:11:37 -0500471 }
472 break;
473
474 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
475 break;
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 default:
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600478 dev_warn(&dev->dev, "unknown resource type %d in _CRS\n",
479 res->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return AE_ERROR;
481 }
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return AE_OK;
484}
485
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600486int pnpacpi_parse_allocated_resource(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Bjorn Helgaasc4da6942009-11-17 17:05:14 -0700488 struct acpi_device *acpi_dev = dev->data;
489 acpi_handle handle = acpi_dev->handle;
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600490 acpi_status status;
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600491
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600492 pnp_dbg(&dev->dev, "parse allocated resources\n");
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600493
Bjorn Helgaasf4490002008-04-28 16:34:09 -0600494 pnp_init_resources(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600496 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
497 pnpacpi_allocated_resource, dev);
498
499 if (ACPI_FAILURE(status)) {
500 if (status != AE_NOT_FOUND)
501 dev_err(&dev->dev, "can't evaluate _CRS: %d", status);
502 return -EPERM;
503 }
504 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600507static __init void pnpacpi_parse_dma_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600508 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800509 struct acpi_resource_dma *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 int i;
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600512 unsigned char map = 0, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Bob Moore50eca3e2005-09-30 19:03:00 -0400514 if (p->channel_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700517 for (i = 0; i < p->channel_count; i++)
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600518 map |= 1 << p->channels[i];
Michael Karcher362ea082007-03-08 23:29:29 -0500519
Bjorn Helgaas958a1fd2008-08-19 16:53:21 -0600520 flags = dma_flags(dev, p->type, p->bus_master, p->transfer);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600521 pnp_register_dma_resource(dev, option_flags, map, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600524static __init void pnpacpi_parse_irq_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600525 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800526 struct acpi_resource_irq *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 int i;
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600529 pnp_irq_mask_t map;
530 unsigned char flags;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500531
Bob Moore50eca3e2005-09-30 19:03:00 -0400532 if (p->interrupt_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600535 bitmap_zero(map.bits, PNP_IRQ_NR);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700536 for (i = 0; i < p->interrupt_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (p->interrupts[i])
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600538 __set_bit(p->interrupts[i], map.bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600540 flags = irq_flags(p->triggering, p->polarity, p->sharable);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600541 pnp_register_irq_resource(dev, option_flags, &map, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600544static __init void pnpacpi_parse_ext_irq_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600545 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800546 struct acpi_resource_extended_irq *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 int i;
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600549 pnp_irq_mask_t map;
550 unsigned char flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Bob Moore50eca3e2005-09-30 19:03:00 -0400552 if (p->interrupt_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600555 bitmap_zero(map.bits, PNP_IRQ_NR);
Bjorn Helgaasfe2cf592008-06-27 16:57:12 -0600556 for (i = 0; i < p->interrupt_count; i++) {
557 if (p->interrupts[i]) {
558 if (p->interrupts[i] < PNP_IRQ_NR)
559 __set_bit(p->interrupts[i], map.bits);
560 else
561 dev_err(&dev->dev, "ignoring IRQ %d option "
562 "(too large for %d entry bitmap)\n",
563 p->interrupts[i], PNP_IRQ_NR);
564 }
565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600567 flags = irq_flags(p->triggering, p->polarity, p->sharable);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600568 pnp_register_irq_resource(dev, option_flags, &map, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600571static __init void pnpacpi_parse_port_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600572 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800573 struct acpi_resource_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600575 unsigned char flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Bob Moore50eca3e2005-09-30 19:03:00 -0400577 if (io->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return;
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600579
580 if (io->io_decode == ACPI_DECODE_16)
581 flags = IORESOURCE_IO_16BIT_ADDR;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600582 pnp_register_port_resource(dev, option_flags, io->minimum, io->maximum,
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600583 io->alignment, io->address_length, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600586static __init void pnpacpi_parse_fixed_port_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600587 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800588 struct acpi_resource_fixed_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Bob Moore50eca3e2005-09-30 19:03:00 -0400590 if (io->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return;
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600592
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600593 pnp_register_port_resource(dev, option_flags, io->address, io->address,
594 0, io->address_length, IORESOURCE_IO_FIXED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600597static __init void pnpacpi_parse_mem24_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600598 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800599 struct acpi_resource_memory24 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600601 unsigned char flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Bob Moore50eca3e2005-09-30 19:03:00 -0400603 if (p->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600606 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
607 flags = IORESOURCE_MEM_WRITEABLE;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600608 pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600609 p->alignment, p->address_length, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600612static __init void pnpacpi_parse_mem32_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600613 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800614 struct acpi_resource_memory32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600616 unsigned char flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Bob Moore50eca3e2005-09-30 19:03:00 -0400618 if (p->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600621 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
622 flags = IORESOURCE_MEM_WRITEABLE;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600623 pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600624 p->alignment, p->address_length, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600627static __init void pnpacpi_parse_fixed_mem32_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600628 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800629 struct acpi_resource_fixed_memory32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600631 unsigned char flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Bob Moore50eca3e2005-09-30 19:03:00 -0400633 if (p->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600636 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
637 flags = IORESOURCE_MEM_WRITEABLE;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600638 pnp_register_mem_resource(dev, option_flags, p->address, p->address,
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600639 0, p->address_length, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600642static __init void pnpacpi_parse_address_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600643 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800644 struct acpi_resource *r)
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400645{
646 struct acpi_resource_address64 addr, *p = &addr;
647 acpi_status status;
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600648 unsigned char flags = 0;
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400649
650 status = acpi_resource_to_address64(r, p);
Bjorn Helgaas958a1fd2008-08-19 16:53:21 -0600651 if (ACPI_FAILURE(status)) {
652 dev_warn(&dev->dev, "can't convert resource type %d\n",
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700653 r->type);
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400654 return;
655 }
656
657 if (p->address_length == 0)
658 return;
659
660 if (p->resource_type == ACPI_MEMORY_RANGE) {
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600661 if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
662 flags = IORESOURCE_MEM_WRITEABLE;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600663 pnp_register_mem_resource(dev, option_flags, p->minimum,
664 p->minimum, 0, p->address_length,
665 flags);
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600666 } else if (p->resource_type == ACPI_IO_RANGE)
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600667 pnp_register_port_resource(dev, option_flags, p->minimum,
668 p->minimum, 0, p->address_length,
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600669 IORESOURCE_IO_FIXED);
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400670}
671
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600672static __init void pnpacpi_parse_ext_address_option(struct pnp_dev *dev,
673 unsigned int option_flags,
674 struct acpi_resource *r)
675{
676 struct acpi_resource_extended_address64 *p = &r->data.ext_address64;
677 unsigned char flags = 0;
678
679 if (p->address_length == 0)
680 return;
681
682 if (p->resource_type == ACPI_MEMORY_RANGE) {
683 if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
684 flags = IORESOURCE_MEM_WRITEABLE;
685 pnp_register_mem_resource(dev, option_flags, p->minimum,
686 p->minimum, 0, p->address_length,
687 flags);
688 } else if (p->resource_type == ACPI_IO_RANGE)
689 pnp_register_port_resource(dev, option_flags, p->minimum,
690 p->minimum, 0, p->address_length,
691 IORESOURCE_IO_FIXED);
692}
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694struct acpipnp_parse_option_s {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 struct pnp_dev *dev;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600696 unsigned int option_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697};
698
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800699static __init acpi_status pnpacpi_option_resource(struct acpi_resource *res,
700 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Bjorn Helgaase2a1a6f2008-06-27 16:57:15 -0600702 int priority;
Bjorn Helgaas4721a4cc2007-08-15 10:32:10 -0600703 struct acpipnp_parse_option_s *parse_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 struct pnp_dev *dev = parse_data->dev;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600705 unsigned int option_flags = parse_data->option_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Len Browneca008c2005-09-22 00:25:18 -0400707 switch (res->type) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700708 case ACPI_RESOURCE_TYPE_IRQ:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600709 pnpacpi_parse_irq_option(dev, option_flags, &res->data.irq);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700710 break;
711
712 case ACPI_RESOURCE_TYPE_DMA:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600713 pnpacpi_parse_dma_option(dev, option_flags, &res->data.dma);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700714 break;
715
716 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
717 switch (res->data.start_dpf.compatibility_priority) {
718 case ACPI_GOOD_CONFIGURATION:
719 priority = PNP_RES_PRIORITY_PREFERRED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 break;
Len Brown0af58532006-01-20 01:11:37 -0500721
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700722 case ACPI_ACCEPTABLE_CONFIGURATION:
723 priority = PNP_RES_PRIORITY_ACCEPTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 break;
Len Brown0af58532006-01-20 01:11:37 -0500725
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700726 case ACPI_SUB_OPTIMAL_CONFIGURATION:
727 priority = PNP_RES_PRIORITY_FUNCTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 default:
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700730 priority = PNP_RES_PRIORITY_INVALID;
731 break;
732 }
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600733 parse_data->option_flags = pnp_new_dependent_set(dev, priority);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700734 break;
735
736 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600737 parse_data->option_flags = 0;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700738 break;
739
740 case ACPI_RESOURCE_TYPE_IO:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600741 pnpacpi_parse_port_option(dev, option_flags, &res->data.io);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700742 break;
743
744 case ACPI_RESOURCE_TYPE_FIXED_IO:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600745 pnpacpi_parse_fixed_port_option(dev, option_flags,
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600746 &res->data.fixed_io);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700747 break;
748
749 case ACPI_RESOURCE_TYPE_VENDOR:
750 case ACPI_RESOURCE_TYPE_END_TAG:
751 break;
752
753 case ACPI_RESOURCE_TYPE_MEMORY24:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600754 pnpacpi_parse_mem24_option(dev, option_flags,
755 &res->data.memory24);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700756 break;
757
758 case ACPI_RESOURCE_TYPE_MEMORY32:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600759 pnpacpi_parse_mem32_option(dev, option_flags,
760 &res->data.memory32);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700761 break;
762
763 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600764 pnpacpi_parse_fixed_mem32_option(dev, option_flags,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700765 &res->data.fixed_memory32);
766 break;
767
768 case ACPI_RESOURCE_TYPE_ADDRESS16:
769 case ACPI_RESOURCE_TYPE_ADDRESS32:
770 case ACPI_RESOURCE_TYPE_ADDRESS64:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600771 pnpacpi_parse_address_option(dev, option_flags, res);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700772 break;
773
774 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600775 pnpacpi_parse_ext_address_option(dev, option_flags, res);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700776 break;
777
778 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600779 pnpacpi_parse_ext_irq_option(dev, option_flags,
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600780 &res->data.extended_irq);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700781 break;
782
783 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
784 break;
785
786 default:
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600787 dev_warn(&dev->dev, "unknown resource type %d in _PRS\n",
788 res->type);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700789 return AE_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return AE_OK;
793}
794
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600795int __init pnpacpi_parse_resource_option_data(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Bjorn Helgaasc4da6942009-11-17 17:05:14 -0700797 struct acpi_device *acpi_dev = dev->data;
798 acpi_handle handle = acpi_dev->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 acpi_status status;
800 struct acpipnp_parse_option_s parse_data;
801
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600802 pnp_dbg(&dev->dev, "parse resource options\n");
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 parse_data.dev = dev;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600805 parse_data.option_flags = 0;
806
Bob Moore50eca3e2005-09-30 19:03:00 -0400807 status = acpi_walk_resources(handle, METHOD_NAME__PRS,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700808 pnpacpi_option_resource, &parse_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600810 if (ACPI_FAILURE(status)) {
811 if (status != AE_NOT_FOUND)
812 dev_err(&dev->dev, "can't evaluate _PRS: %d", status);
813 return -EPERM;
814 }
815 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
Bjorn Helgaasb5f2490b2006-03-28 17:04:00 -0500818static int pnpacpi_supported_resource(struct acpi_resource *res)
819{
820 switch (res->type) {
821 case ACPI_RESOURCE_TYPE_IRQ:
822 case ACPI_RESOURCE_TYPE_DMA:
823 case ACPI_RESOURCE_TYPE_IO:
824 case ACPI_RESOURCE_TYPE_FIXED_IO:
825 case ACPI_RESOURCE_TYPE_MEMORY24:
826 case ACPI_RESOURCE_TYPE_MEMORY32:
827 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
828 case ACPI_RESOURCE_TYPE_ADDRESS16:
829 case ACPI_RESOURCE_TYPE_ADDRESS32:
830 case ACPI_RESOURCE_TYPE_ADDRESS64:
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600831 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
Bjorn Helgaasb5f2490b2006-03-28 17:04:00 -0500832 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
833 return 1;
834 }
835 return 0;
836}
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838/*
839 * Set resource
840 */
841static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700842 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Bjorn Helgaas4721a4cc2007-08-15 10:32:10 -0600844 int *res_cnt = data;
Bjorn Helgaasb5f2490b2006-03-28 17:04:00 -0500845
846 if (pnpacpi_supported_resource(res))
847 (*res_cnt)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return AE_OK;
849}
850
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500851static acpi_status pnpacpi_type_resources(struct acpi_resource *res, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Bjorn Helgaas4721a4cc2007-08-15 10:32:10 -0600853 struct acpi_resource **resource = data;
Bjorn Helgaasb5f2490b2006-03-28 17:04:00 -0500854
855 if (pnpacpi_supported_resource(res)) {
Len Browneca008c2005-09-22 00:25:18 -0400856 (*resource)->type = res->type;
Bjorn Helgaasb5f2490b2006-03-28 17:04:00 -0500857 (*resource)->length = sizeof(struct acpi_resource);
Bjorn Helgaas36d872a2008-06-09 16:52:06 -0700858 if (res->type == ACPI_RESOURCE_TYPE_IRQ)
859 (*resource)->data.irq.descriptor_length =
860 res->data.irq.descriptor_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 (*resource)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863
864 return AE_OK;
865}
866
Bjorn Helgaascdef6252008-04-28 16:34:03 -0600867int pnpacpi_build_resource_template(struct pnp_dev *dev,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700868 struct acpi_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Bjorn Helgaasc4da6942009-11-17 17:05:14 -0700870 struct acpi_device *acpi_dev = dev->data;
871 acpi_handle handle = acpi_dev->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 struct acpi_resource *resource;
873 int res_cnt = 0;
874 acpi_status status;
875
Bob Moore50eca3e2005-09-30 19:03:00 -0400876 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700877 pnpacpi_count_resources, &res_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (ACPI_FAILURE(status)) {
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600879 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return -EINVAL;
881 }
882 if (!res_cnt)
883 return -EINVAL;
884 buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
Robert P. J. Daycd861282006-12-13 00:34:52 -0800885 buffer->pointer = kzalloc(buffer->length - 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (!buffer->pointer)
887 return -ENOMEM;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 resource = (struct acpi_resource *)buffer->pointer;
Bob Moore50eca3e2005-09-30 19:03:00 -0400890 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700891 pnpacpi_type_resources, &resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (ACPI_FAILURE(status)) {
893 kfree(buffer->pointer);
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600894 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return -EINVAL;
896 }
897 /* resource will pointer the end resource now */
Bob Moore50eca3e2005-09-30 19:03:00 -0400898 resource->type = ACPI_RESOURCE_TYPE_END_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 return 0;
901}
902
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600903static void pnpacpi_encode_irq(struct pnp_dev *dev,
904 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700905 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600907 struct acpi_resource_irq *irq = &resource->data.irq;
Bjorn Helgaasa9932732008-06-09 16:52:05 -0700908 int triggering, polarity, shareable;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500909
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600910 if (!pnp_resource_enabled(p)) {
911 irq->interrupt_count = 0;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600912 pnp_dbg(&dev->dev, " encode irq (%s)\n",
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600913 p ? "disabled" : "missing");
914 return;
915 }
916
Bjorn Helgaasa9932732008-06-09 16:52:05 -0700917 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600918 irq->triggering = triggering;
919 irq->polarity = polarity;
Bjorn Helgaasa9932732008-06-09 16:52:05 -0700920 irq->sharable = shareable;
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600921 irq->interrupt_count = 1;
922 irq->interrupts[0] = p->start;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600923
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600924 pnp_dbg(&dev->dev, " encode irq %d %s %s %s (%d-byte descriptor)\n",
Bjorn Helgaas36d872a2008-06-09 16:52:06 -0700925 (int) p->start,
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600926 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
927 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
Bjorn Helgaas36d872a2008-06-09 16:52:06 -0700928 irq->sharable == ACPI_SHARED ? "shared" : "exclusive",
929 irq->descriptor_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600932static void pnpacpi_encode_ext_irq(struct pnp_dev *dev,
933 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700934 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600936 struct acpi_resource_extended_irq *extended_irq = &resource->data.extended_irq;
Bjorn Helgaasa9932732008-06-09 16:52:05 -0700937 int triggering, polarity, shareable;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500938
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600939 if (!pnp_resource_enabled(p)) {
940 extended_irq->interrupt_count = 0;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600941 pnp_dbg(&dev->dev, " encode extended irq (%s)\n",
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600942 p ? "disabled" : "missing");
943 return;
944 }
945
Bjorn Helgaasa9932732008-06-09 16:52:05 -0700946 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600947 extended_irq->producer_consumer = ACPI_CONSUMER;
948 extended_irq->triggering = triggering;
949 extended_irq->polarity = polarity;
Bjorn Helgaasa9932732008-06-09 16:52:05 -0700950 extended_irq->sharable = shareable;
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600951 extended_irq->interrupt_count = 1;
952 extended_irq->interrupts[0] = p->start;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600953
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600954 pnp_dbg(&dev->dev, " encode irq %d %s %s %s\n", (int) p->start,
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600955 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
956 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
957 extended_irq->sharable == ACPI_SHARED ? "shared" : "exclusive");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600960static void pnpacpi_encode_dma(struct pnp_dev *dev,
961 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700962 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600964 struct acpi_resource_dma *dma = &resource->data.dma;
965
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600966 if (!pnp_resource_enabled(p)) {
967 dma->channel_count = 0;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600968 pnp_dbg(&dev->dev, " encode dma (%s)\n",
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600969 p ? "disabled" : "missing");
970 return;
971 }
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
Vojtech Pavlikccc4c7b2006-04-07 20:00:27 +0200974 switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700975 case IORESOURCE_DMA_TYPEA:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600976 dma->type = ACPI_TYPE_A;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700977 break;
978 case IORESOURCE_DMA_TYPEB:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600979 dma->type = ACPI_TYPE_B;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700980 break;
981 case IORESOURCE_DMA_TYPEF:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600982 dma->type = ACPI_TYPE_F;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700983 break;
984 default:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600985 dma->type = ACPI_COMPATIBILITY;
Vojtech Pavlikccc4c7b2006-04-07 20:00:27 +0200986 }
987
988 switch (p->flags & IORESOURCE_DMA_TYPE_MASK) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700989 case IORESOURCE_DMA_8BIT:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600990 dma->transfer = ACPI_TRANSFER_8;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700991 break;
992 case IORESOURCE_DMA_8AND16BIT:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600993 dma->transfer = ACPI_TRANSFER_8_16;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700994 break;
995 default:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600996 dma->transfer = ACPI_TRANSFER_16;
Vojtech Pavlikccc4c7b2006-04-07 20:00:27 +0200997 }
998
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600999 dma->bus_master = !!(p->flags & IORESOURCE_DMA_MASTER);
1000 dma->channel_count = 1;
1001 dma->channels[0] = p->start;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001002
Bjorn Helgaas2f534322008-08-19 16:53:47 -06001003 pnp_dbg(&dev->dev, " encode dma %d "
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001004 "type %#x transfer %#x master %d\n",
1005 (int) p->start, dma->type, dma->transfer, dma->bus_master);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001008static void pnpacpi_encode_io(struct pnp_dev *dev,
1009 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -07001010 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Bjorn Helgaas9570a202008-04-28 16:34:00 -06001012 struct acpi_resource_io *io = &resource->data.io;
1013
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -06001014 if (pnp_resource_enabled(p)) {
1015 /* Note: pnp_assign_port copies pnp_port->flags into p->flags */
Bjorn Helgaas08c9f262008-06-27 16:57:03 -06001016 io->io_decode = (p->flags & IORESOURCE_IO_16BIT_ADDR) ?
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -06001017 ACPI_DECODE_16 : ACPI_DECODE_10;
1018 io->minimum = p->start;
1019 io->maximum = p->end;
1020 io->alignment = 0; /* Correct? */
1021 io->address_length = p->end - p->start + 1;
1022 } else {
1023 io->minimum = 0;
1024 io->address_length = 0;
1025 }
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001026
Bjorn Helgaas2f534322008-08-19 16:53:47 -06001027 pnp_dbg(&dev->dev, " encode io %#x-%#x decode %#x\n", io->minimum,
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -06001028 io->minimum + io->address_length - 1, io->io_decode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001031static void pnpacpi_encode_fixed_io(struct pnp_dev *dev,
1032 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -07001033 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Bjorn Helgaas9570a202008-04-28 16:34:00 -06001035 struct acpi_resource_fixed_io *fixed_io = &resource->data.fixed_io;
1036
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -06001037 if (pnp_resource_enabled(p)) {
1038 fixed_io->address = p->start;
1039 fixed_io->address_length = p->end - p->start + 1;
1040 } else {
1041 fixed_io->address = 0;
1042 fixed_io->address_length = 0;
1043 }
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001044
Bjorn Helgaas2f534322008-08-19 16:53:47 -06001045 pnp_dbg(&dev->dev, " encode fixed_io %#x-%#x\n", fixed_io->address,
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -06001046 fixed_io->address + fixed_io->address_length - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001049static void pnpacpi_encode_mem24(struct pnp_dev *dev,
1050 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -07001051 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Bjorn Helgaas9570a202008-04-28 16:34:00 -06001053 struct acpi_resource_memory24 *memory24 = &resource->data.memory24;
1054
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -06001055 if (pnp_resource_enabled(p)) {
1056 /* Note: pnp_assign_mem copies pnp_mem->flags into p->flags */
1057 memory24->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
1058 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1059 memory24->minimum = p->start;
1060 memory24->maximum = p->end;
1061 memory24->alignment = 0;
1062 memory24->address_length = p->end - p->start + 1;
1063 } else {
1064 memory24->minimum = 0;
1065 memory24->address_length = 0;
1066 }
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001067
Bjorn Helgaas2f534322008-08-19 16:53:47 -06001068 pnp_dbg(&dev->dev, " encode mem24 %#x-%#x write_protect %#x\n",
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -06001069 memory24->minimum,
1070 memory24->minimum + memory24->address_length - 1,
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001071 memory24->write_protect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001074static void pnpacpi_encode_mem32(struct pnp_dev *dev,
1075 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -07001076 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Bjorn Helgaas9570a202008-04-28 16:34:00 -06001078 struct acpi_resource_memory32 *memory32 = &resource->data.memory32;
1079
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -06001080 if (pnp_resource_enabled(p)) {
1081 memory32->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
1082 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1083 memory32->minimum = p->start;
1084 memory32->maximum = p->end;
1085 memory32->alignment = 0;
1086 memory32->address_length = p->end - p->start + 1;
1087 } else {
1088 memory32->minimum = 0;
1089 memory32->alignment = 0;
1090 }
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001091
Bjorn Helgaas2f534322008-08-19 16:53:47 -06001092 pnp_dbg(&dev->dev, " encode mem32 %#x-%#x write_protect %#x\n",
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -06001093 memory32->minimum,
1094 memory32->minimum + memory32->address_length - 1,
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001095 memory32->write_protect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001098static void pnpacpi_encode_fixed_mem32(struct pnp_dev *dev,
1099 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -07001100 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
Bjorn Helgaas9570a202008-04-28 16:34:00 -06001102 struct acpi_resource_fixed_memory32 *fixed_memory32 = &resource->data.fixed_memory32;
1103
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -06001104 if (pnp_resource_enabled(p)) {
1105 fixed_memory32->write_protect =
1106 p->flags & IORESOURCE_MEM_WRITEABLE ?
1107 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1108 fixed_memory32->address = p->start;
1109 fixed_memory32->address_length = p->end - p->start + 1;
1110 } else {
1111 fixed_memory32->address = 0;
1112 fixed_memory32->address_length = 0;
1113 }
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001114
Bjorn Helgaas2f534322008-08-19 16:53:47 -06001115 pnp_dbg(&dev->dev, " encode fixed_mem32 %#x-%#x write_protect %#x\n",
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -06001116 fixed_memory32->address,
1117 fixed_memory32->address + fixed_memory32->address_length - 1,
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001118 fixed_memory32->write_protect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -06001121int pnpacpi_encode_resources(struct pnp_dev *dev, struct acpi_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123 int i = 0;
1124 /* pnpacpi_build_resource_template allocates extra mem */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -07001125 int res_cnt = (buffer->length - 1) / sizeof(struct acpi_resource) - 1;
Bjorn Helgaas4721a4cc2007-08-15 10:32:10 -06001126 struct acpi_resource *resource = buffer->pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 int port = 0, irq = 0, dma = 0, mem = 0;
1128
Bjorn Helgaas2f534322008-08-19 16:53:47 -06001129 pnp_dbg(&dev->dev, "encode %d resources\n", res_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 while (i < res_cnt) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -07001131 switch (resource->type) {
Bob Moore50eca3e2005-09-30 19:03:00 -04001132 case ACPI_RESOURCE_TYPE_IRQ:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001133 pnpacpi_encode_irq(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -06001134 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 irq++;
1136 break;
1137
Bob Moore50eca3e2005-09-30 19:03:00 -04001138 case ACPI_RESOURCE_TYPE_DMA:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001139 pnpacpi_encode_dma(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -06001140 pnp_get_resource(dev, IORESOURCE_DMA, dma));
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -05001141 dma++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 break;
Bob Moore50eca3e2005-09-30 19:03:00 -04001143 case ACPI_RESOURCE_TYPE_IO:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001144 pnpacpi_encode_io(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -06001145 pnp_get_resource(dev, IORESOURCE_IO, port));
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -05001146 port++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 break;
Bob Moore50eca3e2005-09-30 19:03:00 -04001148 case ACPI_RESOURCE_TYPE_FIXED_IO:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001149 pnpacpi_encode_fixed_io(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -06001150 pnp_get_resource(dev, IORESOURCE_IO, port));
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -05001151 port++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 break;
Bob Moore50eca3e2005-09-30 19:03:00 -04001153 case ACPI_RESOURCE_TYPE_MEMORY24:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001154 pnpacpi_encode_mem24(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -06001155 pnp_get_resource(dev, IORESOURCE_MEM, mem));
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -05001156 mem++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 break;
Bob Moore50eca3e2005-09-30 19:03:00 -04001158 case ACPI_RESOURCE_TYPE_MEMORY32:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001159 pnpacpi_encode_mem32(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -06001160 pnp_get_resource(dev, IORESOURCE_MEM, mem));
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -05001161 mem++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 break;
Bob Moore50eca3e2005-09-30 19:03:00 -04001163 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001164 pnpacpi_encode_fixed_mem32(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -06001165 pnp_get_resource(dev, IORESOURCE_MEM, mem));
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -05001166 mem++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 break;
Len Brown0af58532006-01-20 01:11:37 -05001168 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001169 pnpacpi_encode_ext_irq(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -06001170 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
Len Brown0af58532006-01-20 01:11:37 -05001171 irq++;
1172 break;
1173 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1174 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
1175 case ACPI_RESOURCE_TYPE_VENDOR:
1176 case ACPI_RESOURCE_TYPE_END_TAG:
1177 case ACPI_RESOURCE_TYPE_ADDRESS16:
1178 case ACPI_RESOURCE_TYPE_ADDRESS32:
1179 case ACPI_RESOURCE_TYPE_ADDRESS64:
1180 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
1181 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
Bjorn Helgaas9dd78462007-07-26 10:41:20 -07001182 default: /* other type */
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -06001183 dev_warn(&dev->dev, "can't encode unknown resource "
1184 "type %d\n", resource->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return -EINVAL;
1186 }
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -05001187 resource++;
1188 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
1190 return 0;
1191}