blob: 70d4ba95735a6a85c6fe81c0cfd1bac4467359ed [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * resource.c - Contains functions for registering and analyzing resource information
4 *
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02005 * based on isapnp.c resource management (c) Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -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
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/errno.h>
14#include <linux/interrupt.h>
15#include <linux/kernel.h>
16#include <asm/io.h>
17#include <asm/dma.h>
18#include <asm/irq.h>
19#include <linux/pci.h>
20#include <linux/ioport.h>
21#include <linux/init.h>
22
23#include <linux/pnp.h>
24#include "base.h"
25
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070026static int pnp_reserve_irq[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some IRQ */
27static int pnp_reserve_dma[8] = {[0 ... 7] = -1 }; /* reserve (don't use) some DMA */
28static int pnp_reserve_io[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some I/O region */
29static int pnp_reserve_mem[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some memory region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/*
32 * option registration
33 */
34
Rashika Kheria62c6dae2013-12-14 18:50:56 +053035static struct pnp_option *pnp_build_option(struct pnp_dev *dev, unsigned long type,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060036 unsigned int option_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060038 struct pnp_option *option;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060040 option = kzalloc(sizeof(struct pnp_option), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 if (!option)
42 return NULL;
43
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060044 option->flags = option_flags;
45 option->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060047 list_add_tail(&option->list, &dev->options);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 return option;
49}
50
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060051int pnp_register_irq_resource(struct pnp_dev *dev, unsigned int option_flags,
Bjorn Helgaasc2275362008-06-27 16:57:11 -060052 pnp_irq_mask_t *map, unsigned char flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060054 struct pnp_option *option;
55 struct pnp_irq *irq;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070056
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060057 option = pnp_build_option(dev, IORESOURCE_IRQ, option_flags);
58 if (!option)
Bjorn Helgaasc2275362008-06-27 16:57:11 -060059 return -ENOMEM;
60
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060061 irq = &option->u.irq;
Bjorn Helgaas2d29a7a2008-06-27 16:57:13 -060062 irq->map = *map;
63 irq->flags = flags;
Bjorn Helgaasc2275362008-06-27 16:57:11 -060064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#ifdef CONFIG_PCI
66 {
67 int i;
68
69 for (i = 0; i < 16; i++)
Bjorn Helgaas2d29a7a2008-06-27 16:57:13 -060070 if (test_bit(i, irq->map.bits))
David Shaohua Lic9c3e452005-04-01 00:07:31 -050071 pcibios_penalize_isa_irq(i, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 }
73#endif
Bjorn Helgaasc1caf062008-04-28 16:34:04 -060074
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060075 dbg_pnp_show_option(dev, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return 0;
77}
78
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060079int pnp_register_dma_resource(struct pnp_dev *dev, unsigned int option_flags,
Bjorn Helgaasc2275362008-06-27 16:57:11 -060080 unsigned char map, unsigned char flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060082 struct pnp_option *option;
83 struct pnp_dma *dma;
Bjorn Helgaasc2275362008-06-27 16:57:11 -060084
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060085 option = pnp_build_option(dev, IORESOURCE_DMA, option_flags);
86 if (!option)
Bjorn Helgaasc2275362008-06-27 16:57:11 -060087 return -ENOMEM;
88
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060089 dma = &option->u.dma;
Bjorn Helgaas2d29a7a2008-06-27 16:57:13 -060090 dma->map = map;
91 dma->flags = flags;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070092
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060093 dbg_pnp_show_option(dev, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return 0;
95}
96
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060097int pnp_register_port_resource(struct pnp_dev *dev, unsigned int option_flags,
Bjorn Helgaasc2275362008-06-27 16:57:11 -060098 resource_size_t min, resource_size_t max,
99 resource_size_t align, resource_size_t size,
100 unsigned char flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600102 struct pnp_option *option;
103 struct pnp_port *port;
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600104
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600105 option = pnp_build_option(dev, IORESOURCE_IO, option_flags);
106 if (!option)
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600107 return -ENOMEM;
108
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600109 port = &option->u.port;
Bjorn Helgaas2d29a7a2008-06-27 16:57:13 -0600110 port->min = min;
111 port->max = max;
112 port->align = align;
113 port->size = size;
114 port->flags = flags;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700115
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600116 dbg_pnp_show_option(dev, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return 0;
118}
119
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600120int pnp_register_mem_resource(struct pnp_dev *dev, unsigned int option_flags,
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600121 resource_size_t min, resource_size_t max,
122 resource_size_t align, resource_size_t size,
123 unsigned char flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600125 struct pnp_option *option;
126 struct pnp_mem *mem;
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600127
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600128 option = pnp_build_option(dev, IORESOURCE_MEM, option_flags);
129 if (!option)
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600130 return -ENOMEM;
131
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600132 mem = &option->u.mem;
Bjorn Helgaas2d29a7a2008-06-27 16:57:13 -0600133 mem->min = min;
134 mem->max = max;
135 mem->align = align;
136 mem->size = size;
137 mem->flags = flags;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700138
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600139 dbg_pnp_show_option(dev, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return 0;
141}
142
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600143void pnp_free_options(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600145 struct pnp_option *option, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600147 list_for_each_entry_safe(option, tmp, &dev->options, list) {
148 list_del(&option->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 kfree(option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/*
154 * resource validity checking
155 */
156
157#define length(start, end) (*(end) - *(start) + 1)
158
159/* Two ranges conflict if one doesn't end before the other starts */
160#define ranged_conflict(starta, enda, startb, endb) \
161 !((*(enda) < *(startb)) || (*(endb) < *(starta)))
162
163#define cannot_compare(flags) \
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600164((flags) & IORESOURCE_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600166int pnp_check_port(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600168 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600170 struct resource *tres;
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700171 resource_size_t *port, *end, *tport, *tend;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700172
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600173 port = &res->start;
174 end = &res->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600177 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return 1;
179
180 /* check if the resource is already in use, skip if the
181 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700182 if (!dev->active) {
Jakub Sitnickieeeb98b2014-12-08 22:01:57 +0100183 if (!request_region(*port, length(port, end), "pnp"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return 0;
Jakub Sitnickieeeb98b2014-12-08 22:01:57 +0100185 release_region(*port, length(port, end));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187
188 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600189 for (i = 0; i < 8; i++) {
190 int rport = pnp_reserve_io[i << 1];
191 int rend = pnp_reserve_io[(i << 1) + 1] + rport - 1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700192 if (ranged_conflict(port, end, &rport, &rend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return 0;
194 }
195
196 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600197 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IO, i)); i++) {
198 if (tres != res && tres->flags & IORESOURCE_IO) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600199 tport = &tres->start;
200 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700201 if (ranged_conflict(port, end, tport, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return 0;
203 }
204 }
205
206 /* check for conflicts with other pnp devices */
207 pnp_for_each_dev(tdev) {
208 if (tdev == dev)
209 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600210 for (i = 0;
211 (tres = pnp_get_resource(tdev, IORESOURCE_IO, i));
212 i++) {
213 if (tres->flags & IORESOURCE_IO) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600214 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 continue;
Bjorn Helgaas11439a62010-05-03 10:47:21 -0600216 if (tres->flags & IORESOURCE_WINDOW)
217 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600218 tport = &tres->start;
219 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700220 if (ranged_conflict(port, end, tport, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return 0;
222 }
223 }
224 }
225
226 return 1;
227}
228
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600229int pnp_check_mem(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600231 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600233 struct resource *tres;
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700234 resource_size_t *addr, *end, *taddr, *tend;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700235
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600236 addr = &res->start;
237 end = &res->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600240 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return 1;
242
243 /* check if the resource is already in use, skip if the
244 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700245 if (!dev->active) {
Jakub Sitnickieeeb98b2014-12-08 22:01:57 +0100246 if (!request_mem_region(*addr, length(addr, end), "pnp"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return 0;
Jakub Sitnickieeeb98b2014-12-08 22:01:57 +0100248 release_mem_region(*addr, length(addr, end));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250
251 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600252 for (i = 0; i < 8; i++) {
253 int raddr = pnp_reserve_mem[i << 1];
254 int rend = pnp_reserve_mem[(i << 1) + 1] + raddr - 1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700255 if (ranged_conflict(addr, end, &raddr, &rend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return 0;
257 }
258
259 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600260 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_MEM, i)); i++) {
261 if (tres != res && tres->flags & IORESOURCE_MEM) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600262 taddr = &tres->start;
263 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700264 if (ranged_conflict(addr, end, taddr, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return 0;
266 }
267 }
268
269 /* check for conflicts with other pnp devices */
270 pnp_for_each_dev(tdev) {
271 if (tdev == dev)
272 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600273 for (i = 0;
274 (tres = pnp_get_resource(tdev, IORESOURCE_MEM, i));
275 i++) {
276 if (tres->flags & IORESOURCE_MEM) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600277 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 continue;
Bjorn Helgaas11439a62010-05-03 10:47:21 -0600279 if (tres->flags & IORESOURCE_WINDOW)
280 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600281 taddr = &tres->start;
282 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700283 if (ranged_conflict(addr, end, taddr, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return 0;
285 }
286 }
287 }
288
289 return 1;
290}
291
David Howells7d12e782006-10-05 14:55:46 +0100292static irqreturn_t pnp_test_handler(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 return IRQ_HANDLED;
295}
296
Bjorn Helgaas84684c72008-06-27 16:57:18 -0600297#ifdef CONFIG_PCI
298static int pci_dev_uses_irq(struct pnp_dev *pnp, struct pci_dev *pci,
299 unsigned int irq)
300{
301 u32 class;
302 u8 progif;
303
304 if (pci->irq == irq) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600305 pnp_dbg(&pnp->dev, " device %s using irq %d\n",
Bjorn Helgaas84684c72008-06-27 16:57:18 -0600306 pci_name(pci), irq);
307 return 1;
308 }
309
310 /*
311 * See pci_setup_device() and ata_pci_sff_activate_host() for
312 * similar IDE legacy detection.
313 */
314 pci_read_config_dword(pci, PCI_CLASS_REVISION, &class);
315 class >>= 8; /* discard revision ID */
316 progif = class & 0xff;
317 class >>= 8;
318
319 if (class == PCI_CLASS_STORAGE_IDE) {
320 /*
321 * Unless both channels are native-PCI mode only,
322 * treat the compatibility IRQs as busy.
323 */
324 if ((progif & 0x5) != 0x5)
325 if (pci_get_legacy_ide_irq(pci, 0) == irq ||
326 pci_get_legacy_ide_irq(pci, 1) == irq) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600327 pnp_dbg(&pnp->dev, " legacy IDE device %s "
Bjorn Helgaas84684c72008-06-27 16:57:18 -0600328 "using irq %d\n", pci_name(pci), irq);
329 return 1;
330 }
331 }
332
333 return 0;
334}
335#endif
336
337static int pci_uses_irq(struct pnp_dev *pnp, unsigned int irq)
338{
339#ifdef CONFIG_PCI
340 struct pci_dev *pci = NULL;
341
342 for_each_pci_dev(pci) {
343 if (pci_dev_uses_irq(pnp, pci, irq)) {
344 pci_dev_put(pci);
345 return 1;
346 }
347 }
348#endif
349 return 0;
350}
351
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600352int pnp_check_irq(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600354 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600356 struct resource *tres;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600357 resource_size_t *irq;
358
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600359 irq = &res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600362 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return 1;
364
365 /* check if the resource is valid */
Fabian Frederickff73b802014-05-10 12:47:15 +0200366 if (*irq > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return 0;
368
369 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600370 for (i = 0; i < 16; i++) {
371 if (pnp_reserve_irq[i] == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return 0;
373 }
374
375 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600376 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IRQ, i)); i++) {
377 if (tres != res && tres->flags & IORESOURCE_IRQ) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600378 if (tres->start == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return 0;
380 }
381 }
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /* check if the resource is being used by a pci device */
Bjorn Helgaas84684c72008-06-27 16:57:18 -0600384 if (pci_uses_irq(dev, *irq))
385 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 /* check if the resource is already in use, skip if the
388 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700389 if (!dev->active) {
Andrew Morton0cadaf42006-07-01 04:36:37 -0700390 if (request_irq(*irq, pnp_test_handler,
Michael Opdenackere1563762014-03-21 01:10:28 +0100391 IRQF_PROBE_SHARED, "pnp", NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return 0;
393 free_irq(*irq, NULL);
394 }
395
396 /* check for conflicts with other pnp devices */
397 pnp_for_each_dev(tdev) {
398 if (tdev == dev)
399 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600400 for (i = 0;
401 (tres = pnp_get_resource(tdev, IORESOURCE_IRQ, i));
402 i++) {
403 if (tres->flags & IORESOURCE_IRQ) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600404 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600406 if (tres->start == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return 0;
408 }
409 }
410 }
411
412 return 1;
413}
414
David Rientjes586f83e2011-03-22 16:34:56 -0700415#ifdef CONFIG_ISA_DMA_API
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600416int pnp_check_dma(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600418 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600420 struct resource *tres;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600421 resource_size_t *dma;
422
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600423 dma = &res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600426 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return 1;
428
429 /* check if the resource is valid */
Fabian Frederickff73b802014-05-10 12:47:15 +0200430 if (*dma == 4 || *dma > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return 0;
432
433 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600434 for (i = 0; i < 8; i++) {
435 if (pnp_reserve_dma[i] == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return 0;
437 }
438
439 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600440 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_DMA, i)); i++) {
441 if (tres != res && tres->flags & IORESOURCE_DMA) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600442 if (tres->start == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return 0;
444 }
445 }
446
447 /* check if the resource is already in use, skip if the
448 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700449 if (!dev->active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (request_dma(*dma, "pnp"))
451 return 0;
452 free_dma(*dma);
453 }
454
455 /* check for conflicts with other pnp devices */
456 pnp_for_each_dev(tdev) {
457 if (tdev == dev)
458 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600459 for (i = 0;
460 (tres = pnp_get_resource(tdev, IORESOURCE_DMA, i));
461 i++) {
462 if (tres->flags & IORESOURCE_DMA) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600463 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600465 if (tres->start == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return 0;
467 }
468 }
469 }
470
471 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
David Rientjes586f83e2011-03-22 16:34:56 -0700473#endif /* CONFIG_ISA_DMA_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Rene Hermanb563cf52008-10-15 22:03:58 -0700475unsigned long pnp_resource_type(struct resource *res)
Bjorn Helgaas940e98d2008-06-27 16:56:54 -0600476{
477 return res->flags & (IORESOURCE_IO | IORESOURCE_MEM |
Bjorn Helgaas7e0e9c02010-03-05 10:47:57 -0700478 IORESOURCE_IRQ | IORESOURCE_DMA |
479 IORESOURCE_BUS);
Bjorn Helgaas940e98d2008-06-27 16:56:54 -0600480}
481
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600482struct resource *pnp_get_resource(struct pnp_dev *dev,
Rene Hermanb563cf52008-10-15 22:03:58 -0700483 unsigned long type, unsigned int num)
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600484{
485 struct pnp_resource *pnp_res;
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600486 struct resource *res;
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600487
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600488 list_for_each_entry(pnp_res, &dev->resources, list) {
489 res = &pnp_res->res;
490 if (pnp_resource_type(res) == type && num-- == 0)
491 return res;
492 }
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600493 return NULL;
494}
Bjorn Helgaasb90eca02008-04-28 16:34:14 -0600495EXPORT_SYMBOL(pnp_get_resource);
496
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600497static struct pnp_resource *pnp_new_resource(struct pnp_dev *dev)
Bjorn Helgaasa50b6d72008-04-28 16:34:33 -0600498{
499 struct pnp_resource *pnp_res;
Bjorn Helgaasa50b6d72008-04-28 16:34:33 -0600500
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600501 pnp_res = kzalloc(sizeof(struct pnp_resource), GFP_KERNEL);
502 if (!pnp_res)
503 return NULL;
504
505 list_add_tail(&pnp_res->list, &dev->resources);
506 return pnp_res;
Bjorn Helgaasa50b6d72008-04-28 16:34:33 -0600507}
508
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100509struct pnp_resource *pnp_add_resource(struct pnp_dev *dev,
510 struct resource *res)
511{
512 struct pnp_resource *pnp_res;
513
514 pnp_res = pnp_new_resource(dev);
515 if (!pnp_res) {
516 dev_err(&dev->dev, "can't add resource %pR\n", res);
517 return NULL;
518 }
519
520 pnp_res->res = *res;
Liu ShuoX3c0fc072013-07-16 16:23:44 +0800521 pnp_res->res.name = dev->name;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100522 dev_dbg(&dev->dev, "%pR\n", res);
523 return pnp_res;
524}
525
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600526struct pnp_resource *pnp_add_irq_resource(struct pnp_dev *dev, int irq,
527 int flags)
528{
529 struct pnp_resource *pnp_res;
530 struct resource *res;
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600531
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600532 pnp_res = pnp_new_resource(dev);
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600533 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600534 dev_err(&dev->dev, "can't add resource for IRQ %d\n", irq);
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600535 return NULL;
536 }
537
538 res = &pnp_res->res;
539 res->flags = IORESOURCE_IRQ | flags;
540 res->start = irq;
541 res->end = irq;
542
Bjorn Helgaasc1f3f282010-09-29 12:24:23 -0600543 dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res);
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600544 return pnp_res;
545}
546
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600547struct pnp_resource *pnp_add_dma_resource(struct pnp_dev *dev, int dma,
548 int flags)
549{
550 struct pnp_resource *pnp_res;
551 struct resource *res;
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600552
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600553 pnp_res = pnp_new_resource(dev);
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600554 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600555 dev_err(&dev->dev, "can't add resource for DMA %d\n", dma);
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600556 return NULL;
557 }
558
559 res = &pnp_res->res;
560 res->flags = IORESOURCE_DMA | flags;
561 res->start = dma;
562 res->end = dma;
563
Bjorn Helgaasc1f3f282010-09-29 12:24:23 -0600564 dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res);
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600565 return pnp_res;
566}
567
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600568struct pnp_resource *pnp_add_io_resource(struct pnp_dev *dev,
569 resource_size_t start,
570 resource_size_t end, int flags)
571{
572 struct pnp_resource *pnp_res;
573 struct resource *res;
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600574
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600575 pnp_res = pnp_new_resource(dev);
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600576 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600577 dev_err(&dev->dev, "can't add resource for IO %#llx-%#llx\n",
578 (unsigned long long) start,
579 (unsigned long long) end);
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600580 return NULL;
581 }
582
583 res = &pnp_res->res;
584 res->flags = IORESOURCE_IO | flags;
585 res->start = start;
586 res->end = end;
587
Bjorn Helgaasc1f3f282010-09-29 12:24:23 -0600588 dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res);
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600589 return pnp_res;
590}
591
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600592struct pnp_resource *pnp_add_mem_resource(struct pnp_dev *dev,
593 resource_size_t start,
594 resource_size_t end, int flags)
595{
596 struct pnp_resource *pnp_res;
597 struct resource *res;
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600598
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600599 pnp_res = pnp_new_resource(dev);
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600600 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600601 dev_err(&dev->dev, "can't add resource for MEM %#llx-%#llx\n",
602 (unsigned long long) start,
603 (unsigned long long) end);
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600604 return NULL;
605 }
606
607 res = &pnp_res->res;
608 res->flags = IORESOURCE_MEM | flags;
609 res->start = start;
610 res->end = end;
611
Bjorn Helgaasc1f3f282010-09-29 12:24:23 -0600612 dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res);
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600613 return pnp_res;
614}
615
Bjorn Helgaas7e0e9c02010-03-05 10:47:57 -0700616struct pnp_resource *pnp_add_bus_resource(struct pnp_dev *dev,
617 resource_size_t start,
618 resource_size_t end)
619{
620 struct pnp_resource *pnp_res;
621 struct resource *res;
622
623 pnp_res = pnp_new_resource(dev);
624 if (!pnp_res) {
625 dev_err(&dev->dev, "can't add resource for BUS %#llx-%#llx\n",
626 (unsigned long long) start,
627 (unsigned long long) end);
628 return NULL;
629 }
630
631 res = &pnp_res->res;
632 res->flags = IORESOURCE_BUS;
633 res->start = start;
634 res->end = end;
635
Bjorn Helgaasc1f3f282010-09-29 12:24:23 -0600636 dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res);
Bjorn Helgaas7e0e9c02010-03-05 10:47:57 -0700637 return pnp_res;
638}
639
Bjorn Helgaas57fd51a2008-06-27 16:57:01 -0600640/*
641 * Determine whether the specified resource is a possible configuration
642 * for this device.
643 */
644int pnp_possible_config(struct pnp_dev *dev, int type, resource_size_t start,
645 resource_size_t size)
646{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600647 struct pnp_option *option;
648 struct pnp_port *port;
649 struct pnp_mem *mem;
650 struct pnp_irq *irq;
651 struct pnp_dma *dma;
Bjorn Helgaas57fd51a2008-06-27 16:57:01 -0600652
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600653 list_for_each_entry(option, &dev->options, list) {
654 if (option->type != type)
655 continue;
656
657 switch (option->type) {
658 case IORESOURCE_IO:
659 port = &option->u.port;
660 if (port->min == start && port->size == size)
661 return 1;
662 break;
663 case IORESOURCE_MEM:
664 mem = &option->u.mem;
665 if (mem->min == start && mem->size == size)
666 return 1;
667 break;
668 case IORESOURCE_IRQ:
669 irq = &option->u.irq;
670 if (start < PNP_IRQ_NR &&
671 test_bit(start, irq->map.bits))
672 return 1;
673 break;
674 case IORESOURCE_DMA:
675 dma = &option->u.dma;
676 if (dma->map & (1 << start))
677 return 1;
678 break;
679 }
680 }
Bjorn Helgaas57fd51a2008-06-27 16:57:01 -0600681
682 return 0;
683}
684EXPORT_SYMBOL(pnp_possible_config);
685
Bjorn Helgaas1b8e6962009-06-05 14:37:23 +0000686int pnp_range_reserved(resource_size_t start, resource_size_t end)
687{
688 struct pnp_dev *dev;
689 struct pnp_resource *pnp_res;
690 resource_size_t *dev_start, *dev_end;
691
692 pnp_for_each_dev(dev) {
693 list_for_each_entry(pnp_res, &dev->resources, list) {
694 dev_start = &pnp_res->res.start;
695 dev_end = &pnp_res->res.end;
696 if (ranged_conflict(&start, &end, dev_start, dev_end))
697 return 1;
698 }
699 }
700 return 0;
701}
702EXPORT_SYMBOL(pnp_range_reserved);
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704/* format is: pnp_reserve_irq=irq1[,irq2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705static int __init pnp_setup_reserve_irq(char *str)
706{
707 int i;
708
709 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700710 if (get_option(&str, &pnp_reserve_irq[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 break;
712 return 1;
713}
714
715__setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
716
717/* format is: pnp_reserve_dma=dma1[,dma2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718static int __init pnp_setup_reserve_dma(char *str)
719{
720 int i;
721
722 for (i = 0; i < 8; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700723 if (get_option(&str, &pnp_reserve_dma[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 break;
725 return 1;
726}
727
728__setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
729
730/* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731static int __init pnp_setup_reserve_io(char *str)
732{
733 int i;
734
735 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700736 if (get_option(&str, &pnp_reserve_io[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 break;
738 return 1;
739}
740
741__setup("pnp_reserve_io=", pnp_setup_reserve_io);
742
743/* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744static int __init pnp_setup_reserve_mem(char *str)
745{
746 int i;
747
748 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700749 if (get_option(&str, &pnp_reserve_mem[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 break;
751 return 1;
752}
753
754__setup("pnp_reserve_mem=", pnp_setup_reserve_mem);