blob: 635b11a0cf82977a85baaad86d478fd6890c2065 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * resource.c - Contains functions for registering and analyzing resource information
3 *
4 * based on isapnp.c resource management (c) Jaroslav Kysela <perex@suse.cz>
5 * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
6 *
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/module.h>
10#include <linux/errno.h>
11#include <linux/interrupt.h>
12#include <linux/kernel.h>
13#include <asm/io.h>
14#include <asm/dma.h>
15#include <asm/irq.h>
16#include <linux/pci.h>
17#include <linux/ioport.h>
18#include <linux/init.h>
19
20#include <linux/pnp.h>
21#include "base.h"
22
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070023static int pnp_reserve_irq[16] = {[0...15] = -1 }; /* reserve (don't use) some IRQ */
24static int pnp_reserve_dma[8] = {[0...7] = -1 }; /* reserve (don't use) some DMA */
25static int pnp_reserve_io[16] = {[0...15] = -1 }; /* reserve (don't use) some I/O region */
26static int pnp_reserve_mem[16] = {[0...15] = -1 }; /* reserve (don't use) some memory region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*
29 * option registration
30 */
31
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070032static struct pnp_option *pnp_build_option(int priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
34 struct pnp_option *option = pnp_alloc(sizeof(struct pnp_option));
35
36 /* check if pnp_alloc ran out of memory */
37 if (!option)
38 return NULL;
39
40 option->priority = priority & 0xff;
41 /* make sure the priority is valid */
42 if (option->priority > PNP_RES_PRIORITY_FUNCTIONAL)
43 option->priority = PNP_RES_PRIORITY_INVALID;
44
45 return option;
46}
47
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070048struct pnp_option *pnp_register_independent_option(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 struct pnp_option *option;
51 if (!dev)
52 return NULL;
53
54 option = pnp_build_option(PNP_RES_PRIORITY_PREFERRED);
55
56 /* this should never happen but if it does we'll try to continue */
57 if (dev->independent)
58 pnp_err("independent resource already registered");
59 dev->independent = option;
60 return option;
61}
62
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070063struct pnp_option *pnp_register_dependent_option(struct pnp_dev *dev,
64 int priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 struct pnp_option *option;
67 if (!dev)
68 return NULL;
69
70 option = pnp_build_option(priority);
71
72 if (dev->dependent) {
73 struct pnp_option *parent = dev->dependent;
74 while (parent->next)
75 parent = parent->next;
76 parent->next = option;
77 } else
78 dev->dependent = option;
79 return option;
80}
81
82int pnp_register_irq_resource(struct pnp_option *option, struct pnp_irq *data)
83{
84 struct pnp_irq *ptr;
85 if (!option)
86 return -EINVAL;
87 if (!data)
88 return -EINVAL;
89
90 ptr = option->irq;
91 while (ptr && ptr->next)
92 ptr = ptr->next;
93 if (ptr)
94 ptr->next = data;
95 else
96 option->irq = data;
97
98#ifdef CONFIG_PCI
99 {
100 int i;
101
102 for (i = 0; i < 16; i++)
103 if (test_bit(i, data->map))
David Shaohua Lic9c3e452005-04-01 00:07:31 -0500104 pcibios_penalize_isa_irq(i, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106#endif
107 return 0;
108}
109
110int pnp_register_dma_resource(struct pnp_option *option, struct pnp_dma *data)
111{
112 struct pnp_dma *ptr;
113 if (!option)
114 return -EINVAL;
115 if (!data)
116 return -EINVAL;
117
118 ptr = option->dma;
119 while (ptr && ptr->next)
120 ptr = ptr->next;
121 if (ptr)
122 ptr->next = data;
123 else
124 option->dma = data;
125
126 return 0;
127}
128
129int pnp_register_port_resource(struct pnp_option *option, struct pnp_port *data)
130{
131 struct pnp_port *ptr;
132 if (!option)
133 return -EINVAL;
134 if (!data)
135 return -EINVAL;
136
137 ptr = option->port;
138 while (ptr && ptr->next)
139 ptr = ptr->next;
140 if (ptr)
141 ptr->next = data;
142 else
143 option->port = data;
144
145 return 0;
146}
147
148int pnp_register_mem_resource(struct pnp_option *option, struct pnp_mem *data)
149{
150 struct pnp_mem *ptr;
151 if (!option)
152 return -EINVAL;
153 if (!data)
154 return -EINVAL;
155
156 ptr = option->mem;
157 while (ptr && ptr->next)
158 ptr = ptr->next;
159 if (ptr)
160 ptr->next = data;
161 else
162 option->mem = data;
163 return 0;
164}
165
166static void pnp_free_port(struct pnp_port *port)
167{
168 struct pnp_port *next;
169
170 while (port) {
171 next = port->next;
172 kfree(port);
173 port = next;
174 }
175}
176
177static void pnp_free_irq(struct pnp_irq *irq)
178{
179 struct pnp_irq *next;
180
181 while (irq) {
182 next = irq->next;
183 kfree(irq);
184 irq = next;
185 }
186}
187
188static void pnp_free_dma(struct pnp_dma *dma)
189{
190 struct pnp_dma *next;
191
192 while (dma) {
193 next = dma->next;
194 kfree(dma);
195 dma = next;
196 }
197}
198
199static void pnp_free_mem(struct pnp_mem *mem)
200{
201 struct pnp_mem *next;
202
203 while (mem) {
204 next = mem->next;
205 kfree(mem);
206 mem = next;
207 }
208}
209
210void pnp_free_option(struct pnp_option *option)
211{
212 struct pnp_option *next;
213
214 while (option) {
215 next = option->next;
216 pnp_free_port(option->port);
217 pnp_free_irq(option->irq);
218 pnp_free_dma(option->dma);
219 pnp_free_mem(option->mem);
220 kfree(option);
221 option = next;
222 }
223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225/*
226 * resource validity checking
227 */
228
229#define length(start, end) (*(end) - *(start) + 1)
230
231/* Two ranges conflict if one doesn't end before the other starts */
232#define ranged_conflict(starta, enda, startb, endb) \
233 !((*(enda) < *(startb)) || (*(endb) < *(starta)))
234
235#define cannot_compare(flags) \
236((flags) & (IORESOURCE_UNSET | IORESOURCE_DISABLED))
237
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700238int pnp_check_port(struct pnp_dev *dev, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 int tmp;
241 struct pnp_dev *tdev;
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700242 resource_size_t *port, *end, *tport, *tend;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 port = &dev->res.port_resource[idx].start;
244 end = &dev->res.port_resource[idx].end;
245
246 /* if the resource doesn't exist, don't complain about it */
247 if (cannot_compare(dev->res.port_resource[idx].flags))
248 return 1;
249
250 /* check if the resource is already in use, skip if the
251 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700252 if (!dev->active) {
253 if (__check_region(&ioport_resource, *port, length(port, end)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return 0;
255 }
256
257 /* check if the resource is reserved */
258 for (tmp = 0; tmp < 8; tmp++) {
259 int rport = pnp_reserve_io[tmp << 1];
260 int rend = pnp_reserve_io[(tmp << 1) + 1] + rport - 1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700261 if (ranged_conflict(port, end, &rport, &rend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return 0;
263 }
264
265 /* check for internal conflicts */
266 for (tmp = 0; tmp < PNP_MAX_PORT && tmp != idx; tmp++) {
267 if (dev->res.port_resource[tmp].flags & IORESOURCE_IO) {
268 tport = &dev->res.port_resource[tmp].start;
269 tend = &dev->res.port_resource[tmp].end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700270 if (ranged_conflict(port, end, tport, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return 0;
272 }
273 }
274
275 /* check for conflicts with other pnp devices */
276 pnp_for_each_dev(tdev) {
277 if (tdev == dev)
278 continue;
279 for (tmp = 0; tmp < PNP_MAX_PORT; tmp++) {
280 if (tdev->res.port_resource[tmp].flags & IORESOURCE_IO) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700281 if (cannot_compare
282 (tdev->res.port_resource[tmp].flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 continue;
284 tport = &tdev->res.port_resource[tmp].start;
285 tend = &tdev->res.port_resource[tmp].end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700286 if (ranged_conflict(port, end, tport, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return 0;
288 }
289 }
290 }
291
292 return 1;
293}
294
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700295int pnp_check_mem(struct pnp_dev *dev, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 int tmp;
298 struct pnp_dev *tdev;
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700299 resource_size_t *addr, *end, *taddr, *tend;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 addr = &dev->res.mem_resource[idx].start;
301 end = &dev->res.mem_resource[idx].end;
302
303 /* if the resource doesn't exist, don't complain about it */
304 if (cannot_compare(dev->res.mem_resource[idx].flags))
305 return 1;
306
307 /* check if the resource is already in use, skip if the
308 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700309 if (!dev->active) {
310 if (check_mem_region(*addr, length(addr, end)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return 0;
312 }
313
314 /* check if the resource is reserved */
315 for (tmp = 0; tmp < 8; tmp++) {
316 int raddr = pnp_reserve_mem[tmp << 1];
317 int rend = pnp_reserve_mem[(tmp << 1) + 1] + raddr - 1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700318 if (ranged_conflict(addr, end, &raddr, &rend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return 0;
320 }
321
322 /* check for internal conflicts */
323 for (tmp = 0; tmp < PNP_MAX_MEM && tmp != idx; tmp++) {
324 if (dev->res.mem_resource[tmp].flags & IORESOURCE_MEM) {
325 taddr = &dev->res.mem_resource[tmp].start;
326 tend = &dev->res.mem_resource[tmp].end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700327 if (ranged_conflict(addr, end, taddr, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return 0;
329 }
330 }
331
332 /* check for conflicts with other pnp devices */
333 pnp_for_each_dev(tdev) {
334 if (tdev == dev)
335 continue;
336 for (tmp = 0; tmp < PNP_MAX_MEM; tmp++) {
337 if (tdev->res.mem_resource[tmp].flags & IORESOURCE_MEM) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700338 if (cannot_compare
339 (tdev->res.mem_resource[tmp].flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 continue;
341 taddr = &tdev->res.mem_resource[tmp].start;
342 tend = &tdev->res.mem_resource[tmp].end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700343 if (ranged_conflict(addr, end, taddr, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return 0;
345 }
346 }
347 }
348
349 return 1;
350}
351
David Howells7d12e782006-10-05 14:55:46 +0100352static irqreturn_t pnp_test_handler(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 return IRQ_HANDLED;
355}
356
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700357int pnp_check_irq(struct pnp_dev *dev, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 int tmp;
360 struct pnp_dev *tdev;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700361 resource_size_t *irq = &dev->res.irq_resource[idx].start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 /* if the resource doesn't exist, don't complain about it */
364 if (cannot_compare(dev->res.irq_resource[idx].flags))
365 return 1;
366
367 /* check if the resource is valid */
368 if (*irq < 0 || *irq > 15)
369 return 0;
370
371 /* check if the resource is reserved */
372 for (tmp = 0; tmp < 16; tmp++) {
373 if (pnp_reserve_irq[tmp] == *irq)
374 return 0;
375 }
376
377 /* check for internal conflicts */
378 for (tmp = 0; tmp < PNP_MAX_IRQ && tmp != idx; tmp++) {
379 if (dev->res.irq_resource[tmp].flags & IORESOURCE_IRQ) {
380 if (dev->res.irq_resource[tmp].start == *irq)
381 return 0;
382 }
383 }
384
385#ifdef CONFIG_PCI
386 /* check if the resource is being used by a pci device */
387 {
388 struct pci_dev *pci = NULL;
389 for_each_pci_dev(pci) {
390 if (pci->irq == *irq)
391 return 0;
392 }
393 }
394#endif
395
396 /* check if the resource is already in use, skip if the
397 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700398 if (!dev->active) {
Andrew Morton0cadaf42006-07-01 04:36:37 -0700399 if (request_irq(*irq, pnp_test_handler,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700400 IRQF_DISABLED | IRQF_PROBE_SHARED, "pnp", NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return 0;
402 free_irq(*irq, NULL);
403 }
404
405 /* check for conflicts with other pnp devices */
406 pnp_for_each_dev(tdev) {
407 if (tdev == dev)
408 continue;
409 for (tmp = 0; tmp < PNP_MAX_IRQ; tmp++) {
410 if (tdev->res.irq_resource[tmp].flags & IORESOURCE_IRQ) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700411 if (cannot_compare
412 (tdev->res.irq_resource[tmp].flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 continue;
414 if ((tdev->res.irq_resource[tmp].start == *irq))
415 return 0;
416 }
417 }
418 }
419
420 return 1;
421}
422
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700423int pnp_check_dma(struct pnp_dev *dev, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425#ifndef CONFIG_IA64
426 int tmp;
427 struct pnp_dev *tdev;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700428 resource_size_t *dma = &dev->res.dma_resource[idx].start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 /* if the resource doesn't exist, don't complain about it */
431 if (cannot_compare(dev->res.dma_resource[idx].flags))
432 return 1;
433
434 /* check if the resource is valid */
435 if (*dma < 0 || *dma == 4 || *dma > 7)
436 return 0;
437
438 /* check if the resource is reserved */
439 for (tmp = 0; tmp < 8; tmp++) {
440 if (pnp_reserve_dma[tmp] == *dma)
441 return 0;
442 }
443
444 /* check for internal conflicts */
445 for (tmp = 0; tmp < PNP_MAX_DMA && tmp != idx; tmp++) {
446 if (dev->res.dma_resource[tmp].flags & IORESOURCE_DMA) {
447 if (dev->res.dma_resource[tmp].start == *dma)
448 return 0;
449 }
450 }
451
452 /* check if the resource is already in use, skip if the
453 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700454 if (!dev->active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (request_dma(*dma, "pnp"))
456 return 0;
457 free_dma(*dma);
458 }
459
460 /* check for conflicts with other pnp devices */
461 pnp_for_each_dev(tdev) {
462 if (tdev == dev)
463 continue;
464 for (tmp = 0; tmp < PNP_MAX_DMA; tmp++) {
465 if (tdev->res.dma_resource[tmp].flags & IORESOURCE_DMA) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700466 if (cannot_compare
467 (tdev->res.dma_resource[tmp].flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 continue;
469 if ((tdev->res.dma_resource[tmp].start == *dma))
470 return 0;
471 }
472 }
473 }
474
475 return 1;
476#else
477 /* IA64 hasn't legacy DMA */
478 return 0;
479#endif
480}
481
Adrian Bunkb449f632005-11-07 01:01:48 -0800482#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483EXPORT_SYMBOL(pnp_register_dependent_option);
484EXPORT_SYMBOL(pnp_register_independent_option);
485EXPORT_SYMBOL(pnp_register_irq_resource);
486EXPORT_SYMBOL(pnp_register_dma_resource);
487EXPORT_SYMBOL(pnp_register_port_resource);
488EXPORT_SYMBOL(pnp_register_mem_resource);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700489#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491/* format is: pnp_reserve_irq=irq1[,irq2] .... */
492
493static int __init pnp_setup_reserve_irq(char *str)
494{
495 int i;
496
497 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700498 if (get_option(&str, &pnp_reserve_irq[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 break;
500 return 1;
501}
502
503__setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
504
505/* format is: pnp_reserve_dma=dma1[,dma2] .... */
506
507static int __init pnp_setup_reserve_dma(char *str)
508{
509 int i;
510
511 for (i = 0; i < 8; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700512 if (get_option(&str, &pnp_reserve_dma[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 break;
514 return 1;
515}
516
517__setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
518
519/* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
520
521static int __init pnp_setup_reserve_io(char *str)
522{
523 int i;
524
525 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700526 if (get_option(&str, &pnp_reserve_io[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 break;
528 return 1;
529}
530
531__setup("pnp_reserve_io=", pnp_setup_reserve_io);
532
533/* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
534
535static int __init pnp_setup_reserve_mem(char *str)
536{
537 int i;
538
539 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700540 if (get_option(&str, &pnp_reserve_mem[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 break;
542 return 1;
543}
544
545__setup("pnp_reserve_mem=", pnp_setup_reserve_mem);