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