blob: 2e54e6a23c72c8ff44b610412e3c1c92baac81b8 [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
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060034struct pnp_option *pnp_build_option(struct pnp_dev *dev, unsigned long type,
35 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) {
182 if (__check_region(&ioport_resource, *port, length(port, end)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return 0;
184 }
185
186 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600187 for (i = 0; i < 8; i++) {
188 int rport = pnp_reserve_io[i << 1];
189 int rend = pnp_reserve_io[(i << 1) + 1] + rport - 1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700190 if (ranged_conflict(port, end, &rport, &rend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return 0;
192 }
193
194 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600195 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IO, i)); i++) {
196 if (tres != res && tres->flags & IORESOURCE_IO) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600197 tport = &tres->start;
198 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700199 if (ranged_conflict(port, end, tport, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return 0;
201 }
202 }
203
204 /* check for conflicts with other pnp devices */
205 pnp_for_each_dev(tdev) {
206 if (tdev == dev)
207 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600208 for (i = 0;
209 (tres = pnp_get_resource(tdev, IORESOURCE_IO, i));
210 i++) {
211 if (tres->flags & IORESOURCE_IO) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600212 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600214 tport = &tres->start;
215 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700216 if (ranged_conflict(port, end, tport, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return 0;
218 }
219 }
220 }
221
222 return 1;
223}
224
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600225int pnp_check_mem(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600227 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600229 struct resource *tres;
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700230 resource_size_t *addr, *end, *taddr, *tend;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700231
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600232 addr = &res->start;
233 end = &res->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600236 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return 1;
238
239 /* check if the resource is already in use, skip if the
240 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700241 if (!dev->active) {
242 if (check_mem_region(*addr, length(addr, end)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return 0;
244 }
245
246 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600247 for (i = 0; i < 8; i++) {
248 int raddr = pnp_reserve_mem[i << 1];
249 int rend = pnp_reserve_mem[(i << 1) + 1] + raddr - 1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700250 if (ranged_conflict(addr, end, &raddr, &rend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return 0;
252 }
253
254 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600255 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_MEM, i)); i++) {
256 if (tres != res && tres->flags & IORESOURCE_MEM) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600257 taddr = &tres->start;
258 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700259 if (ranged_conflict(addr, end, taddr, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return 0;
261 }
262 }
263
264 /* check for conflicts with other pnp devices */
265 pnp_for_each_dev(tdev) {
266 if (tdev == dev)
267 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600268 for (i = 0;
269 (tres = pnp_get_resource(tdev, IORESOURCE_MEM, i));
270 i++) {
271 if (tres->flags & IORESOURCE_MEM) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600272 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600274 taddr = &tres->start;
275 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700276 if (ranged_conflict(addr, end, taddr, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return 0;
278 }
279 }
280 }
281
282 return 1;
283}
284
David Howells7d12e782006-10-05 14:55:46 +0100285static irqreturn_t pnp_test_handler(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 return IRQ_HANDLED;
288}
289
Bjorn Helgaas84684c72008-06-27 16:57:18 -0600290#ifdef CONFIG_PCI
291static int pci_dev_uses_irq(struct pnp_dev *pnp, struct pci_dev *pci,
292 unsigned int irq)
293{
294 u32 class;
295 u8 progif;
296
297 if (pci->irq == irq) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600298 pnp_dbg(&pnp->dev, " device %s using irq %d\n",
Bjorn Helgaas84684c72008-06-27 16:57:18 -0600299 pci_name(pci), irq);
300 return 1;
301 }
302
303 /*
304 * See pci_setup_device() and ata_pci_sff_activate_host() for
305 * similar IDE legacy detection.
306 */
307 pci_read_config_dword(pci, PCI_CLASS_REVISION, &class);
308 class >>= 8; /* discard revision ID */
309 progif = class & 0xff;
310 class >>= 8;
311
312 if (class == PCI_CLASS_STORAGE_IDE) {
313 /*
314 * Unless both channels are native-PCI mode only,
315 * treat the compatibility IRQs as busy.
316 */
317 if ((progif & 0x5) != 0x5)
318 if (pci_get_legacy_ide_irq(pci, 0) == irq ||
319 pci_get_legacy_ide_irq(pci, 1) == irq) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600320 pnp_dbg(&pnp->dev, " legacy IDE device %s "
Bjorn Helgaas84684c72008-06-27 16:57:18 -0600321 "using irq %d\n", pci_name(pci), irq);
322 return 1;
323 }
324 }
325
326 return 0;
327}
328#endif
329
330static int pci_uses_irq(struct pnp_dev *pnp, unsigned int irq)
331{
332#ifdef CONFIG_PCI
333 struct pci_dev *pci = NULL;
334
335 for_each_pci_dev(pci) {
336 if (pci_dev_uses_irq(pnp, pci, irq)) {
337 pci_dev_put(pci);
338 return 1;
339 }
340 }
341#endif
342 return 0;
343}
344
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600345int pnp_check_irq(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600347 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600349 struct resource *tres;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600350 resource_size_t *irq;
351
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600352 irq = &res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600355 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return 1;
357
358 /* check if the resource is valid */
359 if (*irq < 0 || *irq > 15)
360 return 0;
361
362 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600363 for (i = 0; i < 16; i++) {
364 if (pnp_reserve_irq[i] == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return 0;
366 }
367
368 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600369 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IRQ, i)); i++) {
370 if (tres != res && tres->flags & IORESOURCE_IRQ) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600371 if (tres->start == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return 0;
373 }
374 }
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 /* check if the resource is being used by a pci device */
Bjorn Helgaas84684c72008-06-27 16:57:18 -0600377 if (pci_uses_irq(dev, *irq))
378 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 /* check if the resource is already in use, skip if the
381 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700382 if (!dev->active) {
Andrew Morton0cadaf42006-07-01 04:36:37 -0700383 if (request_irq(*irq, pnp_test_handler,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700384 IRQF_DISABLED | IRQF_PROBE_SHARED, "pnp", NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return 0;
386 free_irq(*irq, NULL);
387 }
388
389 /* check for conflicts with other pnp devices */
390 pnp_for_each_dev(tdev) {
391 if (tdev == dev)
392 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600393 for (i = 0;
394 (tres = pnp_get_resource(tdev, IORESOURCE_IRQ, i));
395 i++) {
396 if (tres->flags & IORESOURCE_IRQ) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600397 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600399 if (tres->start == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return 0;
401 }
402 }
403 }
404
405 return 1;
406}
407
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600408int pnp_check_dma(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410#ifndef CONFIG_IA64
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600411 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600413 struct resource *tres;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600414 resource_size_t *dma;
415
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600416 dma = &res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600419 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return 1;
421
422 /* check if the resource is valid */
423 if (*dma < 0 || *dma == 4 || *dma > 7)
424 return 0;
425
426 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600427 for (i = 0; i < 8; i++) {
428 if (pnp_reserve_dma[i] == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return 0;
430 }
431
432 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600433 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_DMA, i)); i++) {
434 if (tres != res && tres->flags & IORESOURCE_DMA) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600435 if (tres->start == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return 0;
437 }
438 }
439
440 /* check if the resource is already in use, skip if the
441 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700442 if (!dev->active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (request_dma(*dma, "pnp"))
444 return 0;
445 free_dma(*dma);
446 }
447
448 /* check for conflicts with other pnp devices */
449 pnp_for_each_dev(tdev) {
450 if (tdev == dev)
451 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600452 for (i = 0;
453 (tres = pnp_get_resource(tdev, IORESOURCE_DMA, i));
454 i++) {
455 if (tres->flags & IORESOURCE_DMA) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600456 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600458 if (tres->start == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return 0;
460 }
461 }
462 }
463
464 return 1;
465#else
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700466 /* IA64 does not have legacy DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return 0;
468#endif
469}
470
Rene Hermanb563cf52008-10-15 22:03:58 -0700471unsigned long pnp_resource_type(struct resource *res)
Bjorn Helgaas940e98d2008-06-27 16:56:54 -0600472{
473 return res->flags & (IORESOURCE_IO | IORESOURCE_MEM |
Bjorn Helgaas7e0e9c02010-03-05 10:47:57 -0700474 IORESOURCE_IRQ | IORESOURCE_DMA |
475 IORESOURCE_BUS);
Bjorn Helgaas940e98d2008-06-27 16:56:54 -0600476}
477
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600478struct resource *pnp_get_resource(struct pnp_dev *dev,
Rene Hermanb563cf52008-10-15 22:03:58 -0700479 unsigned long type, unsigned int num)
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600480{
481 struct pnp_resource *pnp_res;
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600482 struct resource *res;
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600483
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600484 list_for_each_entry(pnp_res, &dev->resources, list) {
485 res = &pnp_res->res;
486 if (pnp_resource_type(res) == type && num-- == 0)
487 return res;
488 }
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600489 return NULL;
490}
Bjorn Helgaasb90eca02008-04-28 16:34:14 -0600491EXPORT_SYMBOL(pnp_get_resource);
492
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600493static struct pnp_resource *pnp_new_resource(struct pnp_dev *dev)
Bjorn Helgaasa50b6d72008-04-28 16:34:33 -0600494{
495 struct pnp_resource *pnp_res;
Bjorn Helgaasa50b6d72008-04-28 16:34:33 -0600496
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600497 pnp_res = kzalloc(sizeof(struct pnp_resource), GFP_KERNEL);
498 if (!pnp_res)
499 return NULL;
500
501 list_add_tail(&pnp_res->list, &dev->resources);
502 return pnp_res;
Bjorn Helgaasa50b6d72008-04-28 16:34:33 -0600503}
504
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600505struct pnp_resource *pnp_add_irq_resource(struct pnp_dev *dev, int irq,
506 int flags)
507{
508 struct pnp_resource *pnp_res;
509 struct resource *res;
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600510
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600511 pnp_res = pnp_new_resource(dev);
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600512 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600513 dev_err(&dev->dev, "can't add resource for IRQ %d\n", irq);
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600514 return NULL;
515 }
516
517 res = &pnp_res->res;
518 res->flags = IORESOURCE_IRQ | flags;
519 res->start = irq;
520 res->end = irq;
521
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600522 pnp_dbg(&dev->dev, " add %pr\n", res);
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600523 return pnp_res;
524}
525
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600526struct pnp_resource *pnp_add_dma_resource(struct pnp_dev *dev, int dma,
527 int flags)
528{
529 struct pnp_resource *pnp_res;
530 struct resource *res;
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600531
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600532 pnp_res = pnp_new_resource(dev);
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600533 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600534 dev_err(&dev->dev, "can't add resource for DMA %d\n", dma);
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600535 return NULL;
536 }
537
538 res = &pnp_res->res;
539 res->flags = IORESOURCE_DMA | flags;
540 res->start = dma;
541 res->end = dma;
542
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600543 pnp_dbg(&dev->dev, " add %pr\n", res);
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600544 return pnp_res;
545}
546
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600547struct pnp_resource *pnp_add_io_resource(struct pnp_dev *dev,
548 resource_size_t start,
549 resource_size_t end, int flags)
550{
551 struct pnp_resource *pnp_res;
552 struct resource *res;
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600553
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600554 pnp_res = pnp_new_resource(dev);
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600555 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600556 dev_err(&dev->dev, "can't add resource for IO %#llx-%#llx\n",
557 (unsigned long long) start,
558 (unsigned long long) end);
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600559 return NULL;
560 }
561
562 res = &pnp_res->res;
563 res->flags = IORESOURCE_IO | flags;
564 res->start = start;
565 res->end = end;
566
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600567 pnp_dbg(&dev->dev, " add %pr\n", res);
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600568 return pnp_res;
569}
570
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600571struct pnp_resource *pnp_add_mem_resource(struct pnp_dev *dev,
572 resource_size_t start,
573 resource_size_t end, int flags)
574{
575 struct pnp_resource *pnp_res;
576 struct resource *res;
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600577
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600578 pnp_res = pnp_new_resource(dev);
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600579 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600580 dev_err(&dev->dev, "can't add resource for MEM %#llx-%#llx\n",
581 (unsigned long long) start,
582 (unsigned long long) end);
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600583 return NULL;
584 }
585
586 res = &pnp_res->res;
587 res->flags = IORESOURCE_MEM | flags;
588 res->start = start;
589 res->end = end;
590
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600591 pnp_dbg(&dev->dev, " add %pr\n", res);
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600592 return pnp_res;
593}
594
Bjorn Helgaas7e0e9c02010-03-05 10:47:57 -0700595struct pnp_resource *pnp_add_bus_resource(struct pnp_dev *dev,
596 resource_size_t start,
597 resource_size_t end)
598{
599 struct pnp_resource *pnp_res;
600 struct resource *res;
601
602 pnp_res = pnp_new_resource(dev);
603 if (!pnp_res) {
604 dev_err(&dev->dev, "can't add resource for BUS %#llx-%#llx\n",
605 (unsigned long long) start,
606 (unsigned long long) end);
607 return NULL;
608 }
609
610 res = &pnp_res->res;
611 res->flags = IORESOURCE_BUS;
612 res->start = start;
613 res->end = end;
614
615 pnp_dbg(&dev->dev, " add %pr\n", res);
616 return pnp_res;
617}
618
Bjorn Helgaas57fd51a2008-06-27 16:57:01 -0600619/*
620 * Determine whether the specified resource is a possible configuration
621 * for this device.
622 */
623int pnp_possible_config(struct pnp_dev *dev, int type, resource_size_t start,
624 resource_size_t size)
625{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600626 struct pnp_option *option;
627 struct pnp_port *port;
628 struct pnp_mem *mem;
629 struct pnp_irq *irq;
630 struct pnp_dma *dma;
Bjorn Helgaas57fd51a2008-06-27 16:57:01 -0600631
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600632 list_for_each_entry(option, &dev->options, list) {
633 if (option->type != type)
634 continue;
635
636 switch (option->type) {
637 case IORESOURCE_IO:
638 port = &option->u.port;
639 if (port->min == start && port->size == size)
640 return 1;
641 break;
642 case IORESOURCE_MEM:
643 mem = &option->u.mem;
644 if (mem->min == start && mem->size == size)
645 return 1;
646 break;
647 case IORESOURCE_IRQ:
648 irq = &option->u.irq;
649 if (start < PNP_IRQ_NR &&
650 test_bit(start, irq->map.bits))
651 return 1;
652 break;
653 case IORESOURCE_DMA:
654 dma = &option->u.dma;
655 if (dma->map & (1 << start))
656 return 1;
657 break;
658 }
659 }
Bjorn Helgaas57fd51a2008-06-27 16:57:01 -0600660
661 return 0;
662}
663EXPORT_SYMBOL(pnp_possible_config);
664
Bjorn Helgaas1b8e6962009-06-05 14:37:23 +0000665int pnp_range_reserved(resource_size_t start, resource_size_t end)
666{
667 struct pnp_dev *dev;
668 struct pnp_resource *pnp_res;
669 resource_size_t *dev_start, *dev_end;
670
671 pnp_for_each_dev(dev) {
672 list_for_each_entry(pnp_res, &dev->resources, list) {
673 dev_start = &pnp_res->res.start;
674 dev_end = &pnp_res->res.end;
675 if (ranged_conflict(&start, &end, dev_start, dev_end))
676 return 1;
677 }
678 }
679 return 0;
680}
681EXPORT_SYMBOL(pnp_range_reserved);
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683/* format is: pnp_reserve_irq=irq1[,irq2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684static int __init pnp_setup_reserve_irq(char *str)
685{
686 int i;
687
688 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700689 if (get_option(&str, &pnp_reserve_irq[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 break;
691 return 1;
692}
693
694__setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
695
696/* format is: pnp_reserve_dma=dma1[,dma2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697static int __init pnp_setup_reserve_dma(char *str)
698{
699 int i;
700
701 for (i = 0; i < 8; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700702 if (get_option(&str, &pnp_reserve_dma[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 break;
704 return 1;
705}
706
707__setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
708
709/* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710static int __init pnp_setup_reserve_io(char *str)
711{
712 int i;
713
714 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700715 if (get_option(&str, &pnp_reserve_io[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 break;
717 return 1;
718}
719
720__setup("pnp_reserve_io=", pnp_setup_reserve_io);
721
722/* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723static int __init pnp_setup_reserve_mem(char *str)
724{
725 int i;
726
727 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700728 if (get_option(&str, &pnp_reserve_mem[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 break;
730 return 1;
731}
732
733__setup("pnp_reserve_mem=", pnp_setup_reserve_mem);