resource.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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/module.h>
  10. #include <linux/sched.h>
  11. #include <linux/errno.h>
  12. #include <linux/ioport.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/fs.h>
  17. #include <linux/proc_fs.h>
  18. #include <linux/seq_file.h>
  19. #include <asm/io.h>
  20. struct resource ioport_resource = {
  21. .name = "PCI IO",
  22. .start = 0,
  23. .end = IO_SPACE_LIMIT,
  24. .flags = IORESOURCE_IO,
  25. };
  26. EXPORT_SYMBOL(ioport_resource);
  27. struct resource iomem_resource = {
  28. .name = "PCI mem",
  29. .start = 0,
  30. .end = -1,
  31. .flags = IORESOURCE_MEM,
  32. };
  33. EXPORT_SYMBOL(iomem_resource);
  34. static DEFINE_RWLOCK(resource_lock);
  35. #ifdef CONFIG_PROC_FS
  36. enum { MAX_IORES_LEVEL = 5 };
  37. static void *r_next(struct seq_file *m, void *v, loff_t *pos)
  38. {
  39. struct resource *p = v;
  40. (*pos)++;
  41. if (p->child)
  42. return p->child;
  43. while (!p->sibling && p->parent)
  44. p = p->parent;
  45. return p->sibling;
  46. }
  47. static void *r_start(struct seq_file *m, loff_t *pos)
  48. __acquires(resource_lock)
  49. {
  50. struct resource *p = m->private;
  51. loff_t l = 0;
  52. read_lock(&resource_lock);
  53. for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
  54. ;
  55. return p;
  56. }
  57. static void r_stop(struct seq_file *m, void *v)
  58. __releases(resource_lock)
  59. {
  60. read_unlock(&resource_lock);
  61. }
  62. static int r_show(struct seq_file *m, void *v)
  63. {
  64. struct resource *root = m->private;
  65. struct resource *r = v, *p;
  66. int width = root->end < 0x10000 ? 4 : 8;
  67. int depth;
  68. for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
  69. if (p->parent == root)
  70. break;
  71. seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
  72. depth * 2, "",
  73. width, (unsigned long long) r->start,
  74. width, (unsigned long long) r->end,
  75. r->name ? r->name : "<BAD>");
  76. return 0;
  77. }
  78. static struct seq_operations resource_op = {
  79. .start = r_start,
  80. .next = r_next,
  81. .stop = r_stop,
  82. .show = r_show,
  83. };
  84. static int ioports_open(struct inode *inode, struct file *file)
  85. {
  86. int res = seq_open(file, &resource_op);
  87. if (!res) {
  88. struct seq_file *m = file->private_data;
  89. m->private = &ioport_resource;
  90. }
  91. return res;
  92. }
  93. static int iomem_open(struct inode *inode, struct file *file)
  94. {
  95. int res = seq_open(file, &resource_op);
  96. if (!res) {
  97. struct seq_file *m = file->private_data;
  98. m->private = &iomem_resource;
  99. }
  100. return res;
  101. }
  102. static struct file_operations proc_ioports_operations = {
  103. .open = ioports_open,
  104. .read = seq_read,
  105. .llseek = seq_lseek,
  106. .release = seq_release,
  107. };
  108. static struct file_operations proc_iomem_operations = {
  109. .open = iomem_open,
  110. .read = seq_read,
  111. .llseek = seq_lseek,
  112. .release = seq_release,
  113. };
  114. static int __init ioresources_init(void)
  115. {
  116. struct proc_dir_entry *entry;
  117. entry = create_proc_entry("ioports", 0, NULL);
  118. if (entry)
  119. entry->proc_fops = &proc_ioports_operations;
  120. entry = create_proc_entry("iomem", 0, NULL);
  121. if (entry)
  122. entry->proc_fops = &proc_iomem_operations;
  123. return 0;
  124. }
  125. __initcall(ioresources_init);
  126. #endif /* CONFIG_PROC_FS */
  127. /* Return the conflict entry if you can't request it */
  128. static struct resource * __request_resource(struct resource *root, struct resource *new)
  129. {
  130. resource_size_t start = new->start;
  131. resource_size_t end = new->end;
  132. struct resource *tmp, **p;
  133. if (end < start)
  134. return root;
  135. if (start < root->start)
  136. return root;
  137. if (end > root->end)
  138. return root;
  139. p = &root->child;
  140. for (;;) {
  141. tmp = *p;
  142. if (!tmp || tmp->start > end) {
  143. new->sibling = tmp;
  144. *p = new;
  145. new->parent = root;
  146. return NULL;
  147. }
  148. p = &tmp->sibling;
  149. if (tmp->end < start)
  150. continue;
  151. return tmp;
  152. }
  153. }
  154. static int __release_resource(struct resource *old)
  155. {
  156. struct resource *tmp, **p;
  157. p = &old->parent->child;
  158. for (;;) {
  159. tmp = *p;
  160. if (!tmp)
  161. break;
  162. if (tmp == old) {
  163. *p = tmp->sibling;
  164. old->parent = NULL;
  165. return 0;
  166. }
  167. p = &tmp->sibling;
  168. }
  169. return -EINVAL;
  170. }
  171. int request_resource(struct resource *root, struct resource *new)
  172. {
  173. struct resource *conflict;
  174. write_lock(&resource_lock);
  175. conflict = __request_resource(root, new);
  176. write_unlock(&resource_lock);
  177. return conflict ? -EBUSY : 0;
  178. }
  179. EXPORT_SYMBOL(request_resource);
  180. struct resource *____request_resource(struct resource *root, struct resource *new)
  181. {
  182. struct resource *conflict;
  183. write_lock(&resource_lock);
  184. conflict = __request_resource(root, new);
  185. write_unlock(&resource_lock);
  186. return conflict;
  187. }
  188. EXPORT_SYMBOL(____request_resource);
  189. int release_resource(struct resource *old)
  190. {
  191. int retval;
  192. write_lock(&resource_lock);
  193. retval = __release_resource(old);
  194. write_unlock(&resource_lock);
  195. return retval;
  196. }
  197. EXPORT_SYMBOL(release_resource);
  198. #ifdef CONFIG_MEMORY_HOTPLUG
  199. /*
  200. * Finds the lowest memory reosurce exists within [res->start.res->end)
  201. * the caller must specify res->start, res->end, res->flags.
  202. * If found, returns 0, res is overwritten, if not found, returns -1.
  203. */
  204. int find_next_system_ram(struct resource *res)
  205. {
  206. resource_size_t start, end;
  207. struct resource *p;
  208. BUG_ON(!res);
  209. start = res->start;
  210. end = res->end;
  211. read_lock(&resource_lock);
  212. for (p = iomem_resource.child; p ; p = p->sibling) {
  213. /* system ram is just marked as IORESOURCE_MEM */
  214. if (p->flags != res->flags)
  215. continue;
  216. if (p->start > end) {
  217. p = NULL;
  218. break;
  219. }
  220. if (p->start >= start)
  221. break;
  222. }
  223. read_unlock(&resource_lock);
  224. if (!p)
  225. return -1;
  226. /* copy data */
  227. if (res->start < p->start)
  228. res->start = p->start;
  229. if (res->end > p->end)
  230. res->end = p->end;
  231. return 0;
  232. }
  233. #endif
  234. /*
  235. * Find empty slot in the resource tree given range and alignment.
  236. */
  237. static int find_resource(struct resource *root, struct resource *new,
  238. resource_size_t size, resource_size_t min,
  239. resource_size_t max, resource_size_t align,
  240. void (*alignf)(void *, struct resource *,
  241. resource_size_t, resource_size_t),
  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. resource_size_t size, resource_size_t min,
  282. resource_size_t max, resource_size_t align,
  283. void (*alignf)(void *, struct resource *,
  284. resource_size_t, resource_size_t),
  285. void *alignf_data)
  286. {
  287. int err;
  288. write_lock(&resource_lock);
  289. err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
  290. if (err >= 0 && __request_resource(root, new))
  291. err = -EBUSY;
  292. write_unlock(&resource_lock);
  293. return err;
  294. }
  295. EXPORT_SYMBOL(allocate_resource);
  296. /**
  297. * insert_resource - Inserts a resource in the resource tree
  298. * @parent: parent of the new resource
  299. * @new: new resource to insert
  300. *
  301. * Returns 0 on success, -EBUSY if the resource can't be inserted.
  302. *
  303. * This function is equivalent of request_resource when no conflict
  304. * happens. If a conflict happens, and the conflicting resources
  305. * entirely fit within the range of the new resource, then the new
  306. * resource is inserted and the conflicting resources become childs of
  307. * the new resource. Otherwise the new resource becomes the child of
  308. * the conflicting resource
  309. */
  310. int insert_resource(struct resource *parent, struct resource *new)
  311. {
  312. int result;
  313. struct resource *first, *next;
  314. write_lock(&resource_lock);
  315. begin:
  316. result = 0;
  317. first = __request_resource(parent, new);
  318. if (!first)
  319. goto out;
  320. result = -EBUSY;
  321. if (first == parent)
  322. goto out;
  323. /* Resource fully contained by the clashing resource? Recurse into it */
  324. if (first->start <= new->start && first->end >= new->end) {
  325. parent = first;
  326. goto begin;
  327. }
  328. for (next = first; ; next = next->sibling) {
  329. /* Partial overlap? Bad, and unfixable */
  330. if (next->start < new->start || next->end > new->end)
  331. goto out;
  332. if (!next->sibling)
  333. break;
  334. if (next->sibling->start > new->end)
  335. break;
  336. }
  337. result = 0;
  338. new->parent = parent;
  339. new->sibling = next->sibling;
  340. new->child = first;
  341. next->sibling = NULL;
  342. for (next = first; next; next = next->sibling)
  343. next->parent = new;
  344. if (parent->child == first) {
  345. parent->child = new;
  346. } else {
  347. next = parent->child;
  348. while (next->sibling != first)
  349. next = next->sibling;
  350. next->sibling = new;
  351. }
  352. out:
  353. write_unlock(&resource_lock);
  354. return result;
  355. }
  356. /*
  357. * Given an existing resource, change its start and size to match the
  358. * arguments. Returns -EBUSY if it can't fit. Existing children of
  359. * the resource are assumed to be immutable.
  360. */
  361. int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
  362. {
  363. struct resource *tmp, *parent = res->parent;
  364. resource_size_t end = start + size - 1;
  365. int result = -EBUSY;
  366. write_lock(&resource_lock);
  367. if ((start < parent->start) || (end > parent->end))
  368. goto out;
  369. for (tmp = res->child; tmp; tmp = tmp->sibling) {
  370. if ((tmp->start < start) || (tmp->end > end))
  371. goto out;
  372. }
  373. if (res->sibling && (res->sibling->start <= end))
  374. goto out;
  375. tmp = parent->child;
  376. if (tmp != res) {
  377. while (tmp->sibling != res)
  378. tmp = tmp->sibling;
  379. if (start <= tmp->end)
  380. goto out;
  381. }
  382. res->start = start;
  383. res->end = end;
  384. result = 0;
  385. out:
  386. write_unlock(&resource_lock);
  387. return result;
  388. }
  389. EXPORT_SYMBOL(adjust_resource);
  390. /*
  391. * This is compatibility stuff for IO resources.
  392. *
  393. * Note how this, unlike the above, knows about
  394. * the IO flag meanings (busy etc).
  395. *
  396. * Request-region creates a new busy region.
  397. *
  398. * Check-region returns non-zero if the area is already busy
  399. *
  400. * Release-region releases a matching busy region.
  401. */
  402. struct resource * __request_region(struct resource *parent,
  403. resource_size_t start, resource_size_t n,
  404. 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, resource_size_t start,
  434. resource_size_t n)
  435. {
  436. struct resource * res;
  437. res = __request_region(parent, start, n, "check-region");
  438. if (!res)
  439. return -EBUSY;
  440. release_resource(res);
  441. kfree(res);
  442. return 0;
  443. }
  444. EXPORT_SYMBOL(__check_region);
  445. void __release_region(struct resource *parent, resource_size_t start,
  446. resource_size_t n)
  447. {
  448. struct resource **p;
  449. resource_size_t end;
  450. p = &parent->child;
  451. end = start + n - 1;
  452. write_lock(&resource_lock);
  453. for (;;) {
  454. struct resource *res = *p;
  455. if (!res)
  456. break;
  457. if (res->start <= start && res->end >= end) {
  458. if (!(res->flags & IORESOURCE_BUSY)) {
  459. p = &res->child;
  460. continue;
  461. }
  462. if (res->start != start || res->end != end)
  463. break;
  464. *p = res->sibling;
  465. write_unlock(&resource_lock);
  466. kfree(res);
  467. return;
  468. }
  469. p = &res->sibling;
  470. }
  471. write_unlock(&resource_lock);
  472. printk(KERN_WARNING "Trying to free nonexistent resource "
  473. "<%016llx-%016llx>\n", (unsigned long long)start,
  474. (unsigned long long)end);
  475. }
  476. EXPORT_SYMBOL(__release_region);
  477. /*
  478. * Called from init/main.c to reserve IO ports.
  479. */
  480. #define MAXRESERVE 4
  481. static int __init reserve_setup(char *str)
  482. {
  483. static int reserved;
  484. static struct resource reserve[MAXRESERVE];
  485. for (;;) {
  486. int io_start, io_num;
  487. int x = reserved;
  488. if (get_option (&str, &io_start) != 2)
  489. break;
  490. if (get_option (&str, &io_num) == 0)
  491. break;
  492. if (x < MAXRESERVE) {
  493. struct resource *res = reserve + x;
  494. res->name = "reserved";
  495. res->start = io_start;
  496. res->end = io_start + io_num - 1;
  497. res->flags = IORESOURCE_BUSY;
  498. res->child = NULL;
  499. if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
  500. reserved = x+1;
  501. }
  502. }
  503. return 1;
  504. }
  505. __setup("reserve=", reserve_setup);