resource.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. */
  7. #include <linux/module.h>
  8. #include <linux/errno.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/kernel.h>
  11. #include <asm/io.h>
  12. #include <asm/dma.h>
  13. #include <asm/irq.h>
  14. #include <linux/pci.h>
  15. #include <linux/ioport.h>
  16. #include <linux/init.h>
  17. #include <linux/pnp.h>
  18. #include "base.h"
  19. static int pnp_reserve_irq[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some IRQ */
  20. static int pnp_reserve_dma[8] = {[0 ... 7] = -1 }; /* reserve (don't use) some DMA */
  21. static int pnp_reserve_io[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some I/O region */
  22. static int pnp_reserve_mem[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some memory region */
  23. /*
  24. * option registration
  25. */
  26. static struct pnp_option *pnp_build_option(int priority)
  27. {
  28. struct pnp_option *option = pnp_alloc(sizeof(struct pnp_option));
  29. if (!option)
  30. return NULL;
  31. option->priority = priority & 0xff;
  32. /* make sure the priority is valid */
  33. if (option->priority > PNP_RES_PRIORITY_FUNCTIONAL)
  34. option->priority = PNP_RES_PRIORITY_INVALID;
  35. return option;
  36. }
  37. struct pnp_option *pnp_register_independent_option(struct pnp_dev *dev)
  38. {
  39. struct pnp_option *option;
  40. option = pnp_build_option(PNP_RES_PRIORITY_PREFERRED);
  41. /* this should never happen but if it does we'll try to continue */
  42. if (dev->independent)
  43. dev_err(&dev->dev, "independent resource already registered\n");
  44. dev->independent = option;
  45. return option;
  46. }
  47. struct pnp_option *pnp_register_dependent_option(struct pnp_dev *dev,
  48. int priority)
  49. {
  50. struct pnp_option *option;
  51. option = pnp_build_option(priority);
  52. if (dev->dependent) {
  53. struct pnp_option *parent = dev->dependent;
  54. while (parent->next)
  55. parent = parent->next;
  56. parent->next = option;
  57. } else
  58. dev->dependent = option;
  59. return option;
  60. }
  61. int pnp_register_irq_resource(struct pnp_option *option, struct pnp_irq *data)
  62. {
  63. struct pnp_irq *ptr;
  64. ptr = option->irq;
  65. while (ptr && ptr->next)
  66. ptr = ptr->next;
  67. if (ptr)
  68. ptr->next = data;
  69. else
  70. option->irq = data;
  71. #ifdef CONFIG_PCI
  72. {
  73. int i;
  74. for (i = 0; i < 16; i++)
  75. if (test_bit(i, data->map))
  76. pcibios_penalize_isa_irq(i, 0);
  77. }
  78. #endif
  79. return 0;
  80. }
  81. int pnp_register_dma_resource(struct pnp_option *option, struct pnp_dma *data)
  82. {
  83. struct pnp_dma *ptr;
  84. ptr = option->dma;
  85. while (ptr && ptr->next)
  86. ptr = ptr->next;
  87. if (ptr)
  88. ptr->next = data;
  89. else
  90. option->dma = data;
  91. return 0;
  92. }
  93. int pnp_register_port_resource(struct pnp_option *option, struct pnp_port *data)
  94. {
  95. struct pnp_port *ptr;
  96. ptr = option->port;
  97. while (ptr && ptr->next)
  98. ptr = ptr->next;
  99. if (ptr)
  100. ptr->next = data;
  101. else
  102. option->port = data;
  103. return 0;
  104. }
  105. int pnp_register_mem_resource(struct pnp_option *option, struct pnp_mem *data)
  106. {
  107. struct pnp_mem *ptr;
  108. ptr = option->mem;
  109. while (ptr && ptr->next)
  110. ptr = ptr->next;
  111. if (ptr)
  112. ptr->next = data;
  113. else
  114. option->mem = data;
  115. return 0;
  116. }
  117. static void pnp_free_port(struct pnp_port *port)
  118. {
  119. struct pnp_port *next;
  120. while (port) {
  121. next = port->next;
  122. kfree(port);
  123. port = next;
  124. }
  125. }
  126. static void pnp_free_irq(struct pnp_irq *irq)
  127. {
  128. struct pnp_irq *next;
  129. while (irq) {
  130. next = irq->next;
  131. kfree(irq);
  132. irq = next;
  133. }
  134. }
  135. static void pnp_free_dma(struct pnp_dma *dma)
  136. {
  137. struct pnp_dma *next;
  138. while (dma) {
  139. next = dma->next;
  140. kfree(dma);
  141. dma = next;
  142. }
  143. }
  144. static void pnp_free_mem(struct pnp_mem *mem)
  145. {
  146. struct pnp_mem *next;
  147. while (mem) {
  148. next = mem->next;
  149. kfree(mem);
  150. mem = next;
  151. }
  152. }
  153. void pnp_free_option(struct pnp_option *option)
  154. {
  155. struct pnp_option *next;
  156. while (option) {
  157. next = option->next;
  158. pnp_free_port(option->port);
  159. pnp_free_irq(option->irq);
  160. pnp_free_dma(option->dma);
  161. pnp_free_mem(option->mem);
  162. kfree(option);
  163. option = next;
  164. }
  165. }
  166. /*
  167. * resource validity checking
  168. */
  169. #define length(start, end) (*(end) - *(start) + 1)
  170. /* Two ranges conflict if one doesn't end before the other starts */
  171. #define ranged_conflict(starta, enda, startb, endb) \
  172. !((*(enda) < *(startb)) || (*(endb) < *(starta)))
  173. #define cannot_compare(flags) \
  174. ((flags) & (IORESOURCE_UNSET | IORESOURCE_DISABLED))
  175. int pnp_check_port(struct pnp_dev *dev, int idx)
  176. {
  177. int tmp;
  178. struct pnp_dev *tdev;
  179. resource_size_t *port, *end, *tport, *tend;
  180. port = &dev->res.port_resource[idx].start;
  181. end = &dev->res.port_resource[idx].end;
  182. /* if the resource doesn't exist, don't complain about it */
  183. if (cannot_compare(dev->res.port_resource[idx].flags))
  184. return 1;
  185. /* check if the resource is already in use, skip if the
  186. * device is active because it itself may be in use */
  187. if (!dev->active) {
  188. if (__check_region(&ioport_resource, *port, length(port, end)))
  189. return 0;
  190. }
  191. /* check if the resource is reserved */
  192. for (tmp = 0; tmp < 8; tmp++) {
  193. int rport = pnp_reserve_io[tmp << 1];
  194. int rend = pnp_reserve_io[(tmp << 1) + 1] + rport - 1;
  195. if (ranged_conflict(port, end, &rport, &rend))
  196. return 0;
  197. }
  198. /* check for internal conflicts */
  199. for (tmp = 0; tmp < PNP_MAX_PORT && tmp != idx; tmp++) {
  200. if (dev->res.port_resource[tmp].flags & IORESOURCE_IO) {
  201. tport = &dev->res.port_resource[tmp].start;
  202. tend = &dev->res.port_resource[tmp].end;
  203. if (ranged_conflict(port, end, tport, tend))
  204. return 0;
  205. }
  206. }
  207. /* check for conflicts with other pnp devices */
  208. pnp_for_each_dev(tdev) {
  209. if (tdev == dev)
  210. continue;
  211. for (tmp = 0; tmp < PNP_MAX_PORT; tmp++) {
  212. if (tdev->res.port_resource[tmp].flags & IORESOURCE_IO) {
  213. if (cannot_compare
  214. (tdev->res.port_resource[tmp].flags))
  215. continue;
  216. tport = &tdev->res.port_resource[tmp].start;
  217. tend = &tdev->res.port_resource[tmp].end;
  218. if (ranged_conflict(port, end, tport, tend))
  219. return 0;
  220. }
  221. }
  222. }
  223. return 1;
  224. }
  225. int pnp_check_mem(struct pnp_dev *dev, int idx)
  226. {
  227. int tmp;
  228. struct pnp_dev *tdev;
  229. resource_size_t *addr, *end, *taddr, *tend;
  230. addr = &dev->res.mem_resource[idx].start;
  231. end = &dev->res.mem_resource[idx].end;
  232. /* if the resource doesn't exist, don't complain about it */
  233. if (cannot_compare(dev->res.mem_resource[idx].flags))
  234. return 1;
  235. /* check if the resource is already in use, skip if the
  236. * device is active because it itself may be in use */
  237. if (!dev->active) {
  238. if (check_mem_region(*addr, length(addr, end)))
  239. return 0;
  240. }
  241. /* check if the resource is reserved */
  242. for (tmp = 0; tmp < 8; tmp++) {
  243. int raddr = pnp_reserve_mem[tmp << 1];
  244. int rend = pnp_reserve_mem[(tmp << 1) + 1] + raddr - 1;
  245. if (ranged_conflict(addr, end, &raddr, &rend))
  246. return 0;
  247. }
  248. /* check for internal conflicts */
  249. for (tmp = 0; tmp < PNP_MAX_MEM && tmp != idx; tmp++) {
  250. if (dev->res.mem_resource[tmp].flags & IORESOURCE_MEM) {
  251. taddr = &dev->res.mem_resource[tmp].start;
  252. tend = &dev->res.mem_resource[tmp].end;
  253. if (ranged_conflict(addr, end, taddr, tend))
  254. return 0;
  255. }
  256. }
  257. /* check for conflicts with other pnp devices */
  258. pnp_for_each_dev(tdev) {
  259. if (tdev == dev)
  260. continue;
  261. for (tmp = 0; tmp < PNP_MAX_MEM; tmp++) {
  262. if (tdev->res.mem_resource[tmp].flags & IORESOURCE_MEM) {
  263. if (cannot_compare
  264. (tdev->res.mem_resource[tmp].flags))
  265. continue;
  266. taddr = &tdev->res.mem_resource[tmp].start;
  267. tend = &tdev->res.mem_resource[tmp].end;
  268. if (ranged_conflict(addr, end, taddr, tend))
  269. return 0;
  270. }
  271. }
  272. }
  273. return 1;
  274. }
  275. static irqreturn_t pnp_test_handler(int irq, void *dev_id)
  276. {
  277. return IRQ_HANDLED;
  278. }
  279. int pnp_check_irq(struct pnp_dev *dev, int idx)
  280. {
  281. int tmp;
  282. struct pnp_dev *tdev;
  283. resource_size_t *irq = &dev->res.irq_resource[idx].start;
  284. /* if the resource doesn't exist, don't complain about it */
  285. if (cannot_compare(dev->res.irq_resource[idx].flags))
  286. return 1;
  287. /* check if the resource is valid */
  288. if (*irq < 0 || *irq > 15)
  289. return 0;
  290. /* check if the resource is reserved */
  291. for (tmp = 0; tmp < 16; tmp++) {
  292. if (pnp_reserve_irq[tmp] == *irq)
  293. return 0;
  294. }
  295. /* check for internal conflicts */
  296. for (tmp = 0; tmp < PNP_MAX_IRQ && tmp != idx; tmp++) {
  297. if (dev->res.irq_resource[tmp].flags & IORESOURCE_IRQ) {
  298. if (dev->res.irq_resource[tmp].start == *irq)
  299. return 0;
  300. }
  301. }
  302. #ifdef CONFIG_PCI
  303. /* check if the resource is being used by a pci device */
  304. {
  305. struct pci_dev *pci = NULL;
  306. for_each_pci_dev(pci) {
  307. if (pci->irq == *irq)
  308. return 0;
  309. }
  310. }
  311. #endif
  312. /* check if the resource is already in use, skip if the
  313. * device is active because it itself may be in use */
  314. if (!dev->active) {
  315. if (request_irq(*irq, pnp_test_handler,
  316. IRQF_DISABLED | IRQF_PROBE_SHARED, "pnp", NULL))
  317. return 0;
  318. free_irq(*irq, NULL);
  319. }
  320. /* check for conflicts with other pnp devices */
  321. pnp_for_each_dev(tdev) {
  322. if (tdev == dev)
  323. continue;
  324. for (tmp = 0; tmp < PNP_MAX_IRQ; tmp++) {
  325. if (tdev->res.irq_resource[tmp].flags & IORESOURCE_IRQ) {
  326. if (cannot_compare
  327. (tdev->res.irq_resource[tmp].flags))
  328. continue;
  329. if ((tdev->res.irq_resource[tmp].start == *irq))
  330. return 0;
  331. }
  332. }
  333. }
  334. return 1;
  335. }
  336. int pnp_check_dma(struct pnp_dev *dev, int idx)
  337. {
  338. #ifndef CONFIG_IA64
  339. int tmp;
  340. struct pnp_dev *tdev;
  341. resource_size_t *dma = &dev->res.dma_resource[idx].start;
  342. /* if the resource doesn't exist, don't complain about it */
  343. if (cannot_compare(dev->res.dma_resource[idx].flags))
  344. return 1;
  345. /* check if the resource is valid */
  346. if (*dma < 0 || *dma == 4 || *dma > 7)
  347. return 0;
  348. /* check if the resource is reserved */
  349. for (tmp = 0; tmp < 8; tmp++) {
  350. if (pnp_reserve_dma[tmp] == *dma)
  351. return 0;
  352. }
  353. /* check for internal conflicts */
  354. for (tmp = 0; tmp < PNP_MAX_DMA && tmp != idx; tmp++) {
  355. if (dev->res.dma_resource[tmp].flags & IORESOURCE_DMA) {
  356. if (dev->res.dma_resource[tmp].start == *dma)
  357. return 0;
  358. }
  359. }
  360. /* check if the resource is already in use, skip if the
  361. * device is active because it itself may be in use */
  362. if (!dev->active) {
  363. if (request_dma(*dma, "pnp"))
  364. return 0;
  365. free_dma(*dma);
  366. }
  367. /* check for conflicts with other pnp devices */
  368. pnp_for_each_dev(tdev) {
  369. if (tdev == dev)
  370. continue;
  371. for (tmp = 0; tmp < PNP_MAX_DMA; tmp++) {
  372. if (tdev->res.dma_resource[tmp].flags & IORESOURCE_DMA) {
  373. if (cannot_compare
  374. (tdev->res.dma_resource[tmp].flags))
  375. continue;
  376. if ((tdev->res.dma_resource[tmp].start == *dma))
  377. return 0;
  378. }
  379. }
  380. }
  381. return 1;
  382. #else
  383. /* IA64 does not have legacy DMA */
  384. return 0;
  385. #endif
  386. }
  387. /* format is: pnp_reserve_irq=irq1[,irq2] .... */
  388. static int __init pnp_setup_reserve_irq(char *str)
  389. {
  390. int i;
  391. for (i = 0; i < 16; i++)
  392. if (get_option(&str, &pnp_reserve_irq[i]) != 2)
  393. break;
  394. return 1;
  395. }
  396. __setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
  397. /* format is: pnp_reserve_dma=dma1[,dma2] .... */
  398. static int __init pnp_setup_reserve_dma(char *str)
  399. {
  400. int i;
  401. for (i = 0; i < 8; i++)
  402. if (get_option(&str, &pnp_reserve_dma[i]) != 2)
  403. break;
  404. return 1;
  405. }
  406. __setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
  407. /* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
  408. static int __init pnp_setup_reserve_io(char *str)
  409. {
  410. int i;
  411. for (i = 0; i < 16; i++)
  412. if (get_option(&str, &pnp_reserve_io[i]) != 2)
  413. break;
  414. return 1;
  415. }
  416. __setup("pnp_reserve_io=", pnp_setup_reserve_io);
  417. /* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
  418. static int __init pnp_setup_reserve_mem(char *str)
  419. {
  420. int i;
  421. for (i = 0; i < 16; i++)
  422. if (get_option(&str, &pnp_reserve_mem[i]) != 2)
  423. break;
  424. return 1;
  425. }
  426. __setup("pnp_reserve_mem=", pnp_setup_reserve_mem);