blob: da78dc77aed32e4fbd589d138f13fa3cdcbba4d8 [file] [log] [blame]
Thomas Gleixner3e0a4e82019-05-23 11:14:55 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * pnpacpi -- PnP ACPI driver
4 *
5 * Copyright (c) 2004 Matthieu Castet <castet.matthieu@free.fr>
6 * Copyright (c) 2004 Li Shaohua <shaohua.li@intel.com>
Bjorn Helgaas40ab4f42008-06-27 16:57:19 -06007 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
8 * Bjorn Helgaas <bjorn.helgaas@hp.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10#include <linux/kernel.h>
11#include <linux/acpi.h>
12#include <linux/pci.h>
Bjorn Helgaas02d83b52008-04-28 16:34:28 -060013#include <linux/pnp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Bjorn Helgaas02d83b52008-04-28 16:34:28 -060015#include "../base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "pnpacpi.h"
17
Fabian Frederickbbee06d2015-05-02 17:14:49 +020018static void decode_irq_flags(struct pnp_dev *dev, int flags, u8 *triggering,
19 u8 *polarity, u8 *shareable)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020{
Bjorn Helgaase9fe9e12008-06-09 16:52:04 -070021 switch (flags & (IORESOURCE_IRQ_LOWLEVEL | IORESOURCE_IRQ_HIGHLEVEL |
22 IORESOURCE_IRQ_LOWEDGE | IORESOURCE_IRQ_HIGHEDGE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 case IORESOURCE_IRQ_LOWLEVEL:
Bob Moore50eca3e2005-09-30 19:03:00 -040024 *triggering = ACPI_LEVEL_SENSITIVE;
25 *polarity = ACPI_ACTIVE_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 break;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -050027 case IORESOURCE_IRQ_HIGHLEVEL:
Bob Moore50eca3e2005-09-30 19:03:00 -040028 *triggering = ACPI_LEVEL_SENSITIVE;
29 *polarity = ACPI_ACTIVE_HIGH;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 break;
31 case IORESOURCE_IRQ_LOWEDGE:
Bob Moore50eca3e2005-09-30 19:03:00 -040032 *triggering = ACPI_EDGE_SENSITIVE;
33 *polarity = ACPI_ACTIVE_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 break;
35 case IORESOURCE_IRQ_HIGHEDGE:
Bob Moore50eca3e2005-09-30 19:03:00 -040036 *triggering = ACPI_EDGE_SENSITIVE;
37 *polarity = ACPI_ACTIVE_HIGH;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 break;
Bjorn Helgaase9fe9e12008-06-09 16:52:04 -070039 default:
40 dev_err(&dev->dev, "can't encode invalid IRQ mode %#x\n",
41 flags);
42 *triggering = ACPI_EDGE_SENSITIVE;
43 *polarity = ACPI_ACTIVE_HIGH;
44 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 }
Bjorn Helgaasa9932732008-06-09 16:52:05 -070046
47 if (flags & IORESOURCE_IRQ_SHAREABLE)
48 *shareable = ACPI_SHARED;
49 else
50 *shareable = ACPI_EXCLUSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52
Bjorn Helgaas958a1fd2008-08-19 16:53:21 -060053static int dma_flags(struct pnp_dev *dev, int type, int bus_master,
54 int transfer)
Michael Karcher362ea082007-03-08 23:29:29 -050055{
56 int flags = 0;
57
58 if (bus_master)
59 flags |= IORESOURCE_DMA_MASTER;
60 switch (type) {
61 case ACPI_COMPATIBILITY:
62 flags |= IORESOURCE_DMA_COMPATIBLE;
63 break;
64 case ACPI_TYPE_A:
65 flags |= IORESOURCE_DMA_TYPEA;
66 break;
67 case ACPI_TYPE_B:
68 flags |= IORESOURCE_DMA_TYPEB;
69 break;
70 case ACPI_TYPE_F:
71 flags |= IORESOURCE_DMA_TYPEF;
72 break;
73 default:
74 /* Set a default value ? */
75 flags |= IORESOURCE_DMA_COMPATIBLE;
Bjorn Helgaas958a1fd2008-08-19 16:53:21 -060076 dev_err(&dev->dev, "invalid DMA type %d\n", type);
Michael Karcher362ea082007-03-08 23:29:29 -050077 }
78 switch (transfer) {
79 case ACPI_TRANSFER_8:
80 flags |= IORESOURCE_DMA_8BIT;
81 break;
82 case ACPI_TRANSFER_8_16:
83 flags |= IORESOURCE_DMA_8AND16BIT;
84 break;
85 case ACPI_TRANSFER_16:
86 flags |= IORESOURCE_DMA_16BIT;
87 break;
88 default:
89 /* Set a default value ? */
90 flags |= IORESOURCE_DMA_8AND16BIT;
Bjorn Helgaas958a1fd2008-08-19 16:53:21 -060091 dev_err(&dev->dev, "invalid DMA transfer type %d\n", transfer);
Michael Karcher362ea082007-03-08 23:29:29 -050092 }
93
94 return flags;
95}
96
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010097/*
98 * Allocated Resources
99 */
100
101static void pnpacpi_add_irqresource(struct pnp_dev *dev, struct resource *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100103 if (!(r->flags & IORESOURCE_DISABLED))
104 pcibios_penalize_isa_irq(r->start, 1);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700105
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100106 pnp_add_resource(dev, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Bjorn Helgaas40ab4f42008-06-27 16:57:19 -0600109/*
110 * Device CSRs that do not appear in PCI config space should be described
111 * via ACPI. This would normally be done with Address Space Descriptors
112 * marked as "consumer-only," but old versions of Windows and Linux ignore
113 * the producer/consumer flag, so HP invented a vendor-defined resource to
114 * describe the location and size of CSR space.
115 */
116static struct acpi_vendor_uuid hp_ccsr_uuid = {
117 .subtype = 2,
118 .data = { 0xf9, 0xad, 0xe9, 0x69, 0x4f, 0x92, 0x5f, 0xab, 0xf6, 0x4a,
119 0x24, 0xd2, 0x01, 0x37, 0x0e, 0xad },
120};
121
122static int vendor_resource_matches(struct pnp_dev *dev,
123 struct acpi_resource_vendor_typed *vendor,
124 struct acpi_vendor_uuid *match,
125 int expected_len)
126{
127 int uuid_len = sizeof(vendor->uuid);
128 u8 uuid_subtype = vendor->uuid_subtype;
129 u8 *uuid = vendor->uuid;
130 int actual_len;
131
132 /* byte_length includes uuid_subtype and uuid */
133 actual_len = vendor->byte_length - uuid_len - 1;
134
135 if (uuid_subtype == match->subtype &&
136 uuid_len == sizeof(match->data) &&
137 memcmp(uuid, match->data, uuid_len) == 0) {
138 if (expected_len && expected_len != actual_len) {
Andy Shevchenko1d7f2cd2017-05-23 20:03:26 +0300139 dev_err(&dev->dev,
140 "wrong vendor descriptor size; expected %d, found %d bytes\n",
Bjorn Helgaas40ab4f42008-06-27 16:57:19 -0600141 expected_len, actual_len);
142 return 0;
143 }
144
145 return 1;
146 }
147
148 return 0;
149}
150
151static void pnpacpi_parse_allocated_vendor(struct pnp_dev *dev,
152 struct acpi_resource_vendor_typed *vendor)
153{
154 if (vendor_resource_matches(dev, vendor, &hp_ccsr_uuid, 16)) {
155 u64 start, length;
156
157 memcpy(&start, vendor->byte_data, sizeof(start));
158 memcpy(&length, vendor->byte_data + 8, sizeof(length));
159
160 pnp_add_mem_resource(dev, start, start + length - 1, 0);
161 }
162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700165 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600167 struct pnp_dev *dev = data;
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600168 struct acpi_resource_dma *dma;
Bjorn Helgaas40ab4f42008-06-27 16:57:19 -0600169 struct acpi_resource_vendor_typed *vendor_typed;
Jagadish Krishnamoorthy2a56e912017-05-23 20:03:25 +0300170 struct acpi_resource_gpio *gpio;
Jiang Liua49170b2015-02-02 10:42:58 +0800171 struct resource_win win = {{0}, 0};
172 struct resource *r = &win.res;
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600173 int i, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Jiang Liua49170b2015-02-02 10:42:58 +0800175 if (acpi_dev_resource_address_space(res, &win)
176 || acpi_dev_resource_ext_address_space(res, &win)) {
177 pnp_add_resource(dev, &win.res);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100178 return AE_OK;
179 }
Bjorn Helgaas5acf9142008-06-27 16:56:58 -0600180
Jiang Liua49170b2015-02-02 10:42:58 +0800181 r->flags = 0;
182 if (acpi_dev_resource_interrupt(res, 0, r)) {
183 pnpacpi_add_irqresource(dev, r);
184 for (i = 1; acpi_dev_resource_interrupt(res, i, r); i++)
185 pnpacpi_add_irqresource(dev, r);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100186
187 if (i > 1) {
Bjorn Helgaas5acf9142008-06-27 16:56:58 -0600188 /*
189 * The IRQ encoder puts a single interrupt in each
190 * descriptor, so if a _CRS descriptor has more than
191 * one interrupt, we won't be able to re-encode it.
192 */
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100193 if (pnp_can_write(dev)) {
Andy Shevchenko1d7f2cd2017-05-23 20:03:26 +0300194 dev_warn(&dev->dev,
195 "multiple interrupts in _CRS descriptor; configuration can't be changed\n");
Bjorn Helgaas5acf9142008-06-27 16:56:58 -0600196 dev->capabilities &= ~PNP_WRITE;
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100199 return AE_OK;
Jagadish Krishnamoorthy2a56e912017-05-23 20:03:25 +0300200 } else if (acpi_gpio_get_irq_resource(res, &gpio)) {
201 /*
202 * If the resource is GpioInt() type then extract the IRQ
203 * from GPIO resource and fill it into IRQ resource type.
204 */
205 i = acpi_dev_gpio_irq_get(dev->data, 0);
206 if (i >= 0) {
207 flags = acpi_dev_irq_flags(gpio->triggering,
208 gpio->polarity,
Erik Schmaussc163f90c2019-02-15 13:36:19 -0800209 gpio->shareable);
Jagadish Krishnamoorthy2a56e912017-05-23 20:03:25 +0300210 } else {
211 flags = IORESOURCE_DISABLED;
212 }
213 pnp_add_irq_resource(dev, i, flags);
214 return AE_OK;
Jiang Liua49170b2015-02-02 10:42:58 +0800215 } else if (r->flags & IORESOURCE_DISABLED) {
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100216 pnp_add_irq_resource(dev, 0, IORESOURCE_DISABLED);
217 return AE_OK;
218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100220 switch (res->type) {
Zhang Rui89935312014-03-11 22:40:27 +0800221 case ACPI_RESOURCE_TYPE_MEMORY24:
222 case ACPI_RESOURCE_TYPE_MEMORY32:
223 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
Jiang Liua49170b2015-02-02 10:42:58 +0800224 if (acpi_dev_resource_memory(res, r))
225 pnp_add_resource(dev, r);
Zhang Rui89935312014-03-11 22:40:27 +0800226 break;
227 case ACPI_RESOURCE_TYPE_IO:
228 case ACPI_RESOURCE_TYPE_FIXED_IO:
Jiang Liua49170b2015-02-02 10:42:58 +0800229 if (acpi_dev_resource_io(res, r))
230 pnp_add_resource(dev, r);
Zhang Rui89935312014-03-11 22:40:27 +0800231 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400232 case ACPI_RESOURCE_TYPE_DMA:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600233 dma = &res->data.dma;
Bjorn Helgaas5acf9142008-06-27 16:56:58 -0600234 if (dma->channel_count > 0 && dma->channels[0] != (u8) -1)
Bjorn Helgaas958a1fd2008-08-19 16:53:21 -0600235 flags = dma_flags(dev, dma->type, dma->bus_master,
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600236 dma->transfer);
Bjorn Helgaas5acf9142008-06-27 16:56:58 -0600237 else
238 flags = IORESOURCE_DISABLED;
239 pnp_add_dma_resource(dev, dma->channels[0], flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 break;
Len Brown0af58532006-01-20 01:11:37 -0500241
Len Brown0af58532006-01-20 01:11:37 -0500242 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
243 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
244 break;
245
Len Brown0af58532006-01-20 01:11:37 -0500246 case ACPI_RESOURCE_TYPE_VENDOR:
Bjorn Helgaas40ab4f42008-06-27 16:57:19 -0600247 vendor_typed = &res->data.vendor_typed;
248 pnpacpi_parse_allocated_vendor(dev, vendor_typed);
Len Brown0af58532006-01-20 01:11:37 -0500249 break;
250
251 case ACPI_RESOURCE_TYPE_END_TAG:
252 break;
253
Len Brown0af58532006-01-20 01:11:37 -0500254 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
255 break;
256
Harb Abdulhamid86e75412016-03-01 13:31:45 -0600257 case ACPI_RESOURCE_TYPE_SERIAL_BUS:
258 /* serial bus connections (I2C/SPI/UART) are not pnp */
259 break;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 default:
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600262 dev_warn(&dev->dev, "unknown resource type %d in _CRS\n",
263 res->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return AE_ERROR;
265 }
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return AE_OK;
268}
269
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600270int pnpacpi_parse_allocated_resource(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Bjorn Helgaasc4da6942009-11-17 17:05:14 -0700272 struct acpi_device *acpi_dev = dev->data;
273 acpi_handle handle = acpi_dev->handle;
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600274 acpi_status status;
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600275
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600276 pnp_dbg(&dev->dev, "parse allocated resources\n");
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600277
Bjorn Helgaasf4490002008-04-28 16:34:09 -0600278 pnp_init_resources(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600280 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
281 pnpacpi_allocated_resource, dev);
282
283 if (ACPI_FAILURE(status)) {
284 if (status != AE_NOT_FOUND)
285 dev_err(&dev->dev, "can't evaluate _CRS: %d", status);
286 return -EPERM;
287 }
288 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600291static __init void pnpacpi_parse_dma_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600292 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800293 struct acpi_resource_dma *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 int i;
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200296 unsigned char map = 0, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700298 for (i = 0; i < p->channel_count; i++)
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600299 map |= 1 << p->channels[i];
Michael Karcher362ea082007-03-08 23:29:29 -0500300
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200301 flags = dma_flags(dev, p->type, p->bus_master, p->transfer);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600302 pnp_register_dma_resource(dev, option_flags, map, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600305static __init void pnpacpi_parse_irq_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600306 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800307 struct acpi_resource_irq *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 int i;
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600310 pnp_irq_mask_t map;
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200311 unsigned char flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600313 bitmap_zero(map.bits, PNP_IRQ_NR);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700314 for (i = 0; i < p->interrupt_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (p->interrupts[i])
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600316 __set_bit(p->interrupts[i], map.bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Erik Schmaussc163f90c2019-02-15 13:36:19 -0800318 flags = acpi_dev_irq_flags(p->triggering, p->polarity, p->shareable);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600319 pnp_register_irq_resource(dev, option_flags, &map, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600322static __init void pnpacpi_parse_ext_irq_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600323 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800324 struct acpi_resource_extended_irq *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 int i;
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600327 pnp_irq_mask_t map;
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200328 unsigned char flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600330 bitmap_zero(map.bits, PNP_IRQ_NR);
Bjorn Helgaasfe2cf592008-06-27 16:57:12 -0600331 for (i = 0; i < p->interrupt_count; i++) {
332 if (p->interrupts[i]) {
333 if (p->interrupts[i] < PNP_IRQ_NR)
334 __set_bit(p->interrupts[i], map.bits);
335 else
Andy Shevchenko1d7f2cd2017-05-23 20:03:26 +0300336 dev_err(&dev->dev,
337 "ignoring IRQ %d option (too large for %d entry bitmap)\n",
Bjorn Helgaasfe2cf592008-06-27 16:57:12 -0600338 p->interrupts[i], PNP_IRQ_NR);
339 }
340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Erik Schmaussc163f90c2019-02-15 13:36:19 -0800342 flags = acpi_dev_irq_flags(p->triggering, p->polarity, p->shareable);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600343 pnp_register_irq_resource(dev, option_flags, &map, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600346static __init void pnpacpi_parse_port_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600347 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800348 struct acpi_resource_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600350 unsigned char flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600352 if (io->io_decode == ACPI_DECODE_16)
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200353 flags = IORESOURCE_IO_16BIT_ADDR;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600354 pnp_register_port_resource(dev, option_flags, io->minimum, io->maximum,
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600355 io->alignment, io->address_length, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600358static __init void pnpacpi_parse_fixed_port_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600359 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800360 struct acpi_resource_fixed_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600362 pnp_register_port_resource(dev, option_flags, io->address, io->address,
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200363 0, io->address_length, IORESOURCE_IO_FIXED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600366static __init void pnpacpi_parse_mem24_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600367 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800368 struct acpi_resource_memory24 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600370 unsigned char flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600372 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200373 flags = IORESOURCE_MEM_WRITEABLE;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600374 pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600375 p->alignment, p->address_length, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600378static __init void pnpacpi_parse_mem32_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600379 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800380 struct acpi_resource_memory32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600382 unsigned char flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600384 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200385 flags = IORESOURCE_MEM_WRITEABLE;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600386 pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600387 p->alignment, p->address_length, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600390static __init void pnpacpi_parse_fixed_mem32_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600391 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800392 struct acpi_resource_fixed_memory32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600394 unsigned char flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600396 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200397 flags = IORESOURCE_MEM_WRITEABLE;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600398 pnp_register_mem_resource(dev, option_flags, p->address, p->address,
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600399 0, p->address_length, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600402static __init void pnpacpi_parse_address_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600403 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800404 struct acpi_resource *r)
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400405{
406 struct acpi_resource_address64 addr, *p = &addr;
407 acpi_status status;
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600408 unsigned char flags = 0;
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400409
410 status = acpi_resource_to_address64(r, p);
Bjorn Helgaas958a1fd2008-08-19 16:53:21 -0600411 if (ACPI_FAILURE(status)) {
412 dev_warn(&dev->dev, "can't convert resource type %d\n",
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700413 r->type);
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400414 return;
415 }
416
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400417 if (p->resource_type == ACPI_MEMORY_RANGE) {
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600418 if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200419 flags = IORESOURCE_MEM_WRITEABLE;
Lv Zhenga45de932015-01-26 16:58:56 +0800420 pnp_register_mem_resource(dev, option_flags, p->address.minimum,
421 p->address.minimum, 0, p->address.address_length,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600422 flags);
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600423 } else if (p->resource_type == ACPI_IO_RANGE)
Lv Zhenga45de932015-01-26 16:58:56 +0800424 pnp_register_port_resource(dev, option_flags, p->address.minimum,
425 p->address.minimum, 0, p->address.address_length,
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200426 IORESOURCE_IO_FIXED);
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400427}
428
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600429static __init void pnpacpi_parse_ext_address_option(struct pnp_dev *dev,
430 unsigned int option_flags,
431 struct acpi_resource *r)
432{
433 struct acpi_resource_extended_address64 *p = &r->data.ext_address64;
434 unsigned char flags = 0;
435
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600436 if (p->resource_type == ACPI_MEMORY_RANGE) {
437 if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200438 flags = IORESOURCE_MEM_WRITEABLE;
Lv Zhenga45de932015-01-26 16:58:56 +0800439 pnp_register_mem_resource(dev, option_flags, p->address.minimum,
440 p->address.minimum, 0, p->address.address_length,
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600441 flags);
442 } else if (p->resource_type == ACPI_IO_RANGE)
Lv Zhenga45de932015-01-26 16:58:56 +0800443 pnp_register_port_resource(dev, option_flags, p->address.minimum,
444 p->address.minimum, 0, p->address.address_length,
Witold Szczeponik18fd4702011-10-27 20:56:47 +0200445 IORESOURCE_IO_FIXED);
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600446}
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448struct acpipnp_parse_option_s {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 struct pnp_dev *dev;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600450 unsigned int option_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451};
452
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800453static __init acpi_status pnpacpi_option_resource(struct acpi_resource *res,
454 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Bjorn Helgaase2a1a6f2008-06-27 16:57:15 -0600456 int priority;
Bjorn Helgaas4721a4cc2007-08-15 10:32:10 -0600457 struct acpipnp_parse_option_s *parse_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 struct pnp_dev *dev = parse_data->dev;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600459 unsigned int option_flags = parse_data->option_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Len Browneca008c2005-09-22 00:25:18 -0400461 switch (res->type) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700462 case ACPI_RESOURCE_TYPE_IRQ:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600463 pnpacpi_parse_irq_option(dev, option_flags, &res->data.irq);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700464 break;
465
466 case ACPI_RESOURCE_TYPE_DMA:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600467 pnpacpi_parse_dma_option(dev, option_flags, &res->data.dma);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700468 break;
469
470 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
471 switch (res->data.start_dpf.compatibility_priority) {
472 case ACPI_GOOD_CONFIGURATION:
473 priority = PNP_RES_PRIORITY_PREFERRED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 break;
Len Brown0af58532006-01-20 01:11:37 -0500475
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700476 case ACPI_ACCEPTABLE_CONFIGURATION:
477 priority = PNP_RES_PRIORITY_ACCEPTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 break;
Len Brown0af58532006-01-20 01:11:37 -0500479
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700480 case ACPI_SUB_OPTIMAL_CONFIGURATION:
481 priority = PNP_RES_PRIORITY_FUNCTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 default:
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700484 priority = PNP_RES_PRIORITY_INVALID;
485 break;
486 }
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600487 parse_data->option_flags = pnp_new_dependent_set(dev, priority);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700488 break;
489
490 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600491 parse_data->option_flags = 0;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700492 break;
493
494 case ACPI_RESOURCE_TYPE_IO:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600495 pnpacpi_parse_port_option(dev, option_flags, &res->data.io);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700496 break;
497
498 case ACPI_RESOURCE_TYPE_FIXED_IO:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600499 pnpacpi_parse_fixed_port_option(dev, option_flags,
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600500 &res->data.fixed_io);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700501 break;
502
503 case ACPI_RESOURCE_TYPE_VENDOR:
504 case ACPI_RESOURCE_TYPE_END_TAG:
505 break;
506
507 case ACPI_RESOURCE_TYPE_MEMORY24:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600508 pnpacpi_parse_mem24_option(dev, option_flags,
509 &res->data.memory24);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700510 break;
511
512 case ACPI_RESOURCE_TYPE_MEMORY32:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600513 pnpacpi_parse_mem32_option(dev, option_flags,
514 &res->data.memory32);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700515 break;
516
517 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600518 pnpacpi_parse_fixed_mem32_option(dev, option_flags,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700519 &res->data.fixed_memory32);
520 break;
521
522 case ACPI_RESOURCE_TYPE_ADDRESS16:
523 case ACPI_RESOURCE_TYPE_ADDRESS32:
524 case ACPI_RESOURCE_TYPE_ADDRESS64:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600525 pnpacpi_parse_address_option(dev, option_flags, res);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700526 break;
527
528 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600529 pnpacpi_parse_ext_address_option(dev, option_flags, res);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700530 break;
531
532 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600533 pnpacpi_parse_ext_irq_option(dev, option_flags,
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600534 &res->data.extended_irq);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700535 break;
536
537 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
538 break;
539
540 default:
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600541 dev_warn(&dev->dev, "unknown resource type %d in _PRS\n",
542 res->type);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700543 return AE_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return AE_OK;
547}
548
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600549int __init pnpacpi_parse_resource_option_data(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Bjorn Helgaasc4da6942009-11-17 17:05:14 -0700551 struct acpi_device *acpi_dev = dev->data;
552 acpi_handle handle = acpi_dev->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 acpi_status status;
554 struct acpipnp_parse_option_s parse_data;
555
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600556 pnp_dbg(&dev->dev, "parse resource options\n");
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 parse_data.dev = dev;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600559 parse_data.option_flags = 0;
560
Bob Moore50eca3e2005-09-30 19:03:00 -0400561 status = acpi_walk_resources(handle, METHOD_NAME__PRS,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700562 pnpacpi_option_resource, &parse_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600564 if (ACPI_FAILURE(status)) {
565 if (status != AE_NOT_FOUND)
566 dev_err(&dev->dev, "can't evaluate _PRS: %d", status);
567 return -EPERM;
568 }
569 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
Bjorn Helgaasb5f2490b2006-03-28 17:04:00 -0500572static int pnpacpi_supported_resource(struct acpi_resource *res)
573{
574 switch (res->type) {
575 case ACPI_RESOURCE_TYPE_IRQ:
576 case ACPI_RESOURCE_TYPE_DMA:
577 case ACPI_RESOURCE_TYPE_IO:
578 case ACPI_RESOURCE_TYPE_FIXED_IO:
579 case ACPI_RESOURCE_TYPE_MEMORY24:
580 case ACPI_RESOURCE_TYPE_MEMORY32:
581 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
582 case ACPI_RESOURCE_TYPE_ADDRESS16:
583 case ACPI_RESOURCE_TYPE_ADDRESS32:
584 case ACPI_RESOURCE_TYPE_ADDRESS64:
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600585 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
Bjorn Helgaasb5f2490b2006-03-28 17:04:00 -0500586 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
587 return 1;
588 }
589 return 0;
590}
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592/*
593 * Set resource
594 */
595static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700596 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Bjorn Helgaas4721a4cc2007-08-15 10:32:10 -0600598 int *res_cnt = data;
Bjorn Helgaasb5f2490b2006-03-28 17:04:00 -0500599
600 if (pnpacpi_supported_resource(res))
601 (*res_cnt)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return AE_OK;
603}
604
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500605static acpi_status pnpacpi_type_resources(struct acpi_resource *res, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Bjorn Helgaas4721a4cc2007-08-15 10:32:10 -0600607 struct acpi_resource **resource = data;
Bjorn Helgaasb5f2490b2006-03-28 17:04:00 -0500608
609 if (pnpacpi_supported_resource(res)) {
Len Browneca008c2005-09-22 00:25:18 -0400610 (*resource)->type = res->type;
Bjorn Helgaasb5f2490b2006-03-28 17:04:00 -0500611 (*resource)->length = sizeof(struct acpi_resource);
Bjorn Helgaas36d872a2008-06-09 16:52:06 -0700612 if (res->type == ACPI_RESOURCE_TYPE_IRQ)
613 (*resource)->data.irq.descriptor_length =
614 res->data.irq.descriptor_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 (*resource)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617
618 return AE_OK;
619}
620
Bjorn Helgaascdef6252008-04-28 16:34:03 -0600621int pnpacpi_build_resource_template(struct pnp_dev *dev,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700622 struct acpi_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Bjorn Helgaasc4da6942009-11-17 17:05:14 -0700624 struct acpi_device *acpi_dev = dev->data;
625 acpi_handle handle = acpi_dev->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 struct acpi_resource *resource;
627 int res_cnt = 0;
628 acpi_status status;
629
Bob Moore50eca3e2005-09-30 19:03:00 -0400630 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700631 pnpacpi_count_resources, &res_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (ACPI_FAILURE(status)) {
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600633 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return -EINVAL;
635 }
636 if (!res_cnt)
637 return -EINVAL;
638 buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
Robert P. J. Daycd861282006-12-13 00:34:52 -0800639 buffer->pointer = kzalloc(buffer->length - 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (!buffer->pointer)
641 return -ENOMEM;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 resource = (struct acpi_resource *)buffer->pointer;
Bob Moore50eca3e2005-09-30 19:03:00 -0400644 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700645 pnpacpi_type_resources, &resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (ACPI_FAILURE(status)) {
647 kfree(buffer->pointer);
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600648 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return -EINVAL;
650 }
651 /* resource will pointer the end resource now */
Bob Moore50eca3e2005-09-30 19:03:00 -0400652 resource->type = ACPI_RESOURCE_TYPE_END_TAG;
Yinghai Luf084dbb2013-03-23 19:16:37 +0000653 resource->length = sizeof(struct acpi_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 return 0;
656}
657
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600658static void pnpacpi_encode_irq(struct pnp_dev *dev,
659 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700660 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600662 struct acpi_resource_irq *irq = &resource->data.irq;
Fabian Frederickbbee06d2015-05-02 17:14:49 +0200663 u8 triggering, polarity, shareable;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500664
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600665 if (!pnp_resource_enabled(p)) {
666 irq->interrupt_count = 0;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600667 pnp_dbg(&dev->dev, " encode irq (%s)\n",
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600668 p ? "disabled" : "missing");
669 return;
670 }
671
Bjorn Helgaasa9932732008-06-09 16:52:05 -0700672 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600673 irq->triggering = triggering;
674 irq->polarity = polarity;
Erik Schmaussc163f90c2019-02-15 13:36:19 -0800675 irq->shareable = shareable;
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600676 irq->interrupt_count = 1;
677 irq->interrupts[0] = p->start;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600678
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600679 pnp_dbg(&dev->dev, " encode irq %d %s %s %s (%d-byte descriptor)\n",
Bjorn Helgaas36d872a2008-06-09 16:52:06 -0700680 (int) p->start,
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600681 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
682 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
Erik Schmaussc163f90c2019-02-15 13:36:19 -0800683 irq->shareable == ACPI_SHARED ? "shared" : "exclusive",
Bjorn Helgaas36d872a2008-06-09 16:52:06 -0700684 irq->descriptor_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600687static void pnpacpi_encode_ext_irq(struct pnp_dev *dev,
688 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700689 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600691 struct acpi_resource_extended_irq *extended_irq = &resource->data.extended_irq;
Fabian Frederickbbee06d2015-05-02 17:14:49 +0200692 u8 triggering, polarity, shareable;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500693
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600694 if (!pnp_resource_enabled(p)) {
695 extended_irq->interrupt_count = 0;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600696 pnp_dbg(&dev->dev, " encode extended irq (%s)\n",
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600697 p ? "disabled" : "missing");
698 return;
699 }
700
Bjorn Helgaasa9932732008-06-09 16:52:05 -0700701 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600702 extended_irq->producer_consumer = ACPI_CONSUMER;
703 extended_irq->triggering = triggering;
704 extended_irq->polarity = polarity;
Erik Schmaussc163f90c2019-02-15 13:36:19 -0800705 extended_irq->shareable = shareable;
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600706 extended_irq->interrupt_count = 1;
707 extended_irq->interrupts[0] = p->start;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600708
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600709 pnp_dbg(&dev->dev, " encode irq %d %s %s %s\n", (int) p->start,
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600710 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
711 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
Erik Schmaussc163f90c2019-02-15 13:36:19 -0800712 extended_irq->shareable == ACPI_SHARED ? "shared" : "exclusive");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600715static void pnpacpi_encode_dma(struct pnp_dev *dev,
716 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700717 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600719 struct acpi_resource_dma *dma = &resource->data.dma;
720
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600721 if (!pnp_resource_enabled(p)) {
722 dma->channel_count = 0;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600723 pnp_dbg(&dev->dev, " encode dma (%s)\n",
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600724 p ? "disabled" : "missing");
725 return;
726 }
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
Vojtech Pavlikccc4c7b2006-04-07 20:00:27 +0200729 switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700730 case IORESOURCE_DMA_TYPEA:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600731 dma->type = ACPI_TYPE_A;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700732 break;
733 case IORESOURCE_DMA_TYPEB:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600734 dma->type = ACPI_TYPE_B;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700735 break;
736 case IORESOURCE_DMA_TYPEF:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600737 dma->type = ACPI_TYPE_F;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700738 break;
739 default:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600740 dma->type = ACPI_COMPATIBILITY;
Vojtech Pavlikccc4c7b2006-04-07 20:00:27 +0200741 }
742
743 switch (p->flags & IORESOURCE_DMA_TYPE_MASK) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700744 case IORESOURCE_DMA_8BIT:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600745 dma->transfer = ACPI_TRANSFER_8;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700746 break;
747 case IORESOURCE_DMA_8AND16BIT:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600748 dma->transfer = ACPI_TRANSFER_8_16;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700749 break;
750 default:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600751 dma->transfer = ACPI_TRANSFER_16;
Vojtech Pavlikccc4c7b2006-04-07 20:00:27 +0200752 }
753
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600754 dma->bus_master = !!(p->flags & IORESOURCE_DMA_MASTER);
755 dma->channel_count = 1;
756 dma->channels[0] = p->start;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600757
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600758 pnp_dbg(&dev->dev, " encode dma %d "
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600759 "type %#x transfer %#x master %d\n",
760 (int) p->start, dma->type, dma->transfer, dma->bus_master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600763static void pnpacpi_encode_io(struct pnp_dev *dev,
764 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700765 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600767 struct acpi_resource_io *io = &resource->data.io;
768
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600769 if (pnp_resource_enabled(p)) {
770 /* Note: pnp_assign_port copies pnp_port->flags into p->flags */
Bjorn Helgaas08c9f262008-06-27 16:57:03 -0600771 io->io_decode = (p->flags & IORESOURCE_IO_16BIT_ADDR) ?
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600772 ACPI_DECODE_16 : ACPI_DECODE_10;
773 io->minimum = p->start;
774 io->maximum = p->end;
775 io->alignment = 0; /* Correct? */
Joe Perches28f65c112011-06-09 09:13:32 -0700776 io->address_length = resource_size(p);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600777 } else {
778 io->minimum = 0;
779 io->address_length = 0;
780 }
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600781
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600782 pnp_dbg(&dev->dev, " encode io %#x-%#x decode %#x\n", io->minimum,
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600783 io->minimum + io->address_length - 1, io->io_decode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600786static void pnpacpi_encode_fixed_io(struct pnp_dev *dev,
787 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700788 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600790 struct acpi_resource_fixed_io *fixed_io = &resource->data.fixed_io;
791
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600792 if (pnp_resource_enabled(p)) {
793 fixed_io->address = p->start;
Joe Perches28f65c112011-06-09 09:13:32 -0700794 fixed_io->address_length = resource_size(p);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600795 } else {
796 fixed_io->address = 0;
797 fixed_io->address_length = 0;
798 }
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600799
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600800 pnp_dbg(&dev->dev, " encode fixed_io %#x-%#x\n", fixed_io->address,
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600801 fixed_io->address + fixed_io->address_length - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600804static void pnpacpi_encode_mem24(struct pnp_dev *dev,
805 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700806 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600808 struct acpi_resource_memory24 *memory24 = &resource->data.memory24;
809
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600810 if (pnp_resource_enabled(p)) {
811 /* Note: pnp_assign_mem copies pnp_mem->flags into p->flags */
812 memory24->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
813 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
814 memory24->minimum = p->start;
815 memory24->maximum = p->end;
816 memory24->alignment = 0;
Joe Perches28f65c112011-06-09 09:13:32 -0700817 memory24->address_length = resource_size(p);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600818 } else {
819 memory24->minimum = 0;
820 memory24->address_length = 0;
821 }
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600822
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600823 pnp_dbg(&dev->dev, " encode mem24 %#x-%#x write_protect %#x\n",
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600824 memory24->minimum,
825 memory24->minimum + memory24->address_length - 1,
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600826 memory24->write_protect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600829static void pnpacpi_encode_mem32(struct pnp_dev *dev,
830 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700831 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600833 struct acpi_resource_memory32 *memory32 = &resource->data.memory32;
834
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600835 if (pnp_resource_enabled(p)) {
836 memory32->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
837 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
838 memory32->minimum = p->start;
839 memory32->maximum = p->end;
840 memory32->alignment = 0;
Joe Perches28f65c112011-06-09 09:13:32 -0700841 memory32->address_length = resource_size(p);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600842 } else {
843 memory32->minimum = 0;
844 memory32->alignment = 0;
845 }
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600846
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600847 pnp_dbg(&dev->dev, " encode mem32 %#x-%#x write_protect %#x\n",
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600848 memory32->minimum,
849 memory32->minimum + memory32->address_length - 1,
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600850 memory32->write_protect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600853static void pnpacpi_encode_fixed_mem32(struct pnp_dev *dev,
854 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700855 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600857 struct acpi_resource_fixed_memory32 *fixed_memory32 = &resource->data.fixed_memory32;
858
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600859 if (pnp_resource_enabled(p)) {
860 fixed_memory32->write_protect =
861 p->flags & IORESOURCE_MEM_WRITEABLE ?
862 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
863 fixed_memory32->address = p->start;
Joe Perches28f65c112011-06-09 09:13:32 -0700864 fixed_memory32->address_length = resource_size(p);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600865 } else {
866 fixed_memory32->address = 0;
867 fixed_memory32->address_length = 0;
868 }
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600869
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600870 pnp_dbg(&dev->dev, " encode fixed_mem32 %#x-%#x write_protect %#x\n",
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600871 fixed_memory32->address,
872 fixed_memory32->address + fixed_memory32->address_length - 1,
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600873 fixed_memory32->write_protect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600876int pnpacpi_encode_resources(struct pnp_dev *dev, struct acpi_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 int i = 0;
879 /* pnpacpi_build_resource_template allocates extra mem */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700880 int res_cnt = (buffer->length - 1) / sizeof(struct acpi_resource) - 1;
Bjorn Helgaas4721a4cc2007-08-15 10:32:10 -0600881 struct acpi_resource *resource = buffer->pointer;
Fabian Frederickfac69a22015-05-02 17:14:50 +0200882 unsigned int port = 0, irq = 0, dma = 0, mem = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600884 pnp_dbg(&dev->dev, "encode %d resources\n", res_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 while (i < res_cnt) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700886 switch (resource->type) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400887 case ACPI_RESOURCE_TYPE_IRQ:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600888 pnpacpi_encode_irq(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600889 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 irq++;
891 break;
892
Bob Moore50eca3e2005-09-30 19:03:00 -0400893 case ACPI_RESOURCE_TYPE_DMA:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600894 pnpacpi_encode_dma(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600895 pnp_get_resource(dev, IORESOURCE_DMA, dma));
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500896 dma++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400898 case ACPI_RESOURCE_TYPE_IO:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600899 pnpacpi_encode_io(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600900 pnp_get_resource(dev, IORESOURCE_IO, port));
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500901 port++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400903 case ACPI_RESOURCE_TYPE_FIXED_IO:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600904 pnpacpi_encode_fixed_io(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600905 pnp_get_resource(dev, IORESOURCE_IO, port));
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500906 port++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400908 case ACPI_RESOURCE_TYPE_MEMORY24:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600909 pnpacpi_encode_mem24(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600910 pnp_get_resource(dev, IORESOURCE_MEM, mem));
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500911 mem++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400913 case ACPI_RESOURCE_TYPE_MEMORY32:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600914 pnpacpi_encode_mem32(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600915 pnp_get_resource(dev, IORESOURCE_MEM, mem));
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500916 mem++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400918 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600919 pnpacpi_encode_fixed_mem32(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600920 pnp_get_resource(dev, IORESOURCE_MEM, mem));
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500921 mem++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 break;
Len Brown0af58532006-01-20 01:11:37 -0500923 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600924 pnpacpi_encode_ext_irq(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600925 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
Len Brown0af58532006-01-20 01:11:37 -0500926 irq++;
927 break;
928 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
929 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
930 case ACPI_RESOURCE_TYPE_VENDOR:
931 case ACPI_RESOURCE_TYPE_END_TAG:
932 case ACPI_RESOURCE_TYPE_ADDRESS16:
933 case ACPI_RESOURCE_TYPE_ADDRESS32:
934 case ACPI_RESOURCE_TYPE_ADDRESS64:
935 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
936 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700937 default: /* other type */
Andy Shevchenko1d7f2cd2017-05-23 20:03:26 +0300938 dev_warn(&dev->dev,
939 "can't encode unknown resource type %d\n",
940 resource->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return -EINVAL;
942 }
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500943 resource++;
944 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946 return 0;
947}