resource.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. #ifdef CONFIG_MEMORY_HOTPLUG
  200. /*
  201. * Finds the lowest memory reosurce exists within [res->start.res->end)
  202. * the caller must specify res->start, res->end, res->flags.
  203. * If found, returns 0, res is overwritten, if not found, returns -1.
  204. */
  205. int find_next_system_ram(struct resource *res)
  206. {
  207. resource_size_t start, end;
  208. struct resource *p;
  209. BUG_ON(!res);
  210. start = res->start;
  211. end = res->end;
  212. read_lock(&resource_lock);
  213. for (p = iomem_resource.child; p ; p = p->sibling) {
  214. /* system ram is just marked as IORESOURCE_MEM */
  215. if (p->flags != res->flags)
  216. continue;
  217. if (p->start > end) {
  218. p = NULL;
  219. break;
  220. }
  221. if (p->start >= start)
  222. break;
  223. }
  224. read_unlock(&resource_lock);
  225. if (!p)
  226. return -1;
  227. /* copy data */
  228. res->start = p->start;
  229. res->end = p->end;
  230. return 0;
  231. }
  232. #endif
  233. /*
  234. * Find empty slot in the resource tree given range and alignment.
  235. */
  236. static int find_resource(struct resource *root, struct resource *new,
  237. unsigned long size,
  238. unsigned long min, unsigned long max,
  239. unsigned long align,
  240. void (*alignf)(void *, struct resource *,
  241. unsigned long, unsigned long),
  242. void *alignf_data)
  243. {
  244. struct resource *this = root->child;
  245. new->start = root->start;
  246. /*
  247. * Skip past an allocated resource that starts at 0, since the assignment
  248. * of this->start - 1 to new->end below would cause an underflow.
  249. */
  250. if (this && this->start == 0) {
  251. new->start = this->end + 1;
  252. this = this->sibling;
  253. }
  254. for(;;) {
  255. if (this)
  256. new->end = this->start - 1;
  257. else
  258. new->end = root->end;
  259. if (new->start < min)
  260. new->start = min;
  261. if (new->end > max)
  262. new->end = max;
  263. new->start = ALIGN(new->start, align);
  264. if (alignf)
  265. alignf(alignf_data, new, size, align);
  266. if (new->start < new->end && new->end - new->start >= size - 1) {
  267. new->end = new->start + size - 1;
  268. return 0;
  269. }
  270. if (!this)
  271. break;
  272. new->start = this->end + 1;
  273. this = this->sibling;
  274. }
  275. return -EBUSY;
  276. }
  277. /*
  278. * Allocate empty slot in the resource tree given range and alignment.
  279. */
  280. int allocate_resource(struct resource *root, struct resource *new,
  281. unsigned long size,
  282. unsigned long min, unsigned long max,
  283. unsigned long align,
  284. void (*alignf)(void *, struct resource *,
  285. unsigned long, unsigned long),
  286. void *alignf_data)
  287. {
  288. int err;
  289. write_lock(&resource_lock);
  290. err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
  291. if (err >= 0 && __request_resource(root, new))
  292. err = -EBUSY;
  293. write_unlock(&resource_lock);
  294. return err;
  295. }
  296. EXPORT_SYMBOL(allocate_resource);
  297. /**
  298. * insert_resource - Inserts a resource in the resource tree
  299. * @parent: parent of the new resource
  300. * @new: new resource to insert
  301. *
  302. * Returns 0 on success, -EBUSY if the resource can't be inserted.
  303. *
  304. * This function is equivalent of request_resource when no conflict
  305. * happens. If a conflict happens, and the conflicting resources
  306. * entirely fit within the range of the new resource, then the new
  307. * resource is inserted and the conflicting resources become childs of
  308. * the new resource. Otherwise the new resource becomes the child of
  309. * the conflicting resource
  310. */
  311. int insert_resource(struct resource *parent, struct resource *new)
  312. {
  313. int result;
  314. struct resource *first, *next;
  315. write_lock(&resource_lock);
  316. begin:
  317. result = 0;
  318. first = __request_resource(parent, new);
  319. if (!first)
  320. goto out;
  321. result = -EBUSY;
  322. if (first == parent)
  323. goto out;
  324. /* Resource fully contained by the clashing resource? Recurse into it */
  325. if (first->start <= new->start && first->end >= new->end) {
  326. parent = first;
  327. goto begin;
  328. }
  329. for (next = first; ; next = next->sibling) {
  330. /* Partial overlap? Bad, and unfixable */
  331. if (next->start < new->start || next->end > new->end)
  332. goto out;
  333. if (!next->sibling)
  334. break;
  335. if (next->sibling->start > new->end)
  336. break;
  337. }
  338. result = 0;
  339. new->parent = parent;
  340. new->sibling = next->sibling;
  341. new->child = first;
  342. next->sibling = NULL;
  343. for (next = first; next; next = next->sibling)
  344. next->parent = new;
  345. if (parent->child == first) {
  346. parent->child = new;
  347. } else {
  348. next = parent->child;
  349. while (next->sibling != first)
  350. next = next->sibling;
  351. next->sibling = new;
  352. }
  353. out:
  354. write_unlock(&resource_lock);
  355. return result;
  356. }
  357. EXPORT_SYMBOL(insert_resource);
  358. /*
  359. * Given an existing resource, change its start and size to match the
  360. * arguments. Returns -EBUSY if it can't fit. Existing children of
  361. * the resource are assumed to be immutable.
  362. */
  363. int adjust_resource(struct resource *res, unsigned long start, unsigned long size)
  364. {
  365. struct resource *tmp, *parent = res->parent;
  366. unsigned long end = start + size - 1;
  367. int result = -EBUSY;
  368. write_lock(&resource_lock);
  369. if ((start < parent->start) || (end > parent->end))
  370. goto out;
  371. for (tmp = res->child; tmp; tmp = tmp->sibling) {
  372. if ((tmp->start < start) || (tmp->end > end))
  373. goto out;
  374. }
  375. if (res->sibling && (res->sibling->start <= end))
  376. goto out;
  377. tmp = parent->child;
  378. if (tmp != res) {
  379. while (tmp->sibling != res)
  380. tmp = tmp->sibling;
  381. if (start <= tmp->end)
  382. goto out;
  383. }
  384. res->start = start;
  385. res->end = end;
  386. result = 0;
  387. out:
  388. write_unlock(&resource_lock);
  389. return result;
  390. }
  391. EXPORT_SYMBOL(adjust_resource);
  392. /*
  393. * This is compatibility stuff for IO resources.
  394. *
  395. * Note how this, unlike the above, knows about
  396. * the IO flag meanings (busy etc).
  397. *
  398. * Request-region creates a new busy region.
  399. *
  400. * Check-region returns non-zero if the area is already busy
  401. *
  402. * Release-region releases a matching busy region.
  403. */
  404. struct resource * __request_region(struct resource *parent, unsigned long start, unsigned long n, const char *name)
  405. {
  406. struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
  407. if (res) {
  408. res->name = name;
  409. res->start = start;
  410. res->end = start + n - 1;
  411. res->flags = IORESOURCE_BUSY;
  412. write_lock(&resource_lock);
  413. for (;;) {
  414. struct resource *conflict;
  415. conflict = __request_resource(parent, res);
  416. if (!conflict)
  417. break;
  418. if (conflict != parent) {
  419. parent = conflict;
  420. if (!(conflict->flags & IORESOURCE_BUSY))
  421. continue;
  422. }
  423. /* Uhhuh, that didn't work out.. */
  424. kfree(res);
  425. res = NULL;
  426. break;
  427. }
  428. write_unlock(&resource_lock);
  429. }
  430. return res;
  431. }
  432. EXPORT_SYMBOL(__request_region);
  433. int __check_region(struct resource *parent, unsigned long start, unsigned long n)
  434. {
  435. struct resource * res;
  436. res = __request_region(parent, start, n, "check-region");
  437. if (!res)
  438. return -EBUSY;
  439. release_resource(res);
  440. kfree(res);
  441. return 0;
  442. }
  443. EXPORT_SYMBOL(__check_region);
  444. void __release_region(struct resource *parent, unsigned long start, unsigned long n)
  445. {
  446. struct resource **p;
  447. unsigned long end;
  448. p = &parent->child;
  449. end = start + n - 1;
  450. write_lock(&resource_lock);
  451. for (;;) {
  452. struct resource *res = *p;
  453. if (!res)
  454. break;
  455. if (res->start <= start && res->end >= end) {
  456. if (!(res->flags & IORESOURCE_BUSY)) {
  457. p = &res->child;
  458. continue;
  459. }
  460. if (res->start != start || res->end != end)
  461. break;
  462. *p = res->sibling;
  463. write_unlock(&resource_lock);
  464. kfree(res);
  465. return;
  466. }
  467. p = &res->sibling;
  468. }
  469. write_unlock(&resource_lock);
  470. printk(KERN_WARNING "Trying to free nonexistent resource <%08lx-%08lx>\n", start, end);
  471. }
  472. EXPORT_SYMBOL(__release_region);
  473. /*
  474. * Called from init/main.c to reserve IO ports.
  475. */
  476. #define MAXRESERVE 4
  477. static int __init reserve_setup(char *str)
  478. {
  479. static int reserved;
  480. static struct resource reserve[MAXRESERVE];
  481. for (;;) {
  482. int io_start, io_num;
  483. int x = reserved;
  484. if (get_option (&str, &io_start) != 2)
  485. break;
  486. if (get_option (&str, &io_num) == 0)
  487. break;
  488. if (x < MAXRESERVE) {
  489. struct resource *res = reserve + x;
  490. res->name = "reserved";
  491. res->start = io_start;
  492. res->end = io_start + io_num - 1;
  493. res->flags = IORESOURCE_BUSY;
  494. res->child = NULL;
  495. if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
  496. reserved = x+1;
  497. }
  498. }
  499. return 1;
  500. }
  501. __setup("reserve=", reserve_setup);