resource.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * linux/kernel/resource.c
  3. *
  4. * Copyright (C) 1999 Linus Torvalds
  5. * Copyright (C) 1999 Martin Mares <mj@ucw.cz>
  6. *
  7. * Arbitrary resource management.
  8. */
  9. #include <linux/config.h>
  10. #include <linux/module.h>
  11. #include <linux/sched.h>
  12. #include <linux/errno.h>
  13. #include <linux/ioport.h>
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/fs.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/seq_file.h>
  20. #include <asm/io.h>
  21. struct resource ioport_resource = {
  22. .name = "PCI IO",
  23. .start = 0x0000,
  24. .end = IO_SPACE_LIMIT,
  25. .flags = IORESOURCE_IO,
  26. };
  27. EXPORT_SYMBOL(ioport_resource);
  28. struct resource iomem_resource = {
  29. .name = "PCI mem",
  30. .start = 0UL,
  31. .end = ~0UL,
  32. .flags = IORESOURCE_MEM,
  33. };
  34. EXPORT_SYMBOL(iomem_resource);
  35. static DEFINE_RWLOCK(resource_lock);
  36. #ifdef CONFIG_PROC_FS
  37. enum { MAX_IORES_LEVEL = 5 };
  38. static void *r_next(struct seq_file *m, void *v, loff_t *pos)
  39. {
  40. struct resource *p = v;
  41. (*pos)++;
  42. if (p->child)
  43. return p->child;
  44. while (!p->sibling && p->parent)
  45. p = p->parent;
  46. return p->sibling;
  47. }
  48. static void *r_start(struct seq_file *m, loff_t *pos)
  49. __acquires(resource_lock)
  50. {
  51. struct resource *p = m->private;
  52. loff_t l = 0;
  53. read_lock(&resource_lock);
  54. for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
  55. ;
  56. return p;
  57. }
  58. static void r_stop(struct seq_file *m, void *v)
  59. __releases(resource_lock)
  60. {
  61. read_unlock(&resource_lock);
  62. }
  63. static int r_show(struct seq_file *m, void *v)
  64. {
  65. struct resource *root = m->private;
  66. struct resource *r = v, *p;
  67. int width = root->end < 0x10000 ? 4 : 8;
  68. int depth;
  69. for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
  70. if (p->parent == root)
  71. break;
  72. seq_printf(m, "%*s%0*lx-%0*lx : %s\n",
  73. depth * 2, "",
  74. width, r->start,
  75. width, r->end,
  76. r->name ? r->name : "<BAD>");
  77. return 0;
  78. }
  79. static struct seq_operations resource_op = {
  80. .start = r_start,
  81. .next = r_next,
  82. .stop = r_stop,
  83. .show = r_show,
  84. };
  85. static int ioports_open(struct inode *inode, struct file *file)
  86. {
  87. int res = seq_open(file, &resource_op);
  88. if (!res) {
  89. struct seq_file *m = file->private_data;
  90. m->private = &ioport_resource;
  91. }
  92. return res;
  93. }
  94. static int iomem_open(struct inode *inode, struct file *file)
  95. {
  96. int res = seq_open(file, &resource_op);
  97. if (!res) {
  98. struct seq_file *m = file->private_data;
  99. m->private = &iomem_resource;
  100. }
  101. return res;
  102. }
  103. static struct file_operations proc_ioports_operations = {
  104. .open = ioports_open,
  105. .read = seq_read,
  106. .llseek = seq_lseek,
  107. .release = seq_release,
  108. };
  109. static struct file_operations proc_iomem_operations = {
  110. .open = iomem_open,
  111. .read = seq_read,
  112. .llseek = seq_lseek,
  113. .release = seq_release,
  114. };
  115. static int __init ioresources_init(void)
  116. {
  117. struct proc_dir_entry *entry;
  118. entry = create_proc_entry("ioports", 0, NULL);
  119. if (entry)
  120. entry->proc_fops = &proc_ioports_operations;
  121. entry = create_proc_entry("iomem", 0, NULL);
  122. if (entry)
  123. entry->proc_fops = &proc_iomem_operations;
  124. return 0;
  125. }
  126. __initcall(ioresources_init);
  127. #endif /* CONFIG_PROC_FS */
  128. /* Return the conflict entry if you can't request it */
  129. static struct resource * __request_resource(struct resource *root, struct resource *new)
  130. {
  131. unsigned long start = new->start;
  132. unsigned long end = new->end;
  133. struct resource *tmp, **p;
  134. if (end < start)
  135. return root;
  136. if (start < root->start)
  137. return root;
  138. if (end > root->end)
  139. return root;
  140. p = &root->child;
  141. for (;;) {
  142. tmp = *p;
  143. if (!tmp || tmp->start > end) {
  144. new->sibling = tmp;
  145. *p = new;
  146. new->parent = root;
  147. return NULL;
  148. }
  149. p = &tmp->sibling;
  150. if (tmp->end < start)
  151. continue;
  152. return tmp;
  153. }
  154. }
  155. static int __release_resource(struct resource *old)
  156. {
  157. struct resource *tmp, **p;
  158. p = &old->parent->child;
  159. for (;;) {
  160. tmp = *p;
  161. if (!tmp)
  162. break;
  163. if (tmp == old) {
  164. *p = tmp->sibling;
  165. old->parent = NULL;
  166. return 0;
  167. }
  168. p = &tmp->sibling;
  169. }
  170. return -EINVAL;
  171. }
  172. int request_resource(struct resource *root, struct resource *new)
  173. {
  174. struct resource *conflict;
  175. write_lock(&resource_lock);
  176. conflict = __request_resource(root, new);
  177. write_unlock(&resource_lock);
  178. return conflict ? -EBUSY : 0;
  179. }
  180. EXPORT_SYMBOL(request_resource);
  181. struct resource *____request_resource(struct resource *root, struct resource *new)
  182. {
  183. struct resource *conflict;
  184. write_lock(&resource_lock);
  185. conflict = __request_resource(root, new);
  186. write_unlock(&resource_lock);
  187. return conflict;
  188. }
  189. EXPORT_SYMBOL(____request_resource);
  190. int release_resource(struct resource *old)
  191. {
  192. int retval;
  193. write_lock(&resource_lock);
  194. retval = __release_resource(old);
  195. write_unlock(&resource_lock);
  196. return retval;
  197. }
  198. EXPORT_SYMBOL(release_resource);
  199. /*
  200. * Find empty slot in the resource tree given range and alignment.
  201. */
  202. static int find_resource(struct resource *root, struct resource *new,
  203. unsigned long size,
  204. unsigned long min, unsigned long max,
  205. unsigned long align,
  206. void (*alignf)(void *, struct resource *,
  207. unsigned long, unsigned long),
  208. void *alignf_data)
  209. {
  210. struct resource *this = root->child;
  211. new->start = root->start;
  212. /*
  213. * Skip past an allocated resource that starts at 0, since the assignment
  214. * of this->start - 1 to new->end below would cause an underflow.
  215. */
  216. if (this && this->start == 0) {
  217. new->start = this->end + 1;
  218. this = this->sibling;
  219. }
  220. for(;;) {
  221. if (this)
  222. new->end = this->start - 1;
  223. else
  224. new->end = root->end;
  225. if (new->start < min)
  226. new->start = min;
  227. if (new->end > max)
  228. new->end = max;
  229. new->start = ALIGN(new->start, align);
  230. if (alignf)
  231. alignf(alignf_data, new, size, align);
  232. if (new->start < new->end && new->end - new->start >= size - 1) {
  233. new->end = new->start + size - 1;
  234. return 0;
  235. }
  236. if (!this)
  237. break;
  238. new->start = this->end + 1;
  239. this = this->sibling;
  240. }
  241. return -EBUSY;
  242. }
  243. /*
  244. * Allocate empty slot in the resource tree given range and alignment.
  245. */
  246. int allocate_resource(struct resource *root, struct resource *new,
  247. unsigned long size,
  248. unsigned long min, unsigned long max,
  249. unsigned long align,
  250. void (*alignf)(void *, struct resource *,
  251. unsigned long, unsigned long),
  252. void *alignf_data)
  253. {
  254. int err;
  255. write_lock(&resource_lock);
  256. err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
  257. if (err >= 0 && __request_resource(root, new))
  258. err = -EBUSY;
  259. write_unlock(&resource_lock);
  260. return err;
  261. }
  262. EXPORT_SYMBOL(allocate_resource);
  263. /**
  264. * insert_resource - Inserts a resource in the resource tree
  265. * @parent: parent of the new resource
  266. * @new: new resource to insert
  267. *
  268. * Returns 0 on success, -EBUSY if the resource can't be inserted.
  269. *
  270. * This function is equivalent of request_resource when no conflict
  271. * happens. If a conflict happens, and the conflicting resources
  272. * entirely fit within the range of the new resource, then the new
  273. * resource is inserted and the conflicting resources become childs of
  274. * the new resource. Otherwise the new resource becomes the child of
  275. * the conflicting resource
  276. */
  277. int insert_resource(struct resource *parent, struct resource *new)
  278. {
  279. int result;
  280. struct resource *first, *next;
  281. write_lock(&resource_lock);
  282. begin:
  283. result = 0;
  284. first = __request_resource(parent, new);
  285. if (!first)
  286. goto out;
  287. result = -EBUSY;
  288. if (first == parent)
  289. goto out;
  290. /* Resource fully contained by the clashing resource? Recurse into it */
  291. if (first->start <= new->start && first->end >= new->end) {
  292. parent = first;
  293. goto begin;
  294. }
  295. for (next = first; ; next = next->sibling) {
  296. /* Partial overlap? Bad, and unfixable */
  297. if (next->start < new->start || next->end > new->end)
  298. goto out;
  299. if (!next->sibling)
  300. break;
  301. if (next->sibling->start > new->end)
  302. break;
  303. }
  304. result = 0;
  305. new->parent = parent;
  306. new->sibling = next->sibling;
  307. new->child = first;
  308. next->sibling = NULL;
  309. for (next = first; next; next = next->sibling)
  310. next->parent = new;
  311. if (parent->child == first) {
  312. parent->child = new;
  313. } else {
  314. next = parent->child;
  315. while (next->sibling != first)
  316. next = next->sibling;
  317. next->sibling = new;
  318. }
  319. out:
  320. write_unlock(&resource_lock);
  321. return result;
  322. }
  323. EXPORT_SYMBOL(insert_resource);
  324. /*
  325. * Given an existing resource, change its start and size to match the
  326. * arguments. Returns -EBUSY if it can't fit. Existing children of
  327. * the resource are assumed to be immutable.
  328. */
  329. int adjust_resource(struct resource *res, unsigned long start, unsigned long size)
  330. {
  331. struct resource *tmp, *parent = res->parent;
  332. unsigned long end = start + size - 1;
  333. int result = -EBUSY;
  334. write_lock(&resource_lock);
  335. if ((start < parent->start) || (end > parent->end))
  336. goto out;
  337. for (tmp = res->child; tmp; tmp = tmp->sibling) {
  338. if ((tmp->start < start) || (tmp->end > end))
  339. goto out;
  340. }
  341. if (res->sibling && (res->sibling->start <= end))
  342. goto out;
  343. tmp = parent->child;
  344. if (tmp != res) {
  345. while (tmp->sibling != res)
  346. tmp = tmp->sibling;
  347. if (start <= tmp->end)
  348. goto out;
  349. }
  350. res->start = start;
  351. res->end = end;
  352. result = 0;
  353. out:
  354. write_unlock(&resource_lock);
  355. return result;
  356. }
  357. EXPORT_SYMBOL(adjust_resource);
  358. /*
  359. * This is compatibility stuff for IO resources.
  360. *
  361. * Note how this, unlike the above, knows about
  362. * the IO flag meanings (busy etc).
  363. *
  364. * Request-region creates a new busy region.
  365. *
  366. * Check-region returns non-zero if the area is already busy
  367. *
  368. * Release-region releases a matching busy region.
  369. */
  370. struct resource * __request_region(struct resource *parent, unsigned long start, unsigned long n, const char *name)
  371. {
  372. struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
  373. if (res) {
  374. res->name = name;
  375. res->start = start;
  376. res->end = start + n - 1;
  377. res->flags = IORESOURCE_BUSY;
  378. write_lock(&resource_lock);
  379. for (;;) {
  380. struct resource *conflict;
  381. conflict = __request_resource(parent, res);
  382. if (!conflict)
  383. break;
  384. if (conflict != parent) {
  385. parent = conflict;
  386. if (!(conflict->flags & IORESOURCE_BUSY))
  387. continue;
  388. }
  389. /* Uhhuh, that didn't work out.. */
  390. kfree(res);
  391. res = NULL;
  392. break;
  393. }
  394. write_unlock(&resource_lock);
  395. }
  396. return res;
  397. }
  398. EXPORT_SYMBOL(__request_region);
  399. int __check_region(struct resource *parent, unsigned long start, unsigned long n)
  400. {
  401. struct resource * res;
  402. res = __request_region(parent, start, n, "check-region");
  403. if (!res)
  404. return -EBUSY;
  405. release_resource(res);
  406. kfree(res);
  407. return 0;
  408. }
  409. EXPORT_SYMBOL(__check_region);
  410. void __release_region(struct resource *parent, unsigned long start, unsigned long n)
  411. {
  412. struct resource **p;
  413. unsigned long end;
  414. p = &parent->child;
  415. end = start + n - 1;
  416. write_lock(&resource_lock);
  417. for (;;) {
  418. struct resource *res = *p;
  419. if (!res)
  420. break;
  421. if (res->start <= start && res->end >= end) {
  422. if (!(res->flags & IORESOURCE_BUSY)) {
  423. p = &res->child;
  424. continue;
  425. }
  426. if (res->start != start || res->end != end)
  427. break;
  428. *p = res->sibling;
  429. write_unlock(&resource_lock);
  430. kfree(res);
  431. return;
  432. }
  433. p = &res->sibling;
  434. }
  435. write_unlock(&resource_lock);
  436. printk(KERN_WARNING "Trying to free nonexistent resource <%08lx-%08lx>\n", start, end);
  437. }
  438. EXPORT_SYMBOL(__release_region);
  439. /*
  440. * Called from init/main.c to reserve IO ports.
  441. */
  442. #define MAXRESERVE 4
  443. static int __init reserve_setup(char *str)
  444. {
  445. static int reserved;
  446. static struct resource reserve[MAXRESERVE];
  447. for (;;) {
  448. int io_start, io_num;
  449. int x = reserved;
  450. if (get_option (&str, &io_start) != 2)
  451. break;
  452. if (get_option (&str, &io_num) == 0)
  453. break;
  454. if (x < MAXRESERVE) {
  455. struct resource *res = reserve + x;
  456. res->name = "reserved";
  457. res->start = io_start;
  458. res->end = io_start + io_num - 1;
  459. res->flags = IORESOURCE_BUSY;
  460. res->child = NULL;
  461. if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
  462. reserved = x+1;
  463. }
  464. }
  465. return 1;
  466. }
  467. __setup("reserve=", reserve_setup);