resource.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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 = 0,
  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 = 0,
  31. .end = -1,
  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*llx-%0*llx : %s\n",
  73. depth * 2, "",
  74. width, (unsigned long long) r->start,
  75. width, (unsigned long long) 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. resource_size_t start = new->start;
  132. resource_size_t 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. resource_size_t size, resource_size_t min,
  238. resource_size_t max, resource_size_t align,
  239. void (*alignf)(void *, struct resource *,
  240. resource_size_t, resource_size_t),
  241. void *alignf_data)
  242. {
  243. struct resource *this = root->child;
  244. new->start = root->start;
  245. /*
  246. * Skip past an allocated resource that starts at 0, since the assignment
  247. * of this->start - 1 to new->end below would cause an underflow.
  248. */
  249. if (this && this->start == 0) {
  250. new->start = this->end + 1;
  251. this = this->sibling;
  252. }
  253. for(;;) {
  254. if (this)
  255. new->end = this->start - 1;
  256. else
  257. new->end = root->end;
  258. if (new->start < min)
  259. new->start = min;
  260. if (new->end > max)
  261. new->end = max;
  262. new->start = ALIGN(new->start, align);
  263. if (alignf)
  264. alignf(alignf_data, new, size, align);
  265. if (new->start < new->end && new->end - new->start >= size - 1) {
  266. new->end = new->start + size - 1;
  267. return 0;
  268. }
  269. if (!this)
  270. break;
  271. new->start = this->end + 1;
  272. this = this->sibling;
  273. }
  274. return -EBUSY;
  275. }
  276. /*
  277. * Allocate empty slot in the resource tree given range and alignment.
  278. */
  279. int allocate_resource(struct resource *root, struct resource *new,
  280. resource_size_t size, resource_size_t min,
  281. resource_size_t max, resource_size_t align,
  282. void (*alignf)(void *, struct resource *,
  283. resource_size_t, resource_size_t),
  284. void *alignf_data)
  285. {
  286. int err;
  287. write_lock(&resource_lock);
  288. err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
  289. if (err >= 0 && __request_resource(root, new))
  290. err = -EBUSY;
  291. write_unlock(&resource_lock);
  292. return err;
  293. }
  294. EXPORT_SYMBOL(allocate_resource);
  295. /**
  296. * insert_resource - Inserts a resource in the resource tree
  297. * @parent: parent of the new resource
  298. * @new: new resource to insert
  299. *
  300. * Returns 0 on success, -EBUSY if the resource can't be inserted.
  301. *
  302. * This function is equivalent of request_resource when no conflict
  303. * happens. If a conflict happens, and the conflicting resources
  304. * entirely fit within the range of the new resource, then the new
  305. * resource is inserted and the conflicting resources become childs of
  306. * the new resource. Otherwise the new resource becomes the child of
  307. * the conflicting resource
  308. */
  309. int insert_resource(struct resource *parent, struct resource *new)
  310. {
  311. int result;
  312. struct resource *first, *next;
  313. write_lock(&resource_lock);
  314. begin:
  315. result = 0;
  316. first = __request_resource(parent, new);
  317. if (!first)
  318. goto out;
  319. result = -EBUSY;
  320. if (first == parent)
  321. goto out;
  322. /* Resource fully contained by the clashing resource? Recurse into it */
  323. if (first->start <= new->start && first->end >= new->end) {
  324. parent = first;
  325. goto begin;
  326. }
  327. for (next = first; ; next = next->sibling) {
  328. /* Partial overlap? Bad, and unfixable */
  329. if (next->start < new->start || next->end > new->end)
  330. goto out;
  331. if (!next->sibling)
  332. break;
  333. if (next->sibling->start > new->end)
  334. break;
  335. }
  336. result = 0;
  337. new->parent = parent;
  338. new->sibling = next->sibling;
  339. new->child = first;
  340. next->sibling = NULL;
  341. for (next = first; next; next = next->sibling)
  342. next->parent = new;
  343. if (parent->child == first) {
  344. parent->child = new;
  345. } else {
  346. next = parent->child;
  347. while (next->sibling != first)
  348. next = next->sibling;
  349. next->sibling = new;
  350. }
  351. out:
  352. write_unlock(&resource_lock);
  353. return result;
  354. }
  355. EXPORT_SYMBOL(insert_resource);
  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);