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