blob: 3e6db1c1dc295386f01c37bf23679eba86e1cbd7 [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 Helgaas11439a62010-05-03 10:47:21 -0600214 if (tres->flags & IORESOURCE_WINDOW)
215 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600216 tport = &tres->start;
217 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700218 if (ranged_conflict(port, end, tport, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return 0;
220 }
221 }
222 }
223
224 return 1;
225}
226
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600227int pnp_check_mem(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600229 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600231 struct resource *tres;
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700232 resource_size_t *addr, *end, *taddr, *tend;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700233
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600234 addr = &res->start;
235 end = &res->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600238 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return 1;
240
241 /* check if the resource is already in use, skip if the
242 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700243 if (!dev->active) {
244 if (check_mem_region(*addr, length(addr, end)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return 0;
246 }
247
248 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600249 for (i = 0; i < 8; i++) {
250 int raddr = pnp_reserve_mem[i << 1];
251 int rend = pnp_reserve_mem[(i << 1) + 1] + raddr - 1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700252 if (ranged_conflict(addr, end, &raddr, &rend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return 0;
254 }
255
256 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600257 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_MEM, i)); i++) {
258 if (tres != res && tres->flags & IORESOURCE_MEM) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600259 taddr = &tres->start;
260 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700261 if (ranged_conflict(addr, end, taddr, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return 0;
263 }
264 }
265
266 /* check for conflicts with other pnp devices */
267 pnp_for_each_dev(tdev) {
268 if (tdev == dev)
269 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600270 for (i = 0;
271 (tres = pnp_get_resource(tdev, IORESOURCE_MEM, i));
272 i++) {
273 if (tres->flags & IORESOURCE_MEM) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600274 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 continue;
Bjorn Helgaas11439a62010-05-03 10:47:21 -0600276 if (tres->flags & IORESOURCE_WINDOW)
277 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600278 taddr = &tres->start;
279 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700280 if (ranged_conflict(addr, end, taddr, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return 0;
282 }
283 }
284 }
285
286 return 1;
287}
288
David Howells7d12e782006-10-05 14:55:46 +0100289static irqreturn_t pnp_test_handler(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 return IRQ_HANDLED;
292}
293
Bjorn Helgaas84684c72008-06-27 16:57:18 -0600294#ifdef CONFIG_PCI
295static int pci_dev_uses_irq(struct pnp_dev *pnp, struct pci_dev *pci,
296 unsigned int irq)
297{
298 u32 class;
299 u8 progif;
300
301 if (pci->irq == irq) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600302 pnp_dbg(&pnp->dev, " device %s using irq %d\n",
Bjorn Helgaas84684c72008-06-27 16:57:18 -0600303 pci_name(pci), irq);
304 return 1;
305 }
306
307 /*
308 * See pci_setup_device() and ata_pci_sff_activate_host() for
309 * similar IDE legacy detection.
310 */
311 pci_read_config_dword(pci, PCI_CLASS_REVISION, &class);
312 class >>= 8; /* discard revision ID */
313 progif = class & 0xff;
314 class >>= 8;
315
316 if (class == PCI_CLASS_STORAGE_IDE) {
317 /*
318 * Unless both channels are native-PCI mode only,
319 * treat the compatibility IRQs as busy.
320 */
321 if ((progif & 0x5) != 0x5)
322 if (pci_get_legacy_ide_irq(pci, 0) == irq ||
323 pci_get_legacy_ide_irq(pci, 1) == irq) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600324 pnp_dbg(&pnp->dev, " legacy IDE device %s "
Bjorn Helgaas84684c72008-06-27 16:57:18 -0600325 "using irq %d\n", pci_name(pci), irq);
326 return 1;
327 }
328 }
329
330 return 0;
331}
332#endif
333
334static int pci_uses_irq(struct pnp_dev *pnp, unsigned int irq)
335{
336#ifdef CONFIG_PCI
337 struct pci_dev *pci = NULL;
338
339 for_each_pci_dev(pci) {
340 if (pci_dev_uses_irq(pnp, pci, irq)) {
341 pci_dev_put(pci);
342 return 1;
343 }
344 }
345#endif
346 return 0;
347}
348
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600349int pnp_check_irq(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600351 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600353 struct resource *tres;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600354 resource_size_t *irq;
355
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600356 irq = &res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600359 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return 1;
361
362 /* check if the resource is valid */
363 if (*irq < 0 || *irq > 15)
364 return 0;
365
366 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600367 for (i = 0; i < 16; i++) {
368 if (pnp_reserve_irq[i] == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return 0;
370 }
371
372 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600373 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IRQ, i)); i++) {
374 if (tres != res && tres->flags & IORESOURCE_IRQ) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600375 if (tres->start == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return 0;
377 }
378 }
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /* check if the resource is being used by a pci device */
Bjorn Helgaas84684c72008-06-27 16:57:18 -0600381 if (pci_uses_irq(dev, *irq))
382 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 /* check if the resource is already in use, skip if the
385 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700386 if (!dev->active) {
Andrew Morton0cadaf42006-07-01 04:36:37 -0700387 if (request_irq(*irq, pnp_test_handler,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700388 IRQF_DISABLED | IRQF_PROBE_SHARED, "pnp", NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return 0;
390 free_irq(*irq, NULL);
391 }
392
393 /* check for conflicts with other pnp devices */
394 pnp_for_each_dev(tdev) {
395 if (tdev == dev)
396 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600397 for (i = 0;
398 (tres = pnp_get_resource(tdev, IORESOURCE_IRQ, i));
399 i++) {
400 if (tres->flags & IORESOURCE_IRQ) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600401 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600403 if (tres->start == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return 0;
405 }
406 }
407 }
408
409 return 1;
410}
411
David Rientjes586f83e2011-03-22 16:34:56 -0700412#ifdef CONFIG_ISA_DMA_API
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600413int pnp_check_dma(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600415 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600417 struct resource *tres;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600418 resource_size_t *dma;
419
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600420 dma = &res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600423 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return 1;
425
426 /* check if the resource is valid */
427 if (*dma < 0 || *dma == 4 || *dma > 7)
428 return 0;
429
430 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600431 for (i = 0; i < 8; i++) {
432 if (pnp_reserve_dma[i] == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return 0;
434 }
435
436 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600437 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_DMA, i)); i++) {
438 if (tres != res && tres->flags & IORESOURCE_DMA) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600439 if (tres->start == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return 0;
441 }
442 }
443
444 /* check if the resource is already in use, skip if the
445 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700446 if (!dev->active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (request_dma(*dma, "pnp"))
448 return 0;
449 free_dma(*dma);
450 }
451
452 /* check for conflicts with other pnp devices */
453 pnp_for_each_dev(tdev) {
454 if (tdev == dev)
455 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600456 for (i = 0;
457 (tres = pnp_get_resource(tdev, IORESOURCE_DMA, i));
458 i++) {
459 if (tres->flags & IORESOURCE_DMA) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600460 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600462 if (tres->start == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return 0;
464 }
465 }
466 }
467
468 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
David Rientjes586f83e2011-03-22 16:34:56 -0700470#endif /* CONFIG_ISA_DMA_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Rene Hermanb563cf52008-10-15 22:03:58 -0700472unsigned long pnp_resource_type(struct resource *res)
Bjorn Helgaas940e98d2008-06-27 16:56:54 -0600473{
474 return res->flags & (IORESOURCE_IO | IORESOURCE_MEM |
Bjorn Helgaas7e0e9c02010-03-05 10:47:57 -0700475 IORESOURCE_IRQ | IORESOURCE_DMA |
476 IORESOURCE_BUS);
Bjorn Helgaas940e98d2008-06-27 16:56:54 -0600477}
478
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600479struct resource *pnp_get_resource(struct pnp_dev *dev,
Rene Hermanb563cf52008-10-15 22:03:58 -0700480 unsigned long type, unsigned int num)
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600481{
482 struct pnp_resource *pnp_res;
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600483 struct resource *res;
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600484
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600485 list_for_each_entry(pnp_res, &dev->resources, list) {
486 res = &pnp_res->res;
487 if (pnp_resource_type(res) == type && num-- == 0)
488 return res;
489 }
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600490 return NULL;
491}
Bjorn Helgaasb90eca02008-04-28 16:34:14 -0600492EXPORT_SYMBOL(pnp_get_resource);
493
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600494static struct pnp_resource *pnp_new_resource(struct pnp_dev *dev)
Bjorn Helgaasa50b6d72008-04-28 16:34:33 -0600495{
496 struct pnp_resource *pnp_res;
Bjorn Helgaasa50b6d72008-04-28 16:34:33 -0600497
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600498 pnp_res = kzalloc(sizeof(struct pnp_resource), GFP_KERNEL);
499 if (!pnp_res)
500 return NULL;
501
502 list_add_tail(&pnp_res->list, &dev->resources);
503 return pnp_res;
Bjorn Helgaasa50b6d72008-04-28 16:34:33 -0600504}
505
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100506struct pnp_resource *pnp_add_resource(struct pnp_dev *dev,
507 struct resource *res)
508{
509 struct pnp_resource *pnp_res;
510
511 pnp_res = pnp_new_resource(dev);
512 if (!pnp_res) {
513 dev_err(&dev->dev, "can't add resource %pR\n", res);
514 return NULL;
515 }
516
517 pnp_res->res = *res;
518 dev_dbg(&dev->dev, "%pR\n", res);
519 return pnp_res;
520}
521
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600522struct pnp_resource *pnp_add_irq_resource(struct pnp_dev *dev, int irq,
523 int flags)
524{
525 struct pnp_resource *pnp_res;
526 struct resource *res;
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600527
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600528 pnp_res = pnp_new_resource(dev);
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600529 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600530 dev_err(&dev->dev, "can't add resource for IRQ %d\n", irq);
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600531 return NULL;
532 }
533
534 res = &pnp_res->res;
535 res->flags = IORESOURCE_IRQ | flags;
536 res->start = irq;
537 res->end = irq;
538
Bjorn Helgaasc1f3f282010-09-29 12:24:23 -0600539 dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res);
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600540 return pnp_res;
541}
542
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600543struct pnp_resource *pnp_add_dma_resource(struct pnp_dev *dev, int dma,
544 int flags)
545{
546 struct pnp_resource *pnp_res;
547 struct resource *res;
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600548
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600549 pnp_res = pnp_new_resource(dev);
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600550 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600551 dev_err(&dev->dev, "can't add resource for DMA %d\n", dma);
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600552 return NULL;
553 }
554
555 res = &pnp_res->res;
556 res->flags = IORESOURCE_DMA | flags;
557 res->start = dma;
558 res->end = dma;
559
Bjorn Helgaasc1f3f282010-09-29 12:24:23 -0600560 dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res);
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600561 return pnp_res;
562}
563
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600564struct pnp_resource *pnp_add_io_resource(struct pnp_dev *dev,
565 resource_size_t start,
566 resource_size_t end, int flags)
567{
568 struct pnp_resource *pnp_res;
569 struct resource *res;
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600570
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600571 pnp_res = pnp_new_resource(dev);
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600572 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600573 dev_err(&dev->dev, "can't add resource for IO %#llx-%#llx\n",
574 (unsigned long long) start,
575 (unsigned long long) end);
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600576 return NULL;
577 }
578
579 res = &pnp_res->res;
580 res->flags = IORESOURCE_IO | flags;
581 res->start = start;
582 res->end = end;
583
Bjorn Helgaasc1f3f282010-09-29 12:24:23 -0600584 dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res);
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600585 return pnp_res;
586}
587
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600588struct pnp_resource *pnp_add_mem_resource(struct pnp_dev *dev,
589 resource_size_t start,
590 resource_size_t end, int flags)
591{
592 struct pnp_resource *pnp_res;
593 struct resource *res;
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600594
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600595 pnp_res = pnp_new_resource(dev);
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600596 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600597 dev_err(&dev->dev, "can't add resource for MEM %#llx-%#llx\n",
598 (unsigned long long) start,
599 (unsigned long long) end);
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600600 return NULL;
601 }
602
603 res = &pnp_res->res;
604 res->flags = IORESOURCE_MEM | flags;
605 res->start = start;
606 res->end = end;
607
Bjorn Helgaasc1f3f282010-09-29 12:24:23 -0600608 dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res);
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600609 return pnp_res;
610}
611
Bjorn Helgaas7e0e9c02010-03-05 10:47:57 -0700612struct pnp_resource *pnp_add_bus_resource(struct pnp_dev *dev,
613 resource_size_t start,
614 resource_size_t end)
615{
616 struct pnp_resource *pnp_res;
617 struct resource *res;
618
619 pnp_res = pnp_new_resource(dev);
620 if (!pnp_res) {
621 dev_err(&dev->dev, "can't add resource for BUS %#llx-%#llx\n",
622 (unsigned long long) start,
623 (unsigned long long) end);
624 return NULL;
625 }
626
627 res = &pnp_res->res;
628 res->flags = IORESOURCE_BUS;
629 res->start = start;
630 res->end = end;
631
Bjorn Helgaasc1f3f282010-09-29 12:24:23 -0600632 dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res);
Bjorn Helgaas7e0e9c02010-03-05 10:47:57 -0700633 return pnp_res;
634}
635
Bjorn Helgaas57fd51a2008-06-27 16:57:01 -0600636/*
637 * Determine whether the specified resource is a possible configuration
638 * for this device.
639 */
640int pnp_possible_config(struct pnp_dev *dev, int type, resource_size_t start,
641 resource_size_t size)
642{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600643 struct pnp_option *option;
644 struct pnp_port *port;
645 struct pnp_mem *mem;
646 struct pnp_irq *irq;
647 struct pnp_dma *dma;
Bjorn Helgaas57fd51a2008-06-27 16:57:01 -0600648
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600649 list_for_each_entry(option, &dev->options, list) {
650 if (option->type != type)
651 continue;
652
653 switch (option->type) {
654 case IORESOURCE_IO:
655 port = &option->u.port;
656 if (port->min == start && port->size == size)
657 return 1;
658 break;
659 case IORESOURCE_MEM:
660 mem = &option->u.mem;
661 if (mem->min == start && mem->size == size)
662 return 1;
663 break;
664 case IORESOURCE_IRQ:
665 irq = &option->u.irq;
666 if (start < PNP_IRQ_NR &&
667 test_bit(start, irq->map.bits))
668 return 1;
669 break;
670 case IORESOURCE_DMA:
671 dma = &option->u.dma;
672 if (dma->map & (1 << start))
673 return 1;
674 break;
675 }
676 }
Bjorn Helgaas57fd51a2008-06-27 16:57:01 -0600677
678 return 0;
679}
680EXPORT_SYMBOL(pnp_possible_config);
681
Bjorn Helgaas1b8e6962009-06-05 14:37:23 +0000682int pnp_range_reserved(resource_size_t start, resource_size_t end)
683{
684 struct pnp_dev *dev;
685 struct pnp_resource *pnp_res;
686 resource_size_t *dev_start, *dev_end;
687
688 pnp_for_each_dev(dev) {
689 list_for_each_entry(pnp_res, &dev->resources, list) {
690 dev_start = &pnp_res->res.start;
691 dev_end = &pnp_res->res.end;
692 if (ranged_conflict(&start, &end, dev_start, dev_end))
693 return 1;
694 }
695 }
696 return 0;
697}
698EXPORT_SYMBOL(pnp_range_reserved);
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700/* format is: pnp_reserve_irq=irq1[,irq2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701static int __init pnp_setup_reserve_irq(char *str)
702{
703 int i;
704
705 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700706 if (get_option(&str, &pnp_reserve_irq[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 break;
708 return 1;
709}
710
711__setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
712
713/* format is: pnp_reserve_dma=dma1[,dma2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714static int __init pnp_setup_reserve_dma(char *str)
715{
716 int i;
717
718 for (i = 0; i < 8; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700719 if (get_option(&str, &pnp_reserve_dma[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 break;
721 return 1;
722}
723
724__setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
725
726/* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727static int __init pnp_setup_reserve_io(char *str)
728{
729 int i;
730
731 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700732 if (get_option(&str, &pnp_reserve_io[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 break;
734 return 1;
735}
736
737__setup("pnp_reserve_io=", pnp_setup_reserve_io);
738
739/* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740static int __init pnp_setup_reserve_mem(char *str)
741{
742 int i;
743
744 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700745 if (get_option(&str, &pnp_reserve_mem[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 break;
747 return 1;
748}
749
750__setup("pnp_reserve_mem=", pnp_setup_reserve_mem);