blob: 05796495be0e4ada9ba9b2995be1698c79fed1e8 [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
Fabian Frederickbbee06d2015-05-02 17:14:49 +020031static void decode_irq_flags(struct pnp_dev *dev, int flags, u8 *triggering,
32 u8 *polarity, u8 *shareable)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Bjorn Helgaase9fe9e12008-06-09 16:52:04 -070034 switch (flags & (IORESOURCE_IRQ_LOWLEVEL | IORESOURCE_IRQ_HIGHLEVEL |
35 IORESOURCE_IRQ_LOWEDGE | IORESOURCE_IRQ_HIGHEDGE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 case IORESOURCE_IRQ_LOWLEVEL:
Bob Moore50eca3e2005-09-30 19:03:00 -040037 *triggering = ACPI_LEVEL_SENSITIVE;
38 *polarity = ACPI_ACTIVE_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 break;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -050040 case IORESOURCE_IRQ_HIGHLEVEL:
Bob Moore50eca3e2005-09-30 19:03:00 -040041 *triggering = ACPI_LEVEL_SENSITIVE;
42 *polarity = ACPI_ACTIVE_HIGH;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 break;
44 case IORESOURCE_IRQ_LOWEDGE:
Bob Moore50eca3e2005-09-30 19:03:00 -040045 *triggering = ACPI_EDGE_SENSITIVE;
46 *polarity = ACPI_ACTIVE_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 break;
48 case IORESOURCE_IRQ_HIGHEDGE:
Bob Moore50eca3e2005-09-30 19:03:00 -040049 *triggering = ACPI_EDGE_SENSITIVE;
50 *polarity = ACPI_ACTIVE_HIGH;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 break;
Bjorn Helgaase9fe9e12008-06-09 16:52:04 -070052 default:
53 dev_err(&dev->dev, "can't encode invalid IRQ mode %#x\n",
54 flags);
55 *triggering = ACPI_EDGE_SENSITIVE;
56 *polarity = ACPI_ACTIVE_HIGH;
57 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 }
Bjorn Helgaasa9932732008-06-09 16:52:05 -070059
60 if (flags & IORESOURCE_IRQ_SHAREABLE)
61 *shareable = ACPI_SHARED;
62 else
63 *shareable = ACPI_EXCLUSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
Bjorn Helgaas958a1fd2008-08-19 16:53:21 -060066static int dma_flags(struct pnp_dev *dev, int type, int bus_master,
67 int transfer)
Michael Karcher362ea082007-03-08 23:29:29 -050068{
69 int flags = 0;
70
71 if (bus_master)
72 flags |= IORESOURCE_DMA_MASTER;
73 switch (type) {
74 case ACPI_COMPATIBILITY:
75 flags |= IORESOURCE_DMA_COMPATIBLE;
76 break;
77 case ACPI_TYPE_A:
78 flags |= IORESOURCE_DMA_TYPEA;
79 break;
80 case ACPI_TYPE_B:
81 flags |= IORESOURCE_DMA_TYPEB;
82 break;
83 case ACPI_TYPE_F:
84 flags |= IORESOURCE_DMA_TYPEF;
85 break;
86 default:
87 /* Set a default value ? */
88 flags |= IORESOURCE_DMA_COMPATIBLE;
Bjorn Helgaas958a1fd2008-08-19 16:53:21 -060089 dev_err(&dev->dev, "invalid DMA type %d\n", type);
Michael Karcher362ea082007-03-08 23:29:29 -050090 }
91 switch (transfer) {
92 case ACPI_TRANSFER_8:
93 flags |= IORESOURCE_DMA_8BIT;
94 break;
95 case ACPI_TRANSFER_8_16:
96 flags |= IORESOURCE_DMA_8AND16BIT;
97 break;
98 case ACPI_TRANSFER_16:
99 flags |= IORESOURCE_DMA_16BIT;
100 break;
101 default:
102 /* Set a default value ? */
103 flags |= IORESOURCE_DMA_8AND16BIT;
Bjorn Helgaas958a1fd2008-08-19 16:53:21 -0600104 dev_err(&dev->dev, "invalid DMA transfer type %d\n", transfer);
Michael Karcher362ea082007-03-08 23:29:29 -0500105 }
106
107 return flags;
108}
109
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100110/*
111 * Allocated Resources
112 */
113
114static void pnpacpi_add_irqresource(struct pnp_dev *dev, struct resource *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100116 if (!(r->flags & IORESOURCE_DISABLED))
117 pcibios_penalize_isa_irq(r->start, 1);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700118
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100119 pnp_add_resource(dev, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Bjorn Helgaas40ab4f42008-06-27 16:57:19 -0600122/*
123 * Device CSRs that do not appear in PCI config space should be described
124 * via ACPI. This would normally be done with Address Space Descriptors
125 * marked as "consumer-only," but old versions of Windows and Linux ignore
126 * the producer/consumer flag, so HP invented a vendor-defined resource to
127 * describe the location and size of CSR space.
128 */
129static struct acpi_vendor_uuid hp_ccsr_uuid = {
130 .subtype = 2,
131 .data = { 0xf9, 0xad, 0xe9, 0x69, 0x4f, 0x92, 0x5f, 0xab, 0xf6, 0x4a,
132 0x24, 0xd2, 0x01, 0x37, 0x0e, 0xad },
133};
134
135static int vendor_resource_matches(struct pnp_dev *dev,
136 struct acpi_resource_vendor_typed *vendor,
137 struct acpi_vendor_uuid *match,
138 int expected_len)
139{
140 int uuid_len = sizeof(vendor->uuid);
141 u8 uuid_subtype = vendor->uuid_subtype;
142 u8 *uuid = vendor->uuid;
143 int actual_len;
144
145 /* byte_length includes uuid_subtype and uuid */
146 actual_len = vendor->byte_length - uuid_len - 1;
147
148 if (uuid_subtype == match->subtype &&
149 uuid_len == sizeof(match->data) &&
150 memcmp(uuid, match->data, uuid_len) == 0) {
151 if (expected_len && expected_len != actual_len) {
152 dev_err(&dev->dev, "wrong vendor descriptor size; "
153 "expected %d, found %d bytes\n",
154 expected_len, actual_len);
155 return 0;
156 }
157
158 return 1;
159 }
160
161 return 0;
162}
163
164static void pnpacpi_parse_allocated_vendor(struct pnp_dev *dev,
165 struct acpi_resource_vendor_typed *vendor)
166{
167 if (vendor_resource_matches(dev, vendor, &hp_ccsr_uuid, 16)) {
168 u64 start, length;
169
170 memcpy(&start, vendor->byte_data, sizeof(start));
171 memcpy(&length, vendor->byte_data + 8, sizeof(length));
172
173 pnp_add_mem_resource(dev, start, start + length - 1, 0);
174 }
175}
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700178 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600180 struct pnp_dev *dev = data;
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600181 struct acpi_resource_dma *dma;
Bjorn Helgaas40ab4f42008-06-27 16:57:19 -0600182 struct acpi_resource_vendor_typed *vendor_typed;
Jiang Liua49170b2015-02-02 10:42:58 +0800183 struct resource_win win = {{0}, 0};
184 struct resource *r = &win.res;
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600185 int i, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Jiang Liua49170b2015-02-02 10:42:58 +0800187 if (acpi_dev_resource_address_space(res, &win)
188 || acpi_dev_resource_ext_address_space(res, &win)) {
189 pnp_add_resource(dev, &win.res);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100190 return AE_OK;
191 }
Bjorn Helgaas5acf9142008-06-27 16:56:58 -0600192
Jiang Liua49170b2015-02-02 10:42:58 +0800193 r->flags = 0;
194 if (acpi_dev_resource_interrupt(res, 0, r)) {
195 pnpacpi_add_irqresource(dev, r);
196 for (i = 1; acpi_dev_resource_interrupt(res, i, r); i++)
197 pnpacpi_add_irqresource(dev, r);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100198
199 if (i > 1) {
Bjorn Helgaas5acf9142008-06-27 16:56:58 -0600200 /*
201 * The IRQ encoder puts a single interrupt in each
202 * descriptor, so if a _CRS descriptor has more than
203 * one interrupt, we won't be able to re-encode it.
204 */
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100205 if (pnp_can_write(dev)) {
Bjorn Helgaas5acf9142008-06-27 16:56:58 -0600206 dev_warn(&dev->dev, "multiple interrupts in "
207 "_CRS descriptor; configuration can't "
208 "be changed\n");
209 dev->capabilities &= ~PNP_WRITE;
210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100212 return AE_OK;
Jiang Liua49170b2015-02-02 10:42:58 +0800213 } else if (r->flags & IORESOURCE_DISABLED) {
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100214 pnp_add_irq_resource(dev, 0, IORESOURCE_DISABLED);
215 return AE_OK;
216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100218 switch (res->type) {
Zhang Rui89935312014-03-11 22:40:27 +0800219 case ACPI_RESOURCE_TYPE_MEMORY24:
220 case ACPI_RESOURCE_TYPE_MEMORY32:
221 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
Jiang Liua49170b2015-02-02 10:42:58 +0800222 if (acpi_dev_resource_memory(res, r))
223 pnp_add_resource(dev, r);
Zhang Rui89935312014-03-11 22:40:27 +0800224 break;
225 case ACPI_RESOURCE_TYPE_IO:
226 case ACPI_RESOURCE_TYPE_FIXED_IO:
Jiang Liua49170b2015-02-02 10:42:58 +0800227 if (acpi_dev_resource_io(res, r))
228 pnp_add_resource(dev, r);
Zhang Rui89935312014-03-11 22:40:27 +0800229 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400230 case ACPI_RESOURCE_TYPE_DMA:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600231 dma = &res->data.dma;
Bjorn Helgaas5acf9142008-06-27 16:56:58 -0600232 if (dma->channel_count > 0 && dma->channels[0] != (u8) -1)
Bjorn Helgaas958a1fd2008-08-19 16:53:21 -0600233 flags = dma_flags(dev, dma->type, dma->bus_master,
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600234 dma->transfer);
Bjorn Helgaas5acf9142008-06-27 16:56:58 -0600235 else
236 flags = IORESOURCE_DISABLED;
237 pnp_add_dma_resource(dev, dma->channels[0], flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 break;
Len Brown0af58532006-01-20 01:11:37 -0500239
Len Brown0af58532006-01-20 01:11:37 -0500240 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
241 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
242 break;
243
Len Brown0af58532006-01-20 01:11:37 -0500244 case ACPI_RESOURCE_TYPE_VENDOR:
Bjorn Helgaas40ab4f42008-06-27 16:57:19 -0600245 vendor_typed = &res->data.vendor_typed;
246 pnpacpi_parse_allocated_vendor(dev, vendor_typed);
Len Brown0af58532006-01-20 01:11:37 -0500247 break;
248
249 case ACPI_RESOURCE_TYPE_END_TAG:
250 break;
251
Len Brown0af58532006-01-20 01:11:37 -0500252 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
253 break;
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 default:
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600256 dev_warn(&dev->dev, "unknown resource type %d in _CRS\n",
257 res->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return AE_ERROR;
259 }
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return AE_OK;
262}
263
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600264int pnpacpi_parse_allocated_resource(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Bjorn Helgaasc4da6942009-11-17 17:05:14 -0700266 struct acpi_device *acpi_dev = dev->data;
267 acpi_handle handle = acpi_dev->handle;
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600268 acpi_status status;
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600269
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600270 pnp_dbg(&dev->dev, "parse allocated resources\n");
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600271
Bjorn Helgaasf4490002008-04-28 16:34:09 -0600272 pnp_init_resources(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600274 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
275 pnpacpi_allocated_resource, dev);
276
277 if (ACPI_FAILURE(status)) {
278 if (status != AE_NOT_FOUND)
279 dev_err(&dev->dev, "can't evaluate _CRS: %d", status);
280 return -EPERM;
281 }
282 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600285static __init void pnpacpi_parse_dma_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600286 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800287 struct acpi_resource_dma *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 int i;
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200290 unsigned char map = 0, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700292 for (i = 0; i < p->channel_count; i++)
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600293 map |= 1 << p->channels[i];
Michael Karcher362ea082007-03-08 23:29:29 -0500294
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200295 flags = dma_flags(dev, p->type, p->bus_master, p->transfer);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600296 pnp_register_dma_resource(dev, option_flags, map, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600299static __init void pnpacpi_parse_irq_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600300 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800301 struct acpi_resource_irq *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 int i;
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600304 pnp_irq_mask_t map;
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200305 unsigned char flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600307 bitmap_zero(map.bits, PNP_IRQ_NR);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700308 for (i = 0; i < p->interrupt_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (p->interrupts[i])
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600310 __set_bit(p->interrupts[i], map.bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100312 flags = acpi_dev_irq_flags(p->triggering, p->polarity, p->sharable);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600313 pnp_register_irq_resource(dev, option_flags, &map, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600316static __init void pnpacpi_parse_ext_irq_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600317 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800318 struct acpi_resource_extended_irq *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 int i;
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600321 pnp_irq_mask_t map;
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200322 unsigned char flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600324 bitmap_zero(map.bits, PNP_IRQ_NR);
Bjorn Helgaasfe2cf592008-06-27 16:57:12 -0600325 for (i = 0; i < p->interrupt_count; i++) {
326 if (p->interrupts[i]) {
327 if (p->interrupts[i] < PNP_IRQ_NR)
328 __set_bit(p->interrupts[i], map.bits);
329 else
330 dev_err(&dev->dev, "ignoring IRQ %d option "
331 "(too large for %d entry bitmap)\n",
332 p->interrupts[i], PNP_IRQ_NR);
333 }
334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100336 flags = acpi_dev_irq_flags(p->triggering, p->polarity, p->sharable);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600337 pnp_register_irq_resource(dev, option_flags, &map, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600340static __init void pnpacpi_parse_port_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600341 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800342 struct acpi_resource_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600344 unsigned char flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600346 if (io->io_decode == ACPI_DECODE_16)
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200347 flags = IORESOURCE_IO_16BIT_ADDR;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600348 pnp_register_port_resource(dev, option_flags, io->minimum, io->maximum,
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600349 io->alignment, io->address_length, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600352static __init void pnpacpi_parse_fixed_port_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600353 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800354 struct acpi_resource_fixed_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600356 pnp_register_port_resource(dev, option_flags, io->address, io->address,
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200357 0, io->address_length, IORESOURCE_IO_FIXED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600360static __init void pnpacpi_parse_mem24_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600361 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800362 struct acpi_resource_memory24 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600364 unsigned char flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600366 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200367 flags = IORESOURCE_MEM_WRITEABLE;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600368 pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600369 p->alignment, p->address_length, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600372static __init void pnpacpi_parse_mem32_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600373 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800374 struct acpi_resource_memory32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600376 unsigned char flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600378 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200379 flags = IORESOURCE_MEM_WRITEABLE;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600380 pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600381 p->alignment, p->address_length, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600384static __init void pnpacpi_parse_fixed_mem32_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600385 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800386 struct acpi_resource_fixed_memory32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600388 unsigned char flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600390 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200391 flags = IORESOURCE_MEM_WRITEABLE;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600392 pnp_register_mem_resource(dev, option_flags, p->address, p->address,
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600393 0, p->address_length, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600396static __init void pnpacpi_parse_address_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600397 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800398 struct acpi_resource *r)
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400399{
400 struct acpi_resource_address64 addr, *p = &addr;
401 acpi_status status;
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600402 unsigned char flags = 0;
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400403
404 status = acpi_resource_to_address64(r, p);
Bjorn Helgaas958a1fd2008-08-19 16:53:21 -0600405 if (ACPI_FAILURE(status)) {
406 dev_warn(&dev->dev, "can't convert resource type %d\n",
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700407 r->type);
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400408 return;
409 }
410
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400411 if (p->resource_type == ACPI_MEMORY_RANGE) {
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600412 if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200413 flags = IORESOURCE_MEM_WRITEABLE;
Lv Zhenga45de932015-01-26 16:58:56 +0800414 pnp_register_mem_resource(dev, option_flags, p->address.minimum,
415 p->address.minimum, 0, p->address.address_length,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600416 flags);
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600417 } else if (p->resource_type == ACPI_IO_RANGE)
Lv Zhenga45de932015-01-26 16:58:56 +0800418 pnp_register_port_resource(dev, option_flags, p->address.minimum,
419 p->address.minimum, 0, p->address.address_length,
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200420 IORESOURCE_IO_FIXED);
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400421}
422
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600423static __init void pnpacpi_parse_ext_address_option(struct pnp_dev *dev,
424 unsigned int option_flags,
425 struct acpi_resource *r)
426{
427 struct acpi_resource_extended_address64 *p = &r->data.ext_address64;
428 unsigned char flags = 0;
429
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600430 if (p->resource_type == ACPI_MEMORY_RANGE) {
431 if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200432 flags = IORESOURCE_MEM_WRITEABLE;
Lv Zhenga45de932015-01-26 16:58:56 +0800433 pnp_register_mem_resource(dev, option_flags, p->address.minimum,
434 p->address.minimum, 0, p->address.address_length,
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600435 flags);
436 } else if (p->resource_type == ACPI_IO_RANGE)
Lv Zhenga45de932015-01-26 16:58:56 +0800437 pnp_register_port_resource(dev, option_flags, p->address.minimum,
438 p->address.minimum, 0, p->address.address_length,
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200439 IORESOURCE_IO_FIXED);
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600440}
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442struct acpipnp_parse_option_s {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 struct pnp_dev *dev;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600444 unsigned int option_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445};
446
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800447static __init acpi_status pnpacpi_option_resource(struct acpi_resource *res,
448 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Bjorn Helgaase2a1a6f2008-06-27 16:57:15 -0600450 int priority;
Bjorn Helgaas4721a4cc2007-08-15 10:32:10 -0600451 struct acpipnp_parse_option_s *parse_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 struct pnp_dev *dev = parse_data->dev;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600453 unsigned int option_flags = parse_data->option_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Len Browneca008c2005-09-22 00:25:18 -0400455 switch (res->type) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700456 case ACPI_RESOURCE_TYPE_IRQ:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600457 pnpacpi_parse_irq_option(dev, option_flags, &res->data.irq);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700458 break;
459
460 case ACPI_RESOURCE_TYPE_DMA:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600461 pnpacpi_parse_dma_option(dev, option_flags, &res->data.dma);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700462 break;
463
464 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
465 switch (res->data.start_dpf.compatibility_priority) {
466 case ACPI_GOOD_CONFIGURATION:
467 priority = PNP_RES_PRIORITY_PREFERRED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 break;
Len Brown0af58532006-01-20 01:11:37 -0500469
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700470 case ACPI_ACCEPTABLE_CONFIGURATION:
471 priority = PNP_RES_PRIORITY_ACCEPTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 break;
Len Brown0af58532006-01-20 01:11:37 -0500473
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700474 case ACPI_SUB_OPTIMAL_CONFIGURATION:
475 priority = PNP_RES_PRIORITY_FUNCTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 default:
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700478 priority = PNP_RES_PRIORITY_INVALID;
479 break;
480 }
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600481 parse_data->option_flags = pnp_new_dependent_set(dev, priority);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700482 break;
483
484 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600485 parse_data->option_flags = 0;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700486 break;
487
488 case ACPI_RESOURCE_TYPE_IO:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600489 pnpacpi_parse_port_option(dev, option_flags, &res->data.io);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700490 break;
491
492 case ACPI_RESOURCE_TYPE_FIXED_IO:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600493 pnpacpi_parse_fixed_port_option(dev, option_flags,
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600494 &res->data.fixed_io);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700495 break;
496
497 case ACPI_RESOURCE_TYPE_VENDOR:
498 case ACPI_RESOURCE_TYPE_END_TAG:
499 break;
500
501 case ACPI_RESOURCE_TYPE_MEMORY24:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600502 pnpacpi_parse_mem24_option(dev, option_flags,
503 &res->data.memory24);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700504 break;
505
506 case ACPI_RESOURCE_TYPE_MEMORY32:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600507 pnpacpi_parse_mem32_option(dev, option_flags,
508 &res->data.memory32);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700509 break;
510
511 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600512 pnpacpi_parse_fixed_mem32_option(dev, option_flags,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700513 &res->data.fixed_memory32);
514 break;
515
516 case ACPI_RESOURCE_TYPE_ADDRESS16:
517 case ACPI_RESOURCE_TYPE_ADDRESS32:
518 case ACPI_RESOURCE_TYPE_ADDRESS64:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600519 pnpacpi_parse_address_option(dev, option_flags, res);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700520 break;
521
522 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600523 pnpacpi_parse_ext_address_option(dev, option_flags, res);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700524 break;
525
526 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600527 pnpacpi_parse_ext_irq_option(dev, option_flags,
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600528 &res->data.extended_irq);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700529 break;
530
531 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
532 break;
533
534 default:
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600535 dev_warn(&dev->dev, "unknown resource type %d in _PRS\n",
536 res->type);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700537 return AE_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return AE_OK;
541}
542
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600543int __init pnpacpi_parse_resource_option_data(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Bjorn Helgaasc4da6942009-11-17 17:05:14 -0700545 struct acpi_device *acpi_dev = dev->data;
546 acpi_handle handle = acpi_dev->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 acpi_status status;
548 struct acpipnp_parse_option_s parse_data;
549
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600550 pnp_dbg(&dev->dev, "parse resource options\n");
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 parse_data.dev = dev;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600553 parse_data.option_flags = 0;
554
Bob Moore50eca3e2005-09-30 19:03:00 -0400555 status = acpi_walk_resources(handle, METHOD_NAME__PRS,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700556 pnpacpi_option_resource, &parse_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600558 if (ACPI_FAILURE(status)) {
559 if (status != AE_NOT_FOUND)
560 dev_err(&dev->dev, "can't evaluate _PRS: %d", status);
561 return -EPERM;
562 }
563 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Bjorn Helgaasb5f2490b2006-03-28 17:04:00 -0500566static int pnpacpi_supported_resource(struct acpi_resource *res)
567{
568 switch (res->type) {
569 case ACPI_RESOURCE_TYPE_IRQ:
570 case ACPI_RESOURCE_TYPE_DMA:
571 case ACPI_RESOURCE_TYPE_IO:
572 case ACPI_RESOURCE_TYPE_FIXED_IO:
573 case ACPI_RESOURCE_TYPE_MEMORY24:
574 case ACPI_RESOURCE_TYPE_MEMORY32:
575 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
576 case ACPI_RESOURCE_TYPE_ADDRESS16:
577 case ACPI_RESOURCE_TYPE_ADDRESS32:
578 case ACPI_RESOURCE_TYPE_ADDRESS64:
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600579 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
Bjorn Helgaasb5f2490b2006-03-28 17:04:00 -0500580 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
581 return 1;
582 }
583 return 0;
584}
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/*
587 * Set resource
588 */
589static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700590 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Bjorn Helgaas4721a4cc2007-08-15 10:32:10 -0600592 int *res_cnt = data;
Bjorn Helgaasb5f2490b2006-03-28 17:04:00 -0500593
594 if (pnpacpi_supported_resource(res))
595 (*res_cnt)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return AE_OK;
597}
598
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500599static acpi_status pnpacpi_type_resources(struct acpi_resource *res, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Bjorn Helgaas4721a4cc2007-08-15 10:32:10 -0600601 struct acpi_resource **resource = data;
Bjorn Helgaasb5f2490b2006-03-28 17:04:00 -0500602
603 if (pnpacpi_supported_resource(res)) {
Len Browneca008c2005-09-22 00:25:18 -0400604 (*resource)->type = res->type;
Bjorn Helgaasb5f2490b2006-03-28 17:04:00 -0500605 (*resource)->length = sizeof(struct acpi_resource);
Bjorn Helgaas36d872a2008-06-09 16:52:06 -0700606 if (res->type == ACPI_RESOURCE_TYPE_IRQ)
607 (*resource)->data.irq.descriptor_length =
608 res->data.irq.descriptor_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 (*resource)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
611
612 return AE_OK;
613}
614
Bjorn Helgaascdef6252008-04-28 16:34:03 -0600615int pnpacpi_build_resource_template(struct pnp_dev *dev,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700616 struct acpi_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Bjorn Helgaasc4da6942009-11-17 17:05:14 -0700618 struct acpi_device *acpi_dev = dev->data;
619 acpi_handle handle = acpi_dev->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 struct acpi_resource *resource;
621 int res_cnt = 0;
622 acpi_status status;
623
Bob Moore50eca3e2005-09-30 19:03:00 -0400624 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700625 pnpacpi_count_resources, &res_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (ACPI_FAILURE(status)) {
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600627 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return -EINVAL;
629 }
630 if (!res_cnt)
631 return -EINVAL;
632 buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
Robert P. J. Daycd861282006-12-13 00:34:52 -0800633 buffer->pointer = kzalloc(buffer->length - 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (!buffer->pointer)
635 return -ENOMEM;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 resource = (struct acpi_resource *)buffer->pointer;
Bob Moore50eca3e2005-09-30 19:03:00 -0400638 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700639 pnpacpi_type_resources, &resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (ACPI_FAILURE(status)) {
641 kfree(buffer->pointer);
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600642 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return -EINVAL;
644 }
645 /* resource will pointer the end resource now */
Bob Moore50eca3e2005-09-30 19:03:00 -0400646 resource->type = ACPI_RESOURCE_TYPE_END_TAG;
Yinghai Luf084dbb2013-03-23 19:16:37 +0000647 resource->length = sizeof(struct acpi_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 return 0;
650}
651
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600652static void pnpacpi_encode_irq(struct pnp_dev *dev,
653 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700654 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600656 struct acpi_resource_irq *irq = &resource->data.irq;
Fabian Frederickbbee06d2015-05-02 17:14:49 +0200657 u8 triggering, polarity, shareable;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500658
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600659 if (!pnp_resource_enabled(p)) {
660 irq->interrupt_count = 0;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600661 pnp_dbg(&dev->dev, " encode irq (%s)\n",
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600662 p ? "disabled" : "missing");
663 return;
664 }
665
Bjorn Helgaasa9932732008-06-09 16:52:05 -0700666 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600667 irq->triggering = triggering;
668 irq->polarity = polarity;
Bjorn Helgaasa9932732008-06-09 16:52:05 -0700669 irq->sharable = shareable;
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600670 irq->interrupt_count = 1;
671 irq->interrupts[0] = p->start;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600672
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600673 pnp_dbg(&dev->dev, " encode irq %d %s %s %s (%d-byte descriptor)\n",
Bjorn Helgaas36d872a2008-06-09 16:52:06 -0700674 (int) p->start,
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600675 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
676 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
Bjorn Helgaas36d872a2008-06-09 16:52:06 -0700677 irq->sharable == ACPI_SHARED ? "shared" : "exclusive",
678 irq->descriptor_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600681static void pnpacpi_encode_ext_irq(struct pnp_dev *dev,
682 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700683 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600685 struct acpi_resource_extended_irq *extended_irq = &resource->data.extended_irq;
Fabian Frederickbbee06d2015-05-02 17:14:49 +0200686 u8 triggering, polarity, shareable;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500687
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600688 if (!pnp_resource_enabled(p)) {
689 extended_irq->interrupt_count = 0;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600690 pnp_dbg(&dev->dev, " encode extended irq (%s)\n",
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600691 p ? "disabled" : "missing");
692 return;
693 }
694
Bjorn Helgaasa9932732008-06-09 16:52:05 -0700695 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600696 extended_irq->producer_consumer = ACPI_CONSUMER;
697 extended_irq->triggering = triggering;
698 extended_irq->polarity = polarity;
Bjorn Helgaasa9932732008-06-09 16:52:05 -0700699 extended_irq->sharable = shareable;
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600700 extended_irq->interrupt_count = 1;
701 extended_irq->interrupts[0] = p->start;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600702
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600703 pnp_dbg(&dev->dev, " encode irq %d %s %s %s\n", (int) p->start,
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600704 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
705 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
706 extended_irq->sharable == ACPI_SHARED ? "shared" : "exclusive");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600709static void pnpacpi_encode_dma(struct pnp_dev *dev,
710 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700711 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600713 struct acpi_resource_dma *dma = &resource->data.dma;
714
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600715 if (!pnp_resource_enabled(p)) {
716 dma->channel_count = 0;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600717 pnp_dbg(&dev->dev, " encode dma (%s)\n",
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600718 p ? "disabled" : "missing");
719 return;
720 }
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
Vojtech Pavlikccc4c7b2006-04-07 20:00:27 +0200723 switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700724 case IORESOURCE_DMA_TYPEA:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600725 dma->type = ACPI_TYPE_A;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700726 break;
727 case IORESOURCE_DMA_TYPEB:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600728 dma->type = ACPI_TYPE_B;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700729 break;
730 case IORESOURCE_DMA_TYPEF:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600731 dma->type = ACPI_TYPE_F;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700732 break;
733 default:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600734 dma->type = ACPI_COMPATIBILITY;
Vojtech Pavlikccc4c7b2006-04-07 20:00:27 +0200735 }
736
737 switch (p->flags & IORESOURCE_DMA_TYPE_MASK) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700738 case IORESOURCE_DMA_8BIT:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600739 dma->transfer = ACPI_TRANSFER_8;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700740 break;
741 case IORESOURCE_DMA_8AND16BIT:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600742 dma->transfer = ACPI_TRANSFER_8_16;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700743 break;
744 default:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600745 dma->transfer = ACPI_TRANSFER_16;
Vojtech Pavlikccc4c7b2006-04-07 20:00:27 +0200746 }
747
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600748 dma->bus_master = !!(p->flags & IORESOURCE_DMA_MASTER);
749 dma->channel_count = 1;
750 dma->channels[0] = p->start;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600751
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600752 pnp_dbg(&dev->dev, " encode dma %d "
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600753 "type %#x transfer %#x master %d\n",
754 (int) p->start, dma->type, dma->transfer, dma->bus_master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600757static void pnpacpi_encode_io(struct pnp_dev *dev,
758 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700759 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600761 struct acpi_resource_io *io = &resource->data.io;
762
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600763 if (pnp_resource_enabled(p)) {
764 /* Note: pnp_assign_port copies pnp_port->flags into p->flags */
Bjorn Helgaas08c9f262008-06-27 16:57:03 -0600765 io->io_decode = (p->flags & IORESOURCE_IO_16BIT_ADDR) ?
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600766 ACPI_DECODE_16 : ACPI_DECODE_10;
767 io->minimum = p->start;
768 io->maximum = p->end;
769 io->alignment = 0; /* Correct? */
Joe Perches28f65c112011-06-09 09:13:32 -0700770 io->address_length = resource_size(p);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600771 } else {
772 io->minimum = 0;
773 io->address_length = 0;
774 }
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600775
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600776 pnp_dbg(&dev->dev, " encode io %#x-%#x decode %#x\n", io->minimum,
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600777 io->minimum + io->address_length - 1, io->io_decode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600780static void pnpacpi_encode_fixed_io(struct pnp_dev *dev,
781 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700782 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600784 struct acpi_resource_fixed_io *fixed_io = &resource->data.fixed_io;
785
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600786 if (pnp_resource_enabled(p)) {
787 fixed_io->address = p->start;
Joe Perches28f65c112011-06-09 09:13:32 -0700788 fixed_io->address_length = resource_size(p);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600789 } else {
790 fixed_io->address = 0;
791 fixed_io->address_length = 0;
792 }
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600793
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600794 pnp_dbg(&dev->dev, " encode fixed_io %#x-%#x\n", fixed_io->address,
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600795 fixed_io->address + fixed_io->address_length - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600798static void pnpacpi_encode_mem24(struct pnp_dev *dev,
799 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700800 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600802 struct acpi_resource_memory24 *memory24 = &resource->data.memory24;
803
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600804 if (pnp_resource_enabled(p)) {
805 /* Note: pnp_assign_mem copies pnp_mem->flags into p->flags */
806 memory24->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
807 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
808 memory24->minimum = p->start;
809 memory24->maximum = p->end;
810 memory24->alignment = 0;
Joe Perches28f65c112011-06-09 09:13:32 -0700811 memory24->address_length = resource_size(p);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600812 } else {
813 memory24->minimum = 0;
814 memory24->address_length = 0;
815 }
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600816
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600817 pnp_dbg(&dev->dev, " encode mem24 %#x-%#x write_protect %#x\n",
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600818 memory24->minimum,
819 memory24->minimum + memory24->address_length - 1,
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600820 memory24->write_protect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600823static void pnpacpi_encode_mem32(struct pnp_dev *dev,
824 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700825 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600827 struct acpi_resource_memory32 *memory32 = &resource->data.memory32;
828
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600829 if (pnp_resource_enabled(p)) {
830 memory32->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
831 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
832 memory32->minimum = p->start;
833 memory32->maximum = p->end;
834 memory32->alignment = 0;
Joe Perches28f65c112011-06-09 09:13:32 -0700835 memory32->address_length = resource_size(p);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600836 } else {
837 memory32->minimum = 0;
838 memory32->alignment = 0;
839 }
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600840
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600841 pnp_dbg(&dev->dev, " encode mem32 %#x-%#x write_protect %#x\n",
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600842 memory32->minimum,
843 memory32->minimum + memory32->address_length - 1,
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600844 memory32->write_protect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600847static void pnpacpi_encode_fixed_mem32(struct pnp_dev *dev,
848 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700849 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600851 struct acpi_resource_fixed_memory32 *fixed_memory32 = &resource->data.fixed_memory32;
852
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600853 if (pnp_resource_enabled(p)) {
854 fixed_memory32->write_protect =
855 p->flags & IORESOURCE_MEM_WRITEABLE ?
856 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
857 fixed_memory32->address = p->start;
Joe Perches28f65c112011-06-09 09:13:32 -0700858 fixed_memory32->address_length = resource_size(p);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600859 } else {
860 fixed_memory32->address = 0;
861 fixed_memory32->address_length = 0;
862 }
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600863
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600864 pnp_dbg(&dev->dev, " encode fixed_mem32 %#x-%#x write_protect %#x\n",
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600865 fixed_memory32->address,
866 fixed_memory32->address + fixed_memory32->address_length - 1,
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600867 fixed_memory32->write_protect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600870int pnpacpi_encode_resources(struct pnp_dev *dev, struct acpi_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 int i = 0;
873 /* pnpacpi_build_resource_template allocates extra mem */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700874 int res_cnt = (buffer->length - 1) / sizeof(struct acpi_resource) - 1;
Bjorn Helgaas4721a4cc2007-08-15 10:32:10 -0600875 struct acpi_resource *resource = buffer->pointer;
Fabian Frederickfac69a22015-05-02 17:14:50 +0200876 unsigned int port = 0, irq = 0, dma = 0, mem = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600878 pnp_dbg(&dev->dev, "encode %d resources\n", res_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 while (i < res_cnt) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700880 switch (resource->type) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400881 case ACPI_RESOURCE_TYPE_IRQ:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600882 pnpacpi_encode_irq(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600883 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 irq++;
885 break;
886
Bob Moore50eca3e2005-09-30 19:03:00 -0400887 case ACPI_RESOURCE_TYPE_DMA:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600888 pnpacpi_encode_dma(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600889 pnp_get_resource(dev, IORESOURCE_DMA, dma));
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500890 dma++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400892 case ACPI_RESOURCE_TYPE_IO:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600893 pnpacpi_encode_io(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600894 pnp_get_resource(dev, IORESOURCE_IO, port));
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500895 port++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400897 case ACPI_RESOURCE_TYPE_FIXED_IO:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600898 pnpacpi_encode_fixed_io(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600899 pnp_get_resource(dev, IORESOURCE_IO, port));
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500900 port++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400902 case ACPI_RESOURCE_TYPE_MEMORY24:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600903 pnpacpi_encode_mem24(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600904 pnp_get_resource(dev, IORESOURCE_MEM, mem));
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500905 mem++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400907 case ACPI_RESOURCE_TYPE_MEMORY32:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600908 pnpacpi_encode_mem32(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600909 pnp_get_resource(dev, IORESOURCE_MEM, mem));
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500910 mem++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400912 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600913 pnpacpi_encode_fixed_mem32(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600914 pnp_get_resource(dev, IORESOURCE_MEM, mem));
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500915 mem++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 break;
Len Brown0af58532006-01-20 01:11:37 -0500917 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600918 pnpacpi_encode_ext_irq(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600919 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
Len Brown0af58532006-01-20 01:11:37 -0500920 irq++;
921 break;
922 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
923 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
924 case ACPI_RESOURCE_TYPE_VENDOR:
925 case ACPI_RESOURCE_TYPE_END_TAG:
926 case ACPI_RESOURCE_TYPE_ADDRESS16:
927 case ACPI_RESOURCE_TYPE_ADDRESS32:
928 case ACPI_RESOURCE_TYPE_ADDRESS64:
929 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
930 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700931 default: /* other type */
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600932 dev_warn(&dev->dev, "can't encode unknown resource "
933 "type %d\n", resource->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return -EINVAL;
935 }
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500936 resource++;
937 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939 return 0;
940}