manager.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * manager.c - Resource Management, Conflict Resolution, Activation and Disabling of Devices
  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. #include <linux/errno.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/pnp.h>
  15. #include <linux/bitmap.h>
  16. #include <linux/mutex.h>
  17. #include "base.h"
  18. DEFINE_MUTEX(pnp_res_mutex);
  19. static struct resource *pnp_find_resource(struct pnp_dev *dev,
  20. unsigned char rule,
  21. unsigned long type,
  22. unsigned int bar)
  23. {
  24. struct resource *res = pnp_get_resource(dev, type, bar);
  25. /* when the resource already exists, set its resource bits from rule */
  26. if (res) {
  27. res->flags &= ~IORESOURCE_BITS;
  28. res->flags |= rule & IORESOURCE_BITS;
  29. }
  30. return res;
  31. }
  32. static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx)
  33. {
  34. struct resource *res, local_res;
  35. res = pnp_find_resource(dev, rule->flags, IORESOURCE_IO, idx);
  36. if (res) {
  37. pnp_dbg(&dev->dev, " io %d already set to %#llx-%#llx "
  38. "flags %#lx\n", idx, (unsigned long long) res->start,
  39. (unsigned long long) res->end, res->flags);
  40. return 0;
  41. }
  42. res = &local_res;
  43. res->flags = rule->flags | IORESOURCE_AUTO;
  44. res->start = 0;
  45. res->end = 0;
  46. if (!rule->size) {
  47. res->flags |= IORESOURCE_DISABLED;
  48. pnp_dbg(&dev->dev, " io %d disabled\n", idx);
  49. goto __add;
  50. }
  51. res->start = rule->min;
  52. res->end = res->start + rule->size - 1;
  53. while (!pnp_check_port(dev, res)) {
  54. res->start += rule->align;
  55. res->end = res->start + rule->size - 1;
  56. if (res->start > rule->max || !rule->align) {
  57. pnp_dbg(&dev->dev, " couldn't assign io %d "
  58. "(min %#llx max %#llx)\n", idx,
  59. (unsigned long long) rule->min,
  60. (unsigned long long) rule->max);
  61. return -EBUSY;
  62. }
  63. }
  64. __add:
  65. pnp_add_io_resource(dev, res->start, res->end, res->flags);
  66. return 0;
  67. }
  68. static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
  69. {
  70. struct resource *res, local_res;
  71. res = pnp_find_resource(dev, rule->flags, IORESOURCE_MEM, idx);
  72. if (res) {
  73. pnp_dbg(&dev->dev, " mem %d already set to %#llx-%#llx "
  74. "flags %#lx\n", idx, (unsigned long long) res->start,
  75. (unsigned long long) res->end, res->flags);
  76. return 0;
  77. }
  78. res = &local_res;
  79. res->flags = rule->flags | IORESOURCE_AUTO;
  80. res->start = 0;
  81. res->end = 0;
  82. /* ??? rule->flags restricted to 8 bits, all tests bogus ??? */
  83. if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
  84. res->flags |= IORESOURCE_READONLY;
  85. if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
  86. res->flags |= IORESOURCE_RANGELENGTH;
  87. if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
  88. res->flags |= IORESOURCE_SHADOWABLE;
  89. if (!rule->size) {
  90. res->flags |= IORESOURCE_DISABLED;
  91. pnp_dbg(&dev->dev, " mem %d disabled\n", idx);
  92. goto __add;
  93. }
  94. res->start = rule->min;
  95. res->end = res->start + rule->size - 1;
  96. while (!pnp_check_mem(dev, res)) {
  97. res->start += rule->align;
  98. res->end = res->start + rule->size - 1;
  99. if (res->start > rule->max || !rule->align) {
  100. pnp_dbg(&dev->dev, " couldn't assign mem %d "
  101. "(min %#llx max %#llx)\n", idx,
  102. (unsigned long long) rule->min,
  103. (unsigned long long) rule->max);
  104. return -EBUSY;
  105. }
  106. }
  107. __add:
  108. pnp_add_mem_resource(dev, res->start, res->end, res->flags);
  109. return 0;
  110. }
  111. static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
  112. {
  113. struct resource *res, local_res;
  114. int i;
  115. /* IRQ priority: this table is good for i386 */
  116. static unsigned short xtab[16] = {
  117. 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
  118. };
  119. res = pnp_find_resource(dev, rule->flags, IORESOURCE_IRQ, idx);
  120. if (res) {
  121. pnp_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n",
  122. idx, (int) res->start, res->flags);
  123. return 0;
  124. }
  125. res = &local_res;
  126. res->flags = rule->flags | IORESOURCE_AUTO;
  127. res->start = -1;
  128. res->end = -1;
  129. if (bitmap_empty(rule->map.bits, PNP_IRQ_NR)) {
  130. res->flags |= IORESOURCE_DISABLED;
  131. pnp_dbg(&dev->dev, " irq %d disabled\n", idx);
  132. goto __add;
  133. }
  134. /* TBD: need check for >16 IRQ */
  135. res->start = find_next_bit(rule->map.bits, PNP_IRQ_NR, 16);
  136. if (res->start < PNP_IRQ_NR) {
  137. res->end = res->start;
  138. goto __add;
  139. }
  140. for (i = 0; i < 16; i++) {
  141. if (test_bit(xtab[i], rule->map.bits)) {
  142. res->start = res->end = xtab[i];
  143. if (pnp_check_irq(dev, res))
  144. goto __add;
  145. }
  146. }
  147. if (rule->flags & IORESOURCE_IRQ_OPTIONAL) {
  148. res->start = -1;
  149. res->end = -1;
  150. res->flags |= IORESOURCE_DISABLED;
  151. pnp_dbg(&dev->dev, " irq %d disabled (optional)\n", idx);
  152. goto __add;
  153. }
  154. pnp_dbg(&dev->dev, " couldn't assign irq %d\n", idx);
  155. return -EBUSY;
  156. __add:
  157. pnp_add_irq_resource(dev, res->start, res->flags);
  158. return 0;
  159. }
  160. #ifdef CONFIG_ISA_DMA_API
  161. static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
  162. {
  163. struct resource *res, local_res;
  164. int i;
  165. /* DMA priority: this table is good for i386 */
  166. static unsigned short xtab[8] = {
  167. 1, 3, 5, 6, 7, 0, 2, 4
  168. };
  169. res = pnp_find_resource(dev, rule->flags, IORESOURCE_DMA, idx);
  170. if (res) {
  171. pnp_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n",
  172. idx, (int) res->start, res->flags);
  173. return 0;
  174. }
  175. res = &local_res;
  176. res->flags = rule->flags | IORESOURCE_AUTO;
  177. res->start = -1;
  178. res->end = -1;
  179. if (!rule->map) {
  180. res->flags |= IORESOURCE_DISABLED;
  181. pnp_dbg(&dev->dev, " dma %d disabled\n", idx);
  182. goto __add;
  183. }
  184. for (i = 0; i < 8; i++) {
  185. if (rule->map & (1 << xtab[i])) {
  186. res->start = res->end = xtab[i];
  187. if (pnp_check_dma(dev, res))
  188. goto __add;
  189. }
  190. }
  191. pnp_dbg(&dev->dev, " couldn't assign dma %d\n", idx);
  192. return -EBUSY;
  193. __add:
  194. pnp_add_dma_resource(dev, res->start, res->flags);
  195. return 0;
  196. }
  197. #endif /* CONFIG_ISA_DMA_API */
  198. void pnp_init_resources(struct pnp_dev *dev)
  199. {
  200. pnp_free_resources(dev);
  201. }
  202. static void pnp_clean_resource_table(struct pnp_dev *dev)
  203. {
  204. struct pnp_resource *pnp_res, *tmp;
  205. list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
  206. if (pnp_res->res.flags & IORESOURCE_AUTO)
  207. pnp_free_resource(pnp_res);
  208. }
  209. }
  210. /**
  211. * pnp_assign_resources - assigns resources to the device based on the specified dependent number
  212. * @dev: pointer to the desired device
  213. * @set: the dependent function number
  214. */
  215. static int pnp_assign_resources(struct pnp_dev *dev, int set)
  216. {
  217. struct pnp_option *option;
  218. int nport = 0, nmem = 0, nirq = 0;
  219. int ndma __maybe_unused = 0;
  220. int ret = 0;
  221. pnp_dbg(&dev->dev, "pnp_assign_resources, try dependent set %d\n", set);
  222. mutex_lock(&pnp_res_mutex);
  223. pnp_clean_resource_table(dev);
  224. list_for_each_entry(option, &dev->options, list) {
  225. if (pnp_option_is_dependent(option) &&
  226. pnp_option_set(option) != set)
  227. continue;
  228. switch (option->type) {
  229. case IORESOURCE_IO:
  230. ret = pnp_assign_port(dev, &option->u.port, nport++);
  231. break;
  232. case IORESOURCE_MEM:
  233. ret = pnp_assign_mem(dev, &option->u.mem, nmem++);
  234. break;
  235. case IORESOURCE_IRQ:
  236. ret = pnp_assign_irq(dev, &option->u.irq, nirq++);
  237. break;
  238. #ifdef CONFIG_ISA_DMA_API
  239. case IORESOURCE_DMA:
  240. ret = pnp_assign_dma(dev, &option->u.dma, ndma++);
  241. break;
  242. #endif
  243. default:
  244. ret = -EINVAL;
  245. break;
  246. }
  247. if (ret < 0)
  248. break;
  249. }
  250. mutex_unlock(&pnp_res_mutex);
  251. if (ret < 0) {
  252. pnp_dbg(&dev->dev, "pnp_assign_resources failed (%d)\n", ret);
  253. pnp_clean_resource_table(dev);
  254. } else
  255. dbg_pnp_show_resources(dev, "pnp_assign_resources succeeded");
  256. return ret;
  257. }
  258. /**
  259. * pnp_auto_config_dev - automatically assigns resources to a device
  260. * @dev: pointer to the desired device
  261. */
  262. int pnp_auto_config_dev(struct pnp_dev *dev)
  263. {
  264. int i, ret;
  265. if (!pnp_can_configure(dev)) {
  266. pnp_dbg(&dev->dev, "configuration not supported\n");
  267. return -ENODEV;
  268. }
  269. ret = pnp_assign_resources(dev, 0);
  270. if (ret == 0)
  271. return 0;
  272. for (i = 1; i < dev->num_dependent_sets; i++) {
  273. ret = pnp_assign_resources(dev, i);
  274. if (ret == 0)
  275. return 0;
  276. }
  277. dev_err(&dev->dev, "unable to assign resources\n");
  278. return ret;
  279. }
  280. /**
  281. * pnp_start_dev - low-level start of the PnP device
  282. * @dev: pointer to the desired device
  283. *
  284. * assumes that resources have already been allocated
  285. */
  286. int pnp_start_dev(struct pnp_dev *dev)
  287. {
  288. if (!pnp_can_write(dev)) {
  289. pnp_dbg(&dev->dev, "activation not supported\n");
  290. return -EINVAL;
  291. }
  292. dbg_pnp_show_resources(dev, "pnp_start_dev");
  293. if (dev->protocol->set(dev) < 0) {
  294. dev_err(&dev->dev, "activation failed\n");
  295. return -EIO;
  296. }
  297. dev_info(&dev->dev, "activated\n");
  298. return 0;
  299. }
  300. /**
  301. * pnp_stop_dev - low-level disable of the PnP device
  302. * @dev: pointer to the desired device
  303. *
  304. * does not free resources
  305. */
  306. int pnp_stop_dev(struct pnp_dev *dev)
  307. {
  308. if (!pnp_can_disable(dev)) {
  309. pnp_dbg(&dev->dev, "disabling not supported\n");
  310. return -EINVAL;
  311. }
  312. if (dev->protocol->disable(dev) < 0) {
  313. dev_err(&dev->dev, "disable failed\n");
  314. return -EIO;
  315. }
  316. dev_info(&dev->dev, "disabled\n");
  317. return 0;
  318. }
  319. /**
  320. * pnp_activate_dev - activates a PnP device for use
  321. * @dev: pointer to the desired device
  322. *
  323. * does not validate or set resources so be careful.
  324. */
  325. int pnp_activate_dev(struct pnp_dev *dev)
  326. {
  327. int error;
  328. if (dev->active)
  329. return 0;
  330. /* ensure resources are allocated */
  331. if (pnp_auto_config_dev(dev))
  332. return -EBUSY;
  333. error = pnp_start_dev(dev);
  334. if (error)
  335. return error;
  336. dev->active = 1;
  337. return 0;
  338. }
  339. /**
  340. * pnp_disable_dev - disables device
  341. * @dev: pointer to the desired device
  342. *
  343. * inform the correct pnp protocol so that resources can be used by other devices
  344. */
  345. int pnp_disable_dev(struct pnp_dev *dev)
  346. {
  347. int error;
  348. if (!dev->active)
  349. return 0;
  350. error = pnp_stop_dev(dev);
  351. if (error)
  352. return error;
  353. dev->active = 0;
  354. /* release the resources so that other devices can use them */
  355. mutex_lock(&pnp_res_mutex);
  356. pnp_clean_resource_table(dev);
  357. mutex_unlock(&pnp_res_mutex);
  358. return 0;
  359. }
  360. EXPORT_SYMBOL(pnp_start_dev);
  361. EXPORT_SYMBOL(pnp_stop_dev);
  362. EXPORT_SYMBOL(pnp_activate_dev);
  363. EXPORT_SYMBOL(pnp_disable_dev);