]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/pnp/resource.c
Merge branch 'v2.6.34-rc2' into drm-linus
[mirror_ubuntu-bionic-kernel.git] / drivers / pnp / resource.c
1 /*
2 * resource.c - Contains functions for registering and analyzing resource information
3 *
4 * based on isapnp.c resource management (c) Jaroslav Kysela <perex@perex.cz>
5 * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
6 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
7 * Bjorn Helgaas <bjorn.helgaas@hp.com>
8 */
9
10 #include <linux/module.h>
11 #include <linux/errno.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <asm/io.h>
15 #include <asm/dma.h>
16 #include <asm/irq.h>
17 #include <linux/pci.h>
18 #include <linux/ioport.h>
19 #include <linux/init.h>
20
21 #include <linux/pnp.h>
22 #include "base.h"
23
24 static int pnp_reserve_irq[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some IRQ */
25 static int pnp_reserve_dma[8] = {[0 ... 7] = -1 }; /* reserve (don't use) some DMA */
26 static int pnp_reserve_io[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some I/O region */
27 static int pnp_reserve_mem[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some memory region */
28
29 /*
30 * option registration
31 */
32
33 struct pnp_option *pnp_build_option(struct pnp_dev *dev, unsigned long type,
34 unsigned int option_flags)
35 {
36 struct pnp_option *option;
37
38 option = kzalloc(sizeof(struct pnp_option), GFP_KERNEL);
39 if (!option)
40 return NULL;
41
42 option->flags = option_flags;
43 option->type = type;
44
45 list_add_tail(&option->list, &dev->options);
46 return option;
47 }
48
49 int pnp_register_irq_resource(struct pnp_dev *dev, unsigned int option_flags,
50 pnp_irq_mask_t *map, unsigned char flags)
51 {
52 struct pnp_option *option;
53 struct pnp_irq *irq;
54
55 option = pnp_build_option(dev, IORESOURCE_IRQ, option_flags);
56 if (!option)
57 return -ENOMEM;
58
59 irq = &option->u.irq;
60 irq->map = *map;
61 irq->flags = flags;
62
63 #ifdef CONFIG_PCI
64 {
65 int i;
66
67 for (i = 0; i < 16; i++)
68 if (test_bit(i, irq->map.bits))
69 pcibios_penalize_isa_irq(i, 0);
70 }
71 #endif
72
73 dbg_pnp_show_option(dev, option);
74 return 0;
75 }
76
77 int pnp_register_dma_resource(struct pnp_dev *dev, unsigned int option_flags,
78 unsigned char map, unsigned char flags)
79 {
80 struct pnp_option *option;
81 struct pnp_dma *dma;
82
83 option = pnp_build_option(dev, IORESOURCE_DMA, option_flags);
84 if (!option)
85 return -ENOMEM;
86
87 dma = &option->u.dma;
88 dma->map = map;
89 dma->flags = flags;
90
91 dbg_pnp_show_option(dev, option);
92 return 0;
93 }
94
95 int pnp_register_port_resource(struct pnp_dev *dev, unsigned int option_flags,
96 resource_size_t min, resource_size_t max,
97 resource_size_t align, resource_size_t size,
98 unsigned char flags)
99 {
100 struct pnp_option *option;
101 struct pnp_port *port;
102
103 option = pnp_build_option(dev, IORESOURCE_IO, option_flags);
104 if (!option)
105 return -ENOMEM;
106
107 port = &option->u.port;
108 port->min = min;
109 port->max = max;
110 port->align = align;
111 port->size = size;
112 port->flags = flags;
113
114 dbg_pnp_show_option(dev, option);
115 return 0;
116 }
117
118 int pnp_register_mem_resource(struct pnp_dev *dev, unsigned int option_flags,
119 resource_size_t min, resource_size_t max,
120 resource_size_t align, resource_size_t size,
121 unsigned char flags)
122 {
123 struct pnp_option *option;
124 struct pnp_mem *mem;
125
126 option = pnp_build_option(dev, IORESOURCE_MEM, option_flags);
127 if (!option)
128 return -ENOMEM;
129
130 mem = &option->u.mem;
131 mem->min = min;
132 mem->max = max;
133 mem->align = align;
134 mem->size = size;
135 mem->flags = flags;
136
137 dbg_pnp_show_option(dev, option);
138 return 0;
139 }
140
141 void pnp_free_options(struct pnp_dev *dev)
142 {
143 struct pnp_option *option, *tmp;
144
145 list_for_each_entry_safe(option, tmp, &dev->options, list) {
146 list_del(&option->list);
147 kfree(option);
148 }
149 }
150
151 /*
152 * resource validity checking
153 */
154
155 #define length(start, end) (*(end) - *(start) + 1)
156
157 /* Two ranges conflict if one doesn't end before the other starts */
158 #define ranged_conflict(starta, enda, startb, endb) \
159 !((*(enda) < *(startb)) || (*(endb) < *(starta)))
160
161 #define cannot_compare(flags) \
162 ((flags) & IORESOURCE_DISABLED)
163
164 int pnp_check_port(struct pnp_dev *dev, struct resource *res)
165 {
166 int i;
167 struct pnp_dev *tdev;
168 struct resource *tres;
169 resource_size_t *port, *end, *tport, *tend;
170
171 port = &res->start;
172 end = &res->end;
173
174 /* if the resource doesn't exist, don't complain about it */
175 if (cannot_compare(res->flags))
176 return 1;
177
178 /* check if the resource is already in use, skip if the
179 * device is active because it itself may be in use */
180 if (!dev->active) {
181 if (__check_region(&ioport_resource, *port, length(port, end)))
182 return 0;
183 }
184
185 /* check if the resource is reserved */
186 for (i = 0; i < 8; i++) {
187 int rport = pnp_reserve_io[i << 1];
188 int rend = pnp_reserve_io[(i << 1) + 1] + rport - 1;
189 if (ranged_conflict(port, end, &rport, &rend))
190 return 0;
191 }
192
193 /* check for internal conflicts */
194 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IO, i)); i++) {
195 if (tres != res && tres->flags & IORESOURCE_IO) {
196 tport = &tres->start;
197 tend = &tres->end;
198 if (ranged_conflict(port, end, tport, tend))
199 return 0;
200 }
201 }
202
203 /* check for conflicts with other pnp devices */
204 pnp_for_each_dev(tdev) {
205 if (tdev == dev)
206 continue;
207 for (i = 0;
208 (tres = pnp_get_resource(tdev, IORESOURCE_IO, i));
209 i++) {
210 if (tres->flags & IORESOURCE_IO) {
211 if (cannot_compare(tres->flags))
212 continue;
213 tport = &tres->start;
214 tend = &tres->end;
215 if (ranged_conflict(port, end, tport, tend))
216 return 0;
217 }
218 }
219 }
220
221 return 1;
222 }
223
224 int pnp_check_mem(struct pnp_dev *dev, struct resource *res)
225 {
226 int i;
227 struct pnp_dev *tdev;
228 struct resource *tres;
229 resource_size_t *addr, *end, *taddr, *tend;
230
231 addr = &res->start;
232 end = &res->end;
233
234 /* if the resource doesn't exist, don't complain about it */
235 if (cannot_compare(res->flags))
236 return 1;
237
238 /* check if the resource is already in use, skip if the
239 * device is active because it itself may be in use */
240 if (!dev->active) {
241 if (check_mem_region(*addr, length(addr, end)))
242 return 0;
243 }
244
245 /* check if the resource is reserved */
246 for (i = 0; i < 8; i++) {
247 int raddr = pnp_reserve_mem[i << 1];
248 int rend = pnp_reserve_mem[(i << 1) + 1] + raddr - 1;
249 if (ranged_conflict(addr, end, &raddr, &rend))
250 return 0;
251 }
252
253 /* check for internal conflicts */
254 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_MEM, i)); i++) {
255 if (tres != res && tres->flags & IORESOURCE_MEM) {
256 taddr = &tres->start;
257 tend = &tres->end;
258 if (ranged_conflict(addr, end, taddr, tend))
259 return 0;
260 }
261 }
262
263 /* check for conflicts with other pnp devices */
264 pnp_for_each_dev(tdev) {
265 if (tdev == dev)
266 continue;
267 for (i = 0;
268 (tres = pnp_get_resource(tdev, IORESOURCE_MEM, i));
269 i++) {
270 if (tres->flags & IORESOURCE_MEM) {
271 if (cannot_compare(tres->flags))
272 continue;
273 taddr = &tres->start;
274 tend = &tres->end;
275 if (ranged_conflict(addr, end, taddr, tend))
276 return 0;
277 }
278 }
279 }
280
281 return 1;
282 }
283
284 static irqreturn_t pnp_test_handler(int irq, void *dev_id)
285 {
286 return IRQ_HANDLED;
287 }
288
289 #ifdef CONFIG_PCI
290 static int pci_dev_uses_irq(struct pnp_dev *pnp, struct pci_dev *pci,
291 unsigned int irq)
292 {
293 u32 class;
294 u8 progif;
295
296 if (pci->irq == irq) {
297 pnp_dbg(&pnp->dev, " device %s using irq %d\n",
298 pci_name(pci), irq);
299 return 1;
300 }
301
302 /*
303 * See pci_setup_device() and ata_pci_sff_activate_host() for
304 * similar IDE legacy detection.
305 */
306 pci_read_config_dword(pci, PCI_CLASS_REVISION, &class);
307 class >>= 8; /* discard revision ID */
308 progif = class & 0xff;
309 class >>= 8;
310
311 if (class == PCI_CLASS_STORAGE_IDE) {
312 /*
313 * Unless both channels are native-PCI mode only,
314 * treat the compatibility IRQs as busy.
315 */
316 if ((progif & 0x5) != 0x5)
317 if (pci_get_legacy_ide_irq(pci, 0) == irq ||
318 pci_get_legacy_ide_irq(pci, 1) == irq) {
319 pnp_dbg(&pnp->dev, " legacy IDE device %s "
320 "using irq %d\n", pci_name(pci), irq);
321 return 1;
322 }
323 }
324
325 return 0;
326 }
327 #endif
328
329 static int pci_uses_irq(struct pnp_dev *pnp, unsigned int irq)
330 {
331 #ifdef CONFIG_PCI
332 struct pci_dev *pci = NULL;
333
334 for_each_pci_dev(pci) {
335 if (pci_dev_uses_irq(pnp, pci, irq)) {
336 pci_dev_put(pci);
337 return 1;
338 }
339 }
340 #endif
341 return 0;
342 }
343
344 int pnp_check_irq(struct pnp_dev *dev, struct resource *res)
345 {
346 int i;
347 struct pnp_dev *tdev;
348 struct resource *tres;
349 resource_size_t *irq;
350
351 irq = &res->start;
352
353 /* if the resource doesn't exist, don't complain about it */
354 if (cannot_compare(res->flags))
355 return 1;
356
357 /* check if the resource is valid */
358 if (*irq < 0 || *irq > 15)
359 return 0;
360
361 /* check if the resource is reserved */
362 for (i = 0; i < 16; i++) {
363 if (pnp_reserve_irq[i] == *irq)
364 return 0;
365 }
366
367 /* check for internal conflicts */
368 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IRQ, i)); i++) {
369 if (tres != res && tres->flags & IORESOURCE_IRQ) {
370 if (tres->start == *irq)
371 return 0;
372 }
373 }
374
375 /* check if the resource is being used by a pci device */
376 if (pci_uses_irq(dev, *irq))
377 return 0;
378
379 /* check if the resource is already in use, skip if the
380 * device is active because it itself may be in use */
381 if (!dev->active) {
382 if (request_irq(*irq, pnp_test_handler,
383 IRQF_DISABLED | IRQF_PROBE_SHARED, "pnp", NULL))
384 return 0;
385 free_irq(*irq, NULL);
386 }
387
388 /* check for conflicts with other pnp devices */
389 pnp_for_each_dev(tdev) {
390 if (tdev == dev)
391 continue;
392 for (i = 0;
393 (tres = pnp_get_resource(tdev, IORESOURCE_IRQ, i));
394 i++) {
395 if (tres->flags & IORESOURCE_IRQ) {
396 if (cannot_compare(tres->flags))
397 continue;
398 if (tres->start == *irq)
399 return 0;
400 }
401 }
402 }
403
404 return 1;
405 }
406
407 int pnp_check_dma(struct pnp_dev *dev, struct resource *res)
408 {
409 #ifndef CONFIG_IA64
410 int i;
411 struct pnp_dev *tdev;
412 struct resource *tres;
413 resource_size_t *dma;
414
415 dma = &res->start;
416
417 /* if the resource doesn't exist, don't complain about it */
418 if (cannot_compare(res->flags))
419 return 1;
420
421 /* check if the resource is valid */
422 if (*dma < 0 || *dma == 4 || *dma > 7)
423 return 0;
424
425 /* check if the resource is reserved */
426 for (i = 0; i < 8; i++) {
427 if (pnp_reserve_dma[i] == *dma)
428 return 0;
429 }
430
431 /* check for internal conflicts */
432 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_DMA, i)); i++) {
433 if (tres != res && tres->flags & IORESOURCE_DMA) {
434 if (tres->start == *dma)
435 return 0;
436 }
437 }
438
439 /* check if the resource is already in use, skip if the
440 * device is active because it itself may be in use */
441 if (!dev->active) {
442 if (request_dma(*dma, "pnp"))
443 return 0;
444 free_dma(*dma);
445 }
446
447 /* check for conflicts with other pnp devices */
448 pnp_for_each_dev(tdev) {
449 if (tdev == dev)
450 continue;
451 for (i = 0;
452 (tres = pnp_get_resource(tdev, IORESOURCE_DMA, i));
453 i++) {
454 if (tres->flags & IORESOURCE_DMA) {
455 if (cannot_compare(tres->flags))
456 continue;
457 if (tres->start == *dma)
458 return 0;
459 }
460 }
461 }
462
463 return 1;
464 #else
465 /* IA64 does not have legacy DMA */
466 return 0;
467 #endif
468 }
469
470 unsigned long pnp_resource_type(struct resource *res)
471 {
472 return res->flags & (IORESOURCE_IO | IORESOURCE_MEM |
473 IORESOURCE_IRQ | IORESOURCE_DMA |
474 IORESOURCE_BUS);
475 }
476
477 struct resource *pnp_get_resource(struct pnp_dev *dev,
478 unsigned long type, unsigned int num)
479 {
480 struct pnp_resource *pnp_res;
481 struct resource *res;
482
483 list_for_each_entry(pnp_res, &dev->resources, list) {
484 res = &pnp_res->res;
485 if (pnp_resource_type(res) == type && num-- == 0)
486 return res;
487 }
488 return NULL;
489 }
490 EXPORT_SYMBOL(pnp_get_resource);
491
492 static struct pnp_resource *pnp_new_resource(struct pnp_dev *dev)
493 {
494 struct pnp_resource *pnp_res;
495
496 pnp_res = kzalloc(sizeof(struct pnp_resource), GFP_KERNEL);
497 if (!pnp_res)
498 return NULL;
499
500 list_add_tail(&pnp_res->list, &dev->resources);
501 return pnp_res;
502 }
503
504 struct pnp_resource *pnp_add_irq_resource(struct pnp_dev *dev, int irq,
505 int flags)
506 {
507 struct pnp_resource *pnp_res;
508 struct resource *res;
509
510 pnp_res = pnp_new_resource(dev);
511 if (!pnp_res) {
512 dev_err(&dev->dev, "can't add resource for IRQ %d\n", irq);
513 return NULL;
514 }
515
516 res = &pnp_res->res;
517 res->flags = IORESOURCE_IRQ | flags;
518 res->start = irq;
519 res->end = irq;
520
521 pnp_dbg(&dev->dev, " add %pr\n", res);
522 return pnp_res;
523 }
524
525 struct pnp_resource *pnp_add_dma_resource(struct pnp_dev *dev, int dma,
526 int flags)
527 {
528 struct pnp_resource *pnp_res;
529 struct resource *res;
530
531 pnp_res = pnp_new_resource(dev);
532 if (!pnp_res) {
533 dev_err(&dev->dev, "can't add resource for DMA %d\n", dma);
534 return NULL;
535 }
536
537 res = &pnp_res->res;
538 res->flags = IORESOURCE_DMA | flags;
539 res->start = dma;
540 res->end = dma;
541
542 pnp_dbg(&dev->dev, " add %pr\n", res);
543 return pnp_res;
544 }
545
546 struct pnp_resource *pnp_add_io_resource(struct pnp_dev *dev,
547 resource_size_t start,
548 resource_size_t end, int flags)
549 {
550 struct pnp_resource *pnp_res;
551 struct resource *res;
552
553 pnp_res = pnp_new_resource(dev);
554 if (!pnp_res) {
555 dev_err(&dev->dev, "can't add resource for IO %#llx-%#llx\n",
556 (unsigned long long) start,
557 (unsigned long long) end);
558 return NULL;
559 }
560
561 res = &pnp_res->res;
562 res->flags = IORESOURCE_IO | flags;
563 res->start = start;
564 res->end = end;
565
566 pnp_dbg(&dev->dev, " add %pr\n", res);
567 return pnp_res;
568 }
569
570 struct pnp_resource *pnp_add_mem_resource(struct pnp_dev *dev,
571 resource_size_t start,
572 resource_size_t end, int flags)
573 {
574 struct pnp_resource *pnp_res;
575 struct resource *res;
576
577 pnp_res = pnp_new_resource(dev);
578 if (!pnp_res) {
579 dev_err(&dev->dev, "can't add resource for MEM %#llx-%#llx\n",
580 (unsigned long long) start,
581 (unsigned long long) end);
582 return NULL;
583 }
584
585 res = &pnp_res->res;
586 res->flags = IORESOURCE_MEM | flags;
587 res->start = start;
588 res->end = end;
589
590 pnp_dbg(&dev->dev, " add %pr\n", res);
591 return pnp_res;
592 }
593
594 struct pnp_resource *pnp_add_bus_resource(struct pnp_dev *dev,
595 resource_size_t start,
596 resource_size_t end)
597 {
598 struct pnp_resource *pnp_res;
599 struct resource *res;
600
601 pnp_res = pnp_new_resource(dev);
602 if (!pnp_res) {
603 dev_err(&dev->dev, "can't add resource for BUS %#llx-%#llx\n",
604 (unsigned long long) start,
605 (unsigned long long) end);
606 return NULL;
607 }
608
609 res = &pnp_res->res;
610 res->flags = IORESOURCE_BUS;
611 res->start = start;
612 res->end = end;
613
614 pnp_dbg(&dev->dev, " add %pr\n", res);
615 return pnp_res;
616 }
617
618 /*
619 * Determine whether the specified resource is a possible configuration
620 * for this device.
621 */
622 int pnp_possible_config(struct pnp_dev *dev, int type, resource_size_t start,
623 resource_size_t size)
624 {
625 struct pnp_option *option;
626 struct pnp_port *port;
627 struct pnp_mem *mem;
628 struct pnp_irq *irq;
629 struct pnp_dma *dma;
630
631 list_for_each_entry(option, &dev->options, list) {
632 if (option->type != type)
633 continue;
634
635 switch (option->type) {
636 case IORESOURCE_IO:
637 port = &option->u.port;
638 if (port->min == start && port->size == size)
639 return 1;
640 break;
641 case IORESOURCE_MEM:
642 mem = &option->u.mem;
643 if (mem->min == start && mem->size == size)
644 return 1;
645 break;
646 case IORESOURCE_IRQ:
647 irq = &option->u.irq;
648 if (start < PNP_IRQ_NR &&
649 test_bit(start, irq->map.bits))
650 return 1;
651 break;
652 case IORESOURCE_DMA:
653 dma = &option->u.dma;
654 if (dma->map & (1 << start))
655 return 1;
656 break;
657 }
658 }
659
660 return 0;
661 }
662 EXPORT_SYMBOL(pnp_possible_config);
663
664 int pnp_range_reserved(resource_size_t start, resource_size_t end)
665 {
666 struct pnp_dev *dev;
667 struct pnp_resource *pnp_res;
668 resource_size_t *dev_start, *dev_end;
669
670 pnp_for_each_dev(dev) {
671 list_for_each_entry(pnp_res, &dev->resources, list) {
672 dev_start = &pnp_res->res.start;
673 dev_end = &pnp_res->res.end;
674 if (ranged_conflict(&start, &end, dev_start, dev_end))
675 return 1;
676 }
677 }
678 return 0;
679 }
680 EXPORT_SYMBOL(pnp_range_reserved);
681
682 /* format is: pnp_reserve_irq=irq1[,irq2] .... */
683 static int __init pnp_setup_reserve_irq(char *str)
684 {
685 int i;
686
687 for (i = 0; i < 16; i++)
688 if (get_option(&str, &pnp_reserve_irq[i]) != 2)
689 break;
690 return 1;
691 }
692
693 __setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
694
695 /* format is: pnp_reserve_dma=dma1[,dma2] .... */
696 static int __init pnp_setup_reserve_dma(char *str)
697 {
698 int i;
699
700 for (i = 0; i < 8; i++)
701 if (get_option(&str, &pnp_reserve_dma[i]) != 2)
702 break;
703 return 1;
704 }
705
706 __setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
707
708 /* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
709 static int __init pnp_setup_reserve_io(char *str)
710 {
711 int i;
712
713 for (i = 0; i < 16; i++)
714 if (get_option(&str, &pnp_reserve_io[i]) != 2)
715 break;
716 return 1;
717 }
718
719 __setup("pnp_reserve_io=", pnp_setup_reserve_io);
720
721 /* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
722 static int __init pnp_setup_reserve_mem(char *str)
723 {
724 int i;
725
726 for (i = 0; i < 16; i++)
727 if (get_option(&str, &pnp_reserve_mem[i]) != 2)
728 break;
729 return 1;
730 }
731
732 __setup("pnp_reserve_mem=", pnp_setup_reserve_mem);