blob: f980ff7166e98e93546f7f79657087983a5388ce [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * resource.c - Contains functions for registering and analyzing resource information
3 *
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02004 * based on isapnp.c resource management (c) Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -06006 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
7 * Bjorn Helgaas <bjorn.helgaas@hp.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/errno.h>
13#include <linux/interrupt.h>
14#include <linux/kernel.h>
15#include <asm/io.h>
16#include <asm/dma.h>
17#include <asm/irq.h>
18#include <linux/pci.h>
19#include <linux/ioport.h>
20#include <linux/init.h>
21
22#include <linux/pnp.h>
23#include "base.h"
24
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070025static int pnp_reserve_irq[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some IRQ */
26static int pnp_reserve_dma[8] = {[0 ... 7] = -1 }; /* reserve (don't use) some DMA */
27static int pnp_reserve_io[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some I/O region */
28static int pnp_reserve_mem[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some memory region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/*
31 * option registration
32 */
33
Rashika Kheria62c6dae2013-12-14 18:50:56 +053034static struct pnp_option *pnp_build_option(struct pnp_dev *dev, unsigned long type,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060035 unsigned int option_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060037 struct pnp_option *option;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060039 option = kzalloc(sizeof(struct pnp_option), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 if (!option)
41 return NULL;
42
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060043 option->flags = option_flags;
44 option->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060046 list_add_tail(&option->list, &dev->options);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 return option;
48}
49
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060050int pnp_register_irq_resource(struct pnp_dev *dev, unsigned int option_flags,
Bjorn Helgaasc2275362008-06-27 16:57:11 -060051 pnp_irq_mask_t *map, unsigned char flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060053 struct pnp_option *option;
54 struct pnp_irq *irq;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070055
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060056 option = pnp_build_option(dev, IORESOURCE_IRQ, option_flags);
57 if (!option)
Bjorn Helgaasc2275362008-06-27 16:57:11 -060058 return -ENOMEM;
59
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060060 irq = &option->u.irq;
Bjorn Helgaas2d29a7a2008-06-27 16:57:13 -060061 irq->map = *map;
62 irq->flags = flags;
Bjorn Helgaasc2275362008-06-27 16:57:11 -060063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#ifdef CONFIG_PCI
65 {
66 int i;
67
68 for (i = 0; i < 16; i++)
Bjorn Helgaas2d29a7a2008-06-27 16:57:13 -060069 if (test_bit(i, irq->map.bits))
David Shaohua Lic9c3e452005-04-01 00:07:31 -050070 pcibios_penalize_isa_irq(i, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 }
72#endif
Bjorn Helgaasc1caf062008-04-28 16:34:04 -060073
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060074 dbg_pnp_show_option(dev, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return 0;
76}
77
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060078int pnp_register_dma_resource(struct pnp_dev *dev, unsigned int option_flags,
Bjorn Helgaasc2275362008-06-27 16:57:11 -060079 unsigned char map, unsigned char flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060081 struct pnp_option *option;
82 struct pnp_dma *dma;
Bjorn Helgaasc2275362008-06-27 16:57:11 -060083
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060084 option = pnp_build_option(dev, IORESOURCE_DMA, option_flags);
85 if (!option)
Bjorn Helgaasc2275362008-06-27 16:57:11 -060086 return -ENOMEM;
87
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060088 dma = &option->u.dma;
Bjorn Helgaas2d29a7a2008-06-27 16:57:13 -060089 dma->map = map;
90 dma->flags = flags;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070091
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060092 dbg_pnp_show_option(dev, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return 0;
94}
95
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060096int pnp_register_port_resource(struct pnp_dev *dev, unsigned int option_flags,
Bjorn Helgaasc2275362008-06-27 16:57:11 -060097 resource_size_t min, resource_size_t max,
98 resource_size_t align, resource_size_t size,
99 unsigned char flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600101 struct pnp_option *option;
102 struct pnp_port *port;
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600103
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600104 option = pnp_build_option(dev, IORESOURCE_IO, option_flags);
105 if (!option)
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600106 return -ENOMEM;
107
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600108 port = &option->u.port;
Bjorn Helgaas2d29a7a2008-06-27 16:57:13 -0600109 port->min = min;
110 port->max = max;
111 port->align = align;
112 port->size = size;
113 port->flags = flags;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700114
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600115 dbg_pnp_show_option(dev, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return 0;
117}
118
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600119int pnp_register_mem_resource(struct pnp_dev *dev, unsigned int option_flags,
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600120 resource_size_t min, resource_size_t max,
121 resource_size_t align, resource_size_t size,
122 unsigned char flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600124 struct pnp_option *option;
125 struct pnp_mem *mem;
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600126
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600127 option = pnp_build_option(dev, IORESOURCE_MEM, option_flags);
128 if (!option)
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600129 return -ENOMEM;
130
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600131 mem = &option->u.mem;
Bjorn Helgaas2d29a7a2008-06-27 16:57:13 -0600132 mem->min = min;
133 mem->max = max;
134 mem->align = align;
135 mem->size = size;
136 mem->flags = flags;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700137
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600138 dbg_pnp_show_option(dev, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return 0;
140}
141
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600142void pnp_free_options(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600144 struct pnp_option *option, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600146 list_for_each_entry_safe(option, tmp, &dev->options, list) {
147 list_del(&option->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 kfree(option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/*
153 * resource validity checking
154 */
155
156#define length(start, end) (*(end) - *(start) + 1)
157
158/* Two ranges conflict if one doesn't end before the other starts */
159#define ranged_conflict(starta, enda, startb, endb) \
160 !((*(enda) < *(startb)) || (*(endb) < *(starta)))
161
162#define cannot_compare(flags) \
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600163((flags) & IORESOURCE_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600165int pnp_check_port(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600167 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600169 struct resource *tres;
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700170 resource_size_t *port, *end, *tport, *tend;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700171
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600172 port = &res->start;
173 end = &res->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600176 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return 1;
178
179 /* check if the resource is already in use, skip if the
180 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700181 if (!dev->active) {
Jakub Sitnickieeeb98b2014-12-08 22:01:57 +0100182 if (!request_region(*port, length(port, end), "pnp"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return 0;
Jakub Sitnickieeeb98b2014-12-08 22:01:57 +0100184 release_region(*port, length(port, end));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186
187 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600188 for (i = 0; i < 8; i++) {
189 int rport = pnp_reserve_io[i << 1];
190 int rend = pnp_reserve_io[(i << 1) + 1] + rport - 1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700191 if (ranged_conflict(port, end, &rport, &rend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return 0;
193 }
194
195 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600196 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IO, i)); i++) {
197 if (tres != res && tres->flags & IORESOURCE_IO) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600198 tport = &tres->start;
199 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700200 if (ranged_conflict(port, end, tport, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return 0;
202 }
203 }
204
205 /* check for conflicts with other pnp devices */
206 pnp_for_each_dev(tdev) {
207 if (tdev == dev)
208 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600209 for (i = 0;
210 (tres = pnp_get_resource(tdev, IORESOURCE_IO, i));
211 i++) {
212 if (tres->flags & IORESOURCE_IO) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600213 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 continue;
Bjorn Helgaas11439a62010-05-03 10:47:21 -0600215 if (tres->flags & IORESOURCE_WINDOW)
216 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600217 tport = &tres->start;
218 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700219 if (ranged_conflict(port, end, tport, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return 0;
221 }
222 }
223 }
224
225 return 1;
226}
227
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600228int pnp_check_mem(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600230 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600232 struct resource *tres;
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700233 resource_size_t *addr, *end, *taddr, *tend;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700234
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600235 addr = &res->start;
236 end = &res->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600239 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return 1;
241
242 /* check if the resource is already in use, skip if the
243 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700244 if (!dev->active) {
Jakub Sitnickieeeb98b2014-12-08 22:01:57 +0100245 if (!request_mem_region(*addr, length(addr, end), "pnp"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return 0;
Jakub Sitnickieeeb98b2014-12-08 22:01:57 +0100247 release_mem_region(*addr, length(addr, end));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249
250 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600251 for (i = 0; i < 8; i++) {
252 int raddr = pnp_reserve_mem[i << 1];
253 int rend = pnp_reserve_mem[(i << 1) + 1] + raddr - 1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700254 if (ranged_conflict(addr, end, &raddr, &rend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return 0;
256 }
257
258 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600259 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_MEM, i)); i++) {
260 if (tres != res && tres->flags & IORESOURCE_MEM) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600261 taddr = &tres->start;
262 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700263 if (ranged_conflict(addr, end, taddr, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return 0;
265 }
266 }
267
268 /* check for conflicts with other pnp devices */
269 pnp_for_each_dev(tdev) {
270 if (tdev == dev)
271 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600272 for (i = 0;
273 (tres = pnp_get_resource(tdev, IORESOURCE_MEM, i));
274 i++) {
275 if (tres->flags & IORESOURCE_MEM) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600276 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 continue;
Bjorn Helgaas11439a62010-05-03 10:47:21 -0600278 if (tres->flags & IORESOURCE_WINDOW)
279 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600280 taddr = &tres->start;
281 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700282 if (ranged_conflict(addr, end, taddr, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return 0;
284 }
285 }
286 }
287
288 return 1;
289}
290
David Howells7d12e782006-10-05 14:55:46 +0100291static irqreturn_t pnp_test_handler(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 return IRQ_HANDLED;
294}
295
Bjorn Helgaas84684c72008-06-27 16:57:18 -0600296#ifdef CONFIG_PCI
297static int pci_dev_uses_irq(struct pnp_dev *pnp, struct pci_dev *pci,
298 unsigned int irq)
299{
300 u32 class;
301 u8 progif;
302
303 if (pci->irq == irq) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600304 pnp_dbg(&pnp->dev, " device %s using irq %d\n",
Bjorn Helgaas84684c72008-06-27 16:57:18 -0600305 pci_name(pci), irq);
306 return 1;
307 }
308
309 /*
310 * See pci_setup_device() and ata_pci_sff_activate_host() for
311 * similar IDE legacy detection.
312 */
313 pci_read_config_dword(pci, PCI_CLASS_REVISION, &class);
314 class >>= 8; /* discard revision ID */
315 progif = class & 0xff;
316 class >>= 8;
317
318 if (class == PCI_CLASS_STORAGE_IDE) {
319 /*
320 * Unless both channels are native-PCI mode only,
321 * treat the compatibility IRQs as busy.
322 */
323 if ((progif & 0x5) != 0x5)
324 if (pci_get_legacy_ide_irq(pci, 0) == irq ||
325 pci_get_legacy_ide_irq(pci, 1) == irq) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600326 pnp_dbg(&pnp->dev, " legacy IDE device %s "
Bjorn Helgaas84684c72008-06-27 16:57:18 -0600327 "using irq %d\n", pci_name(pci), irq);
328 return 1;
329 }
330 }
331
332 return 0;
333}
334#endif
335
336static int pci_uses_irq(struct pnp_dev *pnp, unsigned int irq)
337{
338#ifdef CONFIG_PCI
339 struct pci_dev *pci = NULL;
340
341 for_each_pci_dev(pci) {
342 if (pci_dev_uses_irq(pnp, pci, irq)) {
343 pci_dev_put(pci);
344 return 1;
345 }
346 }
347#endif
348 return 0;
349}
350
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600351int pnp_check_irq(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600353 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600355 struct resource *tres;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600356 resource_size_t *irq;
357
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600358 irq = &res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600361 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return 1;
363
364 /* check if the resource is valid */
Fabian Frederickff73b802014-05-10 12:47:15 +0200365 if (*irq > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return 0;
367
368 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600369 for (i = 0; i < 16; i++) {
370 if (pnp_reserve_irq[i] == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return 0;
372 }
373
374 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600375 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IRQ, i)); i++) {
376 if (tres != res && tres->flags & IORESOURCE_IRQ) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600377 if (tres->start == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return 0;
379 }
380 }
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /* check if the resource is being used by a pci device */
Bjorn Helgaas84684c72008-06-27 16:57:18 -0600383 if (pci_uses_irq(dev, *irq))
384 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 /* check if the resource is already in use, skip if the
387 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700388 if (!dev->active) {
Andrew Morton0cadaf42006-07-01 04:36:37 -0700389 if (request_irq(*irq, pnp_test_handler,
Michael Opdenackere1563762014-03-21 01:10:28 +0100390 IRQF_PROBE_SHARED, "pnp", NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return 0;
392 free_irq(*irq, NULL);
393 }
394
395 /* check for conflicts with other pnp devices */
396 pnp_for_each_dev(tdev) {
397 if (tdev == dev)
398 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600399 for (i = 0;
400 (tres = pnp_get_resource(tdev, IORESOURCE_IRQ, i));
401 i++) {
402 if (tres->flags & IORESOURCE_IRQ) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600403 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600405 if (tres->start == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return 0;
407 }
408 }
409 }
410
411 return 1;
412}
413
David Rientjes586f83e2011-03-22 16:34:56 -0700414#ifdef CONFIG_ISA_DMA_API
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600415int pnp_check_dma(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600417 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600419 struct resource *tres;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600420 resource_size_t *dma;
421
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600422 dma = &res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600425 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return 1;
427
428 /* check if the resource is valid */
Fabian Frederickff73b802014-05-10 12:47:15 +0200429 if (*dma == 4 || *dma > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return 0;
431
432 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600433 for (i = 0; i < 8; i++) {
434 if (pnp_reserve_dma[i] == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return 0;
436 }
437
438 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600439 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_DMA, i)); i++) {
440 if (tres != res && tres->flags & IORESOURCE_DMA) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600441 if (tres->start == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return 0;
443 }
444 }
445
446 /* check if the resource is already in use, skip if the
447 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700448 if (!dev->active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (request_dma(*dma, "pnp"))
450 return 0;
451 free_dma(*dma);
452 }
453
454 /* check for conflicts with other pnp devices */
455 pnp_for_each_dev(tdev) {
456 if (tdev == dev)
457 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600458 for (i = 0;
459 (tres = pnp_get_resource(tdev, IORESOURCE_DMA, i));
460 i++) {
461 if (tres->flags & IORESOURCE_DMA) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600462 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600464 if (tres->start == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return 0;
466 }
467 }
468 }
469
470 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
David Rientjes586f83e2011-03-22 16:34:56 -0700472#endif /* CONFIG_ISA_DMA_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Rene Hermanb563cf52008-10-15 22:03:58 -0700474unsigned long pnp_resource_type(struct resource *res)
Bjorn Helgaas940e98d2008-06-27 16:56:54 -0600475{
476 return res->flags & (IORESOURCE_IO | IORESOURCE_MEM |
Bjorn Helgaas7e0e9c02010-03-05 10:47:57 -0700477 IORESOURCE_IRQ | IORESOURCE_DMA |
478 IORESOURCE_BUS);
Bjorn Helgaas940e98d2008-06-27 16:56:54 -0600479}
480
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600481struct resource *pnp_get_resource(struct pnp_dev *dev,
Rene Hermanb563cf52008-10-15 22:03:58 -0700482 unsigned long type, unsigned int num)
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600483{
484 struct pnp_resource *pnp_res;
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600485 struct resource *res;
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600486
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600487 list_for_each_entry(pnp_res, &dev->resources, list) {
488 res = &pnp_res->res;
489 if (pnp_resource_type(res) == type && num-- == 0)
490 return res;
491 }
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600492 return NULL;
493}
Bjorn Helgaasb90eca02008-04-28 16:34:14 -0600494EXPORT_SYMBOL(pnp_get_resource);
495
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600496static struct pnp_resource *pnp_new_resource(struct pnp_dev *dev)
Bjorn Helgaasa50b6d72008-04-28 16:34:33 -0600497{
498 struct pnp_resource *pnp_res;
Bjorn Helgaasa50b6d72008-04-28 16:34:33 -0600499
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600500 pnp_res = kzalloc(sizeof(struct pnp_resource), GFP_KERNEL);
501 if (!pnp_res)
502 return NULL;
503
504 list_add_tail(&pnp_res->list, &dev->resources);
505 return pnp_res;
Bjorn Helgaasa50b6d72008-04-28 16:34:33 -0600506}
507
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100508struct pnp_resource *pnp_add_resource(struct pnp_dev *dev,
509 struct resource *res)
510{
511 struct pnp_resource *pnp_res;
512
513 pnp_res = pnp_new_resource(dev);
514 if (!pnp_res) {
515 dev_err(&dev->dev, "can't add resource %pR\n", res);
516 return NULL;
517 }
518
519 pnp_res->res = *res;
Liu ShuoX3c0fc072013-07-16 16:23:44 +0800520 pnp_res->res.name = dev->name;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100521 dev_dbg(&dev->dev, "%pR\n", res);
522 return pnp_res;
523}
524
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600525struct pnp_resource *pnp_add_irq_resource(struct pnp_dev *dev, int irq,
526 int flags)
527{
528 struct pnp_resource *pnp_res;
529 struct resource *res;
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600530
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600531 pnp_res = pnp_new_resource(dev);
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600532 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600533 dev_err(&dev->dev, "can't add resource for IRQ %d\n", irq);
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600534 return NULL;
535 }
536
537 res = &pnp_res->res;
538 res->flags = IORESOURCE_IRQ | flags;
539 res->start = irq;
540 res->end = irq;
541
Bjorn Helgaasc1f3f282010-09-29 12:24:23 -0600542 dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res);
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600543 return pnp_res;
544}
545
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600546struct pnp_resource *pnp_add_dma_resource(struct pnp_dev *dev, int dma,
547 int flags)
548{
549 struct pnp_resource *pnp_res;
550 struct resource *res;
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600551
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600552 pnp_res = pnp_new_resource(dev);
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600553 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600554 dev_err(&dev->dev, "can't add resource for DMA %d\n", dma);
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600555 return NULL;
556 }
557
558 res = &pnp_res->res;
559 res->flags = IORESOURCE_DMA | flags;
560 res->start = dma;
561 res->end = dma;
562
Bjorn Helgaasc1f3f282010-09-29 12:24:23 -0600563 dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res);
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600564 return pnp_res;
565}
566
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600567struct pnp_resource *pnp_add_io_resource(struct pnp_dev *dev,
568 resource_size_t start,
569 resource_size_t end, int flags)
570{
571 struct pnp_resource *pnp_res;
572 struct resource *res;
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600573
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600574 pnp_res = pnp_new_resource(dev);
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600575 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600576 dev_err(&dev->dev, "can't add resource for IO %#llx-%#llx\n",
577 (unsigned long long) start,
578 (unsigned long long) end);
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600579 return NULL;
580 }
581
582 res = &pnp_res->res;
583 res->flags = IORESOURCE_IO | flags;
584 res->start = start;
585 res->end = end;
586
Bjorn Helgaasc1f3f282010-09-29 12:24:23 -0600587 dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res);
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600588 return pnp_res;
589}
590
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600591struct pnp_resource *pnp_add_mem_resource(struct pnp_dev *dev,
592 resource_size_t start,
593 resource_size_t end, int flags)
594{
595 struct pnp_resource *pnp_res;
596 struct resource *res;
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600597
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600598 pnp_res = pnp_new_resource(dev);
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600599 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600600 dev_err(&dev->dev, "can't add resource for MEM %#llx-%#llx\n",
601 (unsigned long long) start,
602 (unsigned long long) end);
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600603 return NULL;
604 }
605
606 res = &pnp_res->res;
607 res->flags = IORESOURCE_MEM | flags;
608 res->start = start;
609 res->end = end;
610
Bjorn Helgaasc1f3f282010-09-29 12:24:23 -0600611 dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res);
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600612 return pnp_res;
613}
614
Bjorn Helgaas7e0e9c02010-03-05 10:47:57 -0700615struct pnp_resource *pnp_add_bus_resource(struct pnp_dev *dev,
616 resource_size_t start,
617 resource_size_t end)
618{
619 struct pnp_resource *pnp_res;
620 struct resource *res;
621
622 pnp_res = pnp_new_resource(dev);
623 if (!pnp_res) {
624 dev_err(&dev->dev, "can't add resource for BUS %#llx-%#llx\n",
625 (unsigned long long) start,
626 (unsigned long long) end);
627 return NULL;
628 }
629
630 res = &pnp_res->res;
631 res->flags = IORESOURCE_BUS;
632 res->start = start;
633 res->end = end;
634
Bjorn Helgaasc1f3f282010-09-29 12:24:23 -0600635 dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res);
Bjorn Helgaas7e0e9c02010-03-05 10:47:57 -0700636 return pnp_res;
637}
638
Bjorn Helgaas57fd51a2008-06-27 16:57:01 -0600639/*
640 * Determine whether the specified resource is a possible configuration
641 * for this device.
642 */
643int pnp_possible_config(struct pnp_dev *dev, int type, resource_size_t start,
644 resource_size_t size)
645{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600646 struct pnp_option *option;
647 struct pnp_port *port;
648 struct pnp_mem *mem;
649 struct pnp_irq *irq;
650 struct pnp_dma *dma;
Bjorn Helgaas57fd51a2008-06-27 16:57:01 -0600651
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600652 list_for_each_entry(option, &dev->options, list) {
653 if (option->type != type)
654 continue;
655
656 switch (option->type) {
657 case IORESOURCE_IO:
658 port = &option->u.port;
659 if (port->min == start && port->size == size)
660 return 1;
661 break;
662 case IORESOURCE_MEM:
663 mem = &option->u.mem;
664 if (mem->min == start && mem->size == size)
665 return 1;
666 break;
667 case IORESOURCE_IRQ:
668 irq = &option->u.irq;
669 if (start < PNP_IRQ_NR &&
670 test_bit(start, irq->map.bits))
671 return 1;
672 break;
673 case IORESOURCE_DMA:
674 dma = &option->u.dma;
675 if (dma->map & (1 << start))
676 return 1;
677 break;
678 }
679 }
Bjorn Helgaas57fd51a2008-06-27 16:57:01 -0600680
681 return 0;
682}
683EXPORT_SYMBOL(pnp_possible_config);
684
Bjorn Helgaas1b8e6962009-06-05 14:37:23 +0000685int pnp_range_reserved(resource_size_t start, resource_size_t end)
686{
687 struct pnp_dev *dev;
688 struct pnp_resource *pnp_res;
689 resource_size_t *dev_start, *dev_end;
690
691 pnp_for_each_dev(dev) {
692 list_for_each_entry(pnp_res, &dev->resources, list) {
693 dev_start = &pnp_res->res.start;
694 dev_end = &pnp_res->res.end;
695 if (ranged_conflict(&start, &end, dev_start, dev_end))
696 return 1;
697 }
698 }
699 return 0;
700}
701EXPORT_SYMBOL(pnp_range_reserved);
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703/* format is: pnp_reserve_irq=irq1[,irq2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704static int __init pnp_setup_reserve_irq(char *str)
705{
706 int i;
707
708 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700709 if (get_option(&str, &pnp_reserve_irq[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 break;
711 return 1;
712}
713
714__setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
715
716/* format is: pnp_reserve_dma=dma1[,dma2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717static int __init pnp_setup_reserve_dma(char *str)
718{
719 int i;
720
721 for (i = 0; i < 8; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700722 if (get_option(&str, &pnp_reserve_dma[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 break;
724 return 1;
725}
726
727__setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
728
729/* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730static int __init pnp_setup_reserve_io(char *str)
731{
732 int i;
733
734 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700735 if (get_option(&str, &pnp_reserve_io[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 break;
737 return 1;
738}
739
740__setup("pnp_reserve_io=", pnp_setup_reserve_io);
741
742/* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743static int __init pnp_setup_reserve_mem(char *str)
744{
745 int i;
746
747 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700748 if (get_option(&str, &pnp_reserve_mem[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 break;
750 return 1;
751}
752
753__setup("pnp_reserve_mem=", pnp_setup_reserve_mem);