blob: 64d0596bafb5949aacc8e1ae9c6bffdd578f91fe [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>
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
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070024static int pnp_reserve_irq[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some IRQ */
25static int pnp_reserve_dma[8] = {[0 ... 7] = -1 }; /* reserve (don't use) some DMA */
26static int pnp_reserve_io[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some I/O region */
27static int pnp_reserve_mem[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some memory region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29/*
30 * option registration
31 */
32
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060033struct pnp_option *pnp_build_option(struct pnp_dev *dev, unsigned long type,
34 unsigned int option_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060036 struct pnp_option *option;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060038 option = kzalloc(sizeof(struct pnp_option), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 if (!option)
40 return NULL;
41
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060042 option->flags = option_flags;
43 option->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060045 list_add_tail(&option->list, &dev->options);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 return option;
47}
48
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060049int pnp_register_irq_resource(struct pnp_dev *dev, unsigned int option_flags,
Bjorn Helgaasc2275362008-06-27 16:57:11 -060050 pnp_irq_mask_t *map, unsigned char flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060052 struct pnp_option *option;
53 struct pnp_irq *irq;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070054
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060055 option = pnp_build_option(dev, IORESOURCE_IRQ, option_flags);
56 if (!option)
Bjorn Helgaasc2275362008-06-27 16:57:11 -060057 return -ENOMEM;
58
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060059 irq = &option->u.irq;
Bjorn Helgaas2d29a7a2008-06-27 16:57:13 -060060 irq->map = *map;
61 irq->flags = flags;
Bjorn Helgaasc2275362008-06-27 16:57:11 -060062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#ifdef CONFIG_PCI
64 {
65 int i;
66
67 for (i = 0; i < 16; i++)
Bjorn Helgaas2d29a7a2008-06-27 16:57:13 -060068 if (test_bit(i, irq->map.bits))
David Shaohua Lic9c3e452005-04-01 00:07:31 -050069 pcibios_penalize_isa_irq(i, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
71#endif
Bjorn Helgaasc1caf062008-04-28 16:34:04 -060072
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060073 dbg_pnp_show_option(dev, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return 0;
75}
76
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060077int pnp_register_dma_resource(struct pnp_dev *dev, unsigned int option_flags,
Bjorn Helgaasc2275362008-06-27 16:57:11 -060078 unsigned char map, unsigned char flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060080 struct pnp_option *option;
81 struct pnp_dma *dma;
Bjorn Helgaasc2275362008-06-27 16:57:11 -060082
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060083 option = pnp_build_option(dev, IORESOURCE_DMA, option_flags);
84 if (!option)
Bjorn Helgaasc2275362008-06-27 16:57:11 -060085 return -ENOMEM;
86
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060087 dma = &option->u.dma;
Bjorn Helgaas2d29a7a2008-06-27 16:57:13 -060088 dma->map = map;
89 dma->flags = flags;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070090
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060091 dbg_pnp_show_option(dev, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return 0;
93}
94
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060095int pnp_register_port_resource(struct pnp_dev *dev, unsigned int option_flags,
Bjorn Helgaasc2275362008-06-27 16:57:11 -060096 resource_size_t min, resource_size_t max,
97 resource_size_t align, resource_size_t size,
98 unsigned char flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600100 struct pnp_option *option;
101 struct pnp_port *port;
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600102
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600103 option = pnp_build_option(dev, IORESOURCE_IO, option_flags);
104 if (!option)
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600105 return -ENOMEM;
106
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600107 port = &option->u.port;
Bjorn Helgaas2d29a7a2008-06-27 16:57:13 -0600108 port->min = min;
109 port->max = max;
110 port->align = align;
111 port->size = size;
112 port->flags = flags;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700113
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600114 dbg_pnp_show_option(dev, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return 0;
116}
117
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600118int pnp_register_mem_resource(struct pnp_dev *dev, unsigned int option_flags,
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600119 resource_size_t min, resource_size_t max,
120 resource_size_t align, resource_size_t size,
121 unsigned char flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600123 struct pnp_option *option;
124 struct pnp_mem *mem;
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600125
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600126 option = pnp_build_option(dev, IORESOURCE_MEM, option_flags);
127 if (!option)
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600128 return -ENOMEM;
129
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600130 mem = &option->u.mem;
Bjorn Helgaas2d29a7a2008-06-27 16:57:13 -0600131 mem->min = min;
132 mem->max = max;
133 mem->align = align;
134 mem->size = size;
135 mem->flags = flags;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700136
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600137 dbg_pnp_show_option(dev, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return 0;
139}
140
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600141void pnp_free_options(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600143 struct pnp_option *option, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600145 list_for_each_entry_safe(option, tmp, &dev->options, list) {
146 list_del(&option->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 kfree(option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/*
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) \
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600162((flags) & IORESOURCE_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600164int pnp_check_port(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600166 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600168 struct resource *tres;
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700169 resource_size_t *port, *end, *tport, *tend;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700170
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600171 port = &res->start;
172 end = &res->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600175 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 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 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700180 if (!dev->active) {
181 if (__check_region(&ioport_resource, *port, length(port, end)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return 0;
183 }
184
185 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600186 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;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700189 if (ranged_conflict(port, end, &rport, &rend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return 0;
191 }
192
193 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600194 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IO, i)); i++) {
195 if (tres != res && tres->flags & IORESOURCE_IO) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600196 tport = &tres->start;
197 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700198 if (ranged_conflict(port, end, tport, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 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;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600207 for (i = 0;
208 (tres = pnp_get_resource(tdev, IORESOURCE_IO, i));
209 i++) {
210 if (tres->flags & IORESOURCE_IO) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600211 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600213 tport = &tres->start;
214 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700215 if (ranged_conflict(port, end, tport, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return 0;
217 }
218 }
219 }
220
221 return 1;
222}
223
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600224int pnp_check_mem(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600226 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600228 struct resource *tres;
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700229 resource_size_t *addr, *end, *taddr, *tend;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700230
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600231 addr = &res->start;
232 end = &res->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600235 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 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 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700240 if (!dev->active) {
241 if (check_mem_region(*addr, length(addr, end)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return 0;
243 }
244
245 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600246 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;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700249 if (ranged_conflict(addr, end, &raddr, &rend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return 0;
251 }
252
253 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600254 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_MEM, i)); i++) {
255 if (tres != res && tres->flags & IORESOURCE_MEM) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600256 taddr = &tres->start;
257 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700258 if (ranged_conflict(addr, end, taddr, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 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;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600267 for (i = 0;
268 (tres = pnp_get_resource(tdev, IORESOURCE_MEM, i));
269 i++) {
270 if (tres->flags & IORESOURCE_MEM) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600271 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600273 taddr = &tres->start;
274 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700275 if (ranged_conflict(addr, end, taddr, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return 0;
277 }
278 }
279 }
280
281 return 1;
282}
283
David Howells7d12e782006-10-05 14:55:46 +0100284static irqreturn_t pnp_test_handler(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 return IRQ_HANDLED;
287}
288
Bjorn Helgaas84684c72008-06-27 16:57:18 -0600289#ifdef CONFIG_PCI
290static 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) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600297 pnp_dbg(&pnp->dev, " device %s using irq %d\n",
Bjorn Helgaas84684c72008-06-27 16:57:18 -0600298 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) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600319 pnp_dbg(&pnp->dev, " legacy IDE device %s "
Bjorn Helgaas84684c72008-06-27 16:57:18 -0600320 "using irq %d\n", pci_name(pci), irq);
321 return 1;
322 }
323 }
324
325 return 0;
326}
327#endif
328
329static 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
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600344int pnp_check_irq(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600346 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600348 struct resource *tres;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600349 resource_size_t *irq;
350
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600351 irq = &res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600354 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 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 */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600362 for (i = 0; i < 16; i++) {
363 if (pnp_reserve_irq[i] == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return 0;
365 }
366
367 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600368 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IRQ, i)); i++) {
369 if (tres != res && tres->flags & IORESOURCE_IRQ) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600370 if (tres->start == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return 0;
372 }
373 }
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /* check if the resource is being used by a pci device */
Bjorn Helgaas84684c72008-06-27 16:57:18 -0600376 if (pci_uses_irq(dev, *irq))
377 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 /* check if the resource is already in use, skip if the
380 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700381 if (!dev->active) {
Andrew Morton0cadaf42006-07-01 04:36:37 -0700382 if (request_irq(*irq, pnp_test_handler,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700383 IRQF_DISABLED | IRQF_PROBE_SHARED, "pnp", NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 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;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600392 for (i = 0;
393 (tres = pnp_get_resource(tdev, IORESOURCE_IRQ, i));
394 i++) {
395 if (tres->flags & IORESOURCE_IRQ) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600396 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600398 if (tres->start == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return 0;
400 }
401 }
402 }
403
404 return 1;
405}
406
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600407int pnp_check_dma(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409#ifndef CONFIG_IA64
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600410 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600412 struct resource *tres;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600413 resource_size_t *dma;
414
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600415 dma = &res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600418 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 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 */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600426 for (i = 0; i < 8; i++) {
427 if (pnp_reserve_dma[i] == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return 0;
429 }
430
431 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600432 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_DMA, i)); i++) {
433 if (tres != res && tres->flags & IORESOURCE_DMA) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600434 if (tres->start == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 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 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700441 if (!dev->active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 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;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600451 for (i = 0;
452 (tres = pnp_get_resource(tdev, IORESOURCE_DMA, i));
453 i++) {
454 if (tres->flags & IORESOURCE_DMA) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600455 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600457 if (tres->start == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return 0;
459 }
460 }
461 }
462
463 return 1;
464#else
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700465 /* IA64 does not have legacy DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return 0;
467#endif
468}
469
Rene Hermanb563cf52008-10-15 22:03:58 -0700470unsigned long pnp_resource_type(struct resource *res)
Bjorn Helgaas940e98d2008-06-27 16:56:54 -0600471{
472 return res->flags & (IORESOURCE_IO | IORESOURCE_MEM |
473 IORESOURCE_IRQ | IORESOURCE_DMA);
474}
475
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600476struct resource *pnp_get_resource(struct pnp_dev *dev,
Rene Hermanb563cf52008-10-15 22:03:58 -0700477 unsigned long type, unsigned int num)
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600478{
479 struct pnp_resource *pnp_res;
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600480 struct resource *res;
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600481
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600482 list_for_each_entry(pnp_res, &dev->resources, list) {
483 res = &pnp_res->res;
484 if (pnp_resource_type(res) == type && num-- == 0)
485 return res;
486 }
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600487 return NULL;
488}
Bjorn Helgaasb90eca02008-04-28 16:34:14 -0600489EXPORT_SYMBOL(pnp_get_resource);
490
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600491static struct pnp_resource *pnp_new_resource(struct pnp_dev *dev)
Bjorn Helgaasa50b6d72008-04-28 16:34:33 -0600492{
493 struct pnp_resource *pnp_res;
Bjorn Helgaasa50b6d72008-04-28 16:34:33 -0600494
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600495 pnp_res = kzalloc(sizeof(struct pnp_resource), GFP_KERNEL);
496 if (!pnp_res)
497 return NULL;
498
499 list_add_tail(&pnp_res->list, &dev->resources);
500 return pnp_res;
Bjorn Helgaasa50b6d72008-04-28 16:34:33 -0600501}
502
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600503struct pnp_resource *pnp_add_irq_resource(struct pnp_dev *dev, int irq,
504 int flags)
505{
506 struct pnp_resource *pnp_res;
507 struct resource *res;
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600508
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600509 pnp_res = pnp_new_resource(dev);
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600510 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600511 dev_err(&dev->dev, "can't add resource for IRQ %d\n", irq);
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600512 return NULL;
513 }
514
515 res = &pnp_res->res;
516 res->flags = IORESOURCE_IRQ | flags;
517 res->start = irq;
518 res->end = irq;
519
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600520 pnp_dbg(&dev->dev, " add %pr\n", res);
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600521 return pnp_res;
522}
523
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600524struct pnp_resource *pnp_add_dma_resource(struct pnp_dev *dev, int dma,
525 int flags)
526{
527 struct pnp_resource *pnp_res;
528 struct resource *res;
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600529
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600530 pnp_res = pnp_new_resource(dev);
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600531 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600532 dev_err(&dev->dev, "can't add resource for DMA %d\n", dma);
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600533 return NULL;
534 }
535
536 res = &pnp_res->res;
537 res->flags = IORESOURCE_DMA | flags;
538 res->start = dma;
539 res->end = dma;
540
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600541 pnp_dbg(&dev->dev, " add %pr\n", res);
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600542 return pnp_res;
543}
544
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600545struct pnp_resource *pnp_add_io_resource(struct pnp_dev *dev,
546 resource_size_t start,
547 resource_size_t end, int flags)
548{
549 struct pnp_resource *pnp_res;
550 struct resource *res;
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600551
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600552 pnp_res = pnp_new_resource(dev);
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600553 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600554 dev_err(&dev->dev, "can't add resource for IO %#llx-%#llx\n",
555 (unsigned long long) start,
556 (unsigned long long) end);
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600557 return NULL;
558 }
559
560 res = &pnp_res->res;
561 res->flags = IORESOURCE_IO | flags;
562 res->start = start;
563 res->end = end;
564
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600565 pnp_dbg(&dev->dev, " add %pr\n", res);
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600566 return pnp_res;
567}
568
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600569struct pnp_resource *pnp_add_mem_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;
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600575
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600576 pnp_res = pnp_new_resource(dev);
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600577 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600578 dev_err(&dev->dev, "can't add resource for MEM %#llx-%#llx\n",
579 (unsigned long long) start,
580 (unsigned long long) end);
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600581 return NULL;
582 }
583
584 res = &pnp_res->res;
585 res->flags = IORESOURCE_MEM | flags;
586 res->start = start;
587 res->end = end;
588
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600589 pnp_dbg(&dev->dev, " add %pr\n", res);
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600590 return pnp_res;
591}
592
Bjorn Helgaas57fd51a2008-06-27 16:57:01 -0600593/*
594 * Determine whether the specified resource is a possible configuration
595 * for this device.
596 */
597int pnp_possible_config(struct pnp_dev *dev, int type, resource_size_t start,
598 resource_size_t size)
599{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600600 struct pnp_option *option;
601 struct pnp_port *port;
602 struct pnp_mem *mem;
603 struct pnp_irq *irq;
604 struct pnp_dma *dma;
Bjorn Helgaas57fd51a2008-06-27 16:57:01 -0600605
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600606 list_for_each_entry(option, &dev->options, list) {
607 if (option->type != type)
608 continue;
609
610 switch (option->type) {
611 case IORESOURCE_IO:
612 port = &option->u.port;
613 if (port->min == start && port->size == size)
614 return 1;
615 break;
616 case IORESOURCE_MEM:
617 mem = &option->u.mem;
618 if (mem->min == start && mem->size == size)
619 return 1;
620 break;
621 case IORESOURCE_IRQ:
622 irq = &option->u.irq;
623 if (start < PNP_IRQ_NR &&
624 test_bit(start, irq->map.bits))
625 return 1;
626 break;
627 case IORESOURCE_DMA:
628 dma = &option->u.dma;
629 if (dma->map & (1 << start))
630 return 1;
631 break;
632 }
633 }
Bjorn Helgaas57fd51a2008-06-27 16:57:01 -0600634
635 return 0;
636}
637EXPORT_SYMBOL(pnp_possible_config);
638
Bjorn Helgaas1b8e6962009-06-05 14:37:23 +0000639int pnp_range_reserved(resource_size_t start, resource_size_t end)
640{
641 struct pnp_dev *dev;
642 struct pnp_resource *pnp_res;
643 resource_size_t *dev_start, *dev_end;
644
645 pnp_for_each_dev(dev) {
646 list_for_each_entry(pnp_res, &dev->resources, list) {
647 dev_start = &pnp_res->res.start;
648 dev_end = &pnp_res->res.end;
649 if (ranged_conflict(&start, &end, dev_start, dev_end))
650 return 1;
651 }
652 }
653 return 0;
654}
655EXPORT_SYMBOL(pnp_range_reserved);
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657/* format is: pnp_reserve_irq=irq1[,irq2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658static int __init pnp_setup_reserve_irq(char *str)
659{
660 int i;
661
662 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700663 if (get_option(&str, &pnp_reserve_irq[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 break;
665 return 1;
666}
667
668__setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
669
670/* format is: pnp_reserve_dma=dma1[,dma2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671static int __init pnp_setup_reserve_dma(char *str)
672{
673 int i;
674
675 for (i = 0; i < 8; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700676 if (get_option(&str, &pnp_reserve_dma[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 break;
678 return 1;
679}
680
681__setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
682
683/* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684static int __init pnp_setup_reserve_io(char *str)
685{
686 int i;
687
688 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700689 if (get_option(&str, &pnp_reserve_io[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 break;
691 return 1;
692}
693
694__setup("pnp_reserve_io=", pnp_setup_reserve_io);
695
696/* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697static int __init pnp_setup_reserve_mem(char *str)
698{
699 int i;
700
701 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700702 if (get_option(&str, &pnp_reserve_mem[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 break;
704 return 1;
705}
706
707__setup("pnp_reserve_mem=", pnp_setup_reserve_mem);