resource.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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/errno.h>
  11. #include <linux/ioport.h>
  12. #include <linux/init.h>
  13. #include <linux/slab.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/fs.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/device.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 const 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 const 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 const 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. /**
  172. * request_resource - request and reserve an I/O or memory resource
  173. * @root: root resource descriptor
  174. * @new: resource descriptor desired by caller
  175. *
  176. * Returns 0 for success, negative error code on error.
  177. */
  178. int request_resource(struct resource *root, struct resource *new)
  179. {
  180. struct resource *conflict;
  181. write_lock(&resource_lock);
  182. conflict = __request_resource(root, new);
  183. write_unlock(&resource_lock);
  184. return conflict ? -EBUSY : 0;
  185. }
  186. EXPORT_SYMBOL(request_resource);
  187. /**
  188. * release_resource - release a previously reserved resource
  189. * @old: resource pointer
  190. */
  191. int release_resource(struct resource *old)
  192. {
  193. int retval;
  194. write_lock(&resource_lock);
  195. retval = __release_resource(old);
  196. write_unlock(&resource_lock);
  197. return retval;
  198. }
  199. EXPORT_SYMBOL(release_resource);
  200. #ifdef CONFIG_MEMORY_HOTPLUG
  201. /*
  202. * Finds the lowest memory reosurce exists within [res->start.res->end)
  203. * the caller must specify res->start, res->end, res->flags.
  204. * If found, returns 0, res is overwritten, if not found, returns -1.
  205. */
  206. int find_next_system_ram(struct resource *res)
  207. {
  208. resource_size_t start, end;
  209. struct resource *p;
  210. BUG_ON(!res);
  211. start = res->start;
  212. end = res->end;
  213. BUG_ON(start >= end);
  214. read_lock(&resource_lock);
  215. for (p = iomem_resource.child; p ; p = p->sibling) {
  216. /* system ram is just marked as IORESOURCE_MEM */
  217. if (p->flags != res->flags)
  218. continue;
  219. if (p->start > end) {
  220. p = NULL;
  221. break;
  222. }
  223. if ((p->end >= start) && (p->start < end))
  224. break;
  225. }
  226. read_unlock(&resource_lock);
  227. if (!p)
  228. return -1;
  229. /* copy data */
  230. if (res->start < p->start)
  231. res->start = p->start;
  232. if (res->end > p->end)
  233. res->end = p->end;
  234. return 0;
  235. }
  236. #endif
  237. /*
  238. * Find empty slot in the resource tree given range and alignment.
  239. */
  240. static int find_resource(struct resource *root, struct resource *new,
  241. resource_size_t size, resource_size_t min,
  242. resource_size_t max, resource_size_t align,
  243. void (*alignf)(void *, struct resource *,
  244. resource_size_t, resource_size_t),
  245. void *alignf_data)
  246. {
  247. struct resource *this = root->child;
  248. new->start = root->start;
  249. /*
  250. * Skip past an allocated resource that starts at 0, since the assignment
  251. * of this->start - 1 to new->end below would cause an underflow.
  252. */
  253. if (this && this->start == 0) {
  254. new->start = this->end + 1;
  255. this = this->sibling;
  256. }
  257. for(;;) {
  258. if (this)
  259. new->end = this->start - 1;
  260. else
  261. new->end = root->end;
  262. if (new->start < min)
  263. new->start = min;
  264. if (new->end > max)
  265. new->end = max;
  266. new->start = ALIGN(new->start, align);
  267. if (alignf)
  268. alignf(alignf_data, new, size, align);
  269. if (new->start < new->end && new->end - new->start >= size - 1) {
  270. new->end = new->start + size - 1;
  271. return 0;
  272. }
  273. if (!this)
  274. break;
  275. new->start = this->end + 1;
  276. this = this->sibling;
  277. }
  278. return -EBUSY;
  279. }
  280. /**
  281. * allocate_resource - allocate empty slot in the resource tree given range & alignment
  282. * @root: root resource descriptor
  283. * @new: resource descriptor desired by caller
  284. * @size: requested resource region size
  285. * @min: minimum size to allocate
  286. * @max: maximum size to allocate
  287. * @align: alignment requested, in bytes
  288. * @alignf: alignment function, optional, called if not NULL
  289. * @alignf_data: arbitrary data to pass to the @alignf function
  290. */
  291. int allocate_resource(struct resource *root, struct resource *new,
  292. resource_size_t size, resource_size_t min,
  293. resource_size_t max, resource_size_t align,
  294. void (*alignf)(void *, struct resource *,
  295. resource_size_t, resource_size_t),
  296. void *alignf_data)
  297. {
  298. int err;
  299. write_lock(&resource_lock);
  300. err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
  301. if (err >= 0 && __request_resource(root, new))
  302. err = -EBUSY;
  303. write_unlock(&resource_lock);
  304. return err;
  305. }
  306. EXPORT_SYMBOL(allocate_resource);
  307. /**
  308. * insert_resource - Inserts a resource in the resource tree
  309. * @parent: parent of the new resource
  310. * @new: new resource to insert
  311. *
  312. * Returns 0 on success, -EBUSY if the resource can't be inserted.
  313. *
  314. * This function is equivalent to request_resource when no conflict
  315. * happens. If a conflict happens, and the conflicting resources
  316. * entirely fit within the range of the new resource, then the new
  317. * resource is inserted and the conflicting resources become children of
  318. * the new resource.
  319. */
  320. int insert_resource(struct resource *parent, struct resource *new)
  321. {
  322. int result;
  323. struct resource *first, *next;
  324. write_lock(&resource_lock);
  325. for (;; parent = first) {
  326. result = 0;
  327. first = __request_resource(parent, new);
  328. if (!first)
  329. goto out;
  330. result = -EBUSY;
  331. if (first == parent)
  332. goto out;
  333. if ((first->start > new->start) || (first->end < new->end))
  334. break;
  335. if ((first->start == new->start) && (first->end == new->end))
  336. break;
  337. }
  338. for (next = first; ; next = next->sibling) {
  339. /* Partial overlap? Bad, and unfixable */
  340. if (next->start < new->start || next->end > new->end)
  341. goto out;
  342. if (!next->sibling)
  343. break;
  344. if (next->sibling->start > new->end)
  345. break;
  346. }
  347. result = 0;
  348. new->parent = parent;
  349. new->sibling = next->sibling;
  350. new->child = first;
  351. next->sibling = NULL;
  352. for (next = first; next; next = next->sibling)
  353. next->parent = new;
  354. if (parent->child == first) {
  355. parent->child = new;
  356. } else {
  357. next = parent->child;
  358. while (next->sibling != first)
  359. next = next->sibling;
  360. next->sibling = new;
  361. }
  362. out:
  363. write_unlock(&resource_lock);
  364. return result;
  365. }
  366. /**
  367. * adjust_resource - modify a resource's start and size
  368. * @res: resource to modify
  369. * @start: new start value
  370. * @size: new size
  371. *
  372. * Given an existing resource, change its start and size to match the
  373. * arguments. Returns 0 on success, -EBUSY if it can't fit.
  374. * Existing children of the resource are assumed to be immutable.
  375. */
  376. int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
  377. {
  378. struct resource *tmp, *parent = res->parent;
  379. resource_size_t end = start + size - 1;
  380. int result = -EBUSY;
  381. write_lock(&resource_lock);
  382. if ((start < parent->start) || (end > parent->end))
  383. goto out;
  384. for (tmp = res->child; tmp; tmp = tmp->sibling) {
  385. if ((tmp->start < start) || (tmp->end > end))
  386. goto out;
  387. }
  388. if (res->sibling && (res->sibling->start <= end))
  389. goto out;
  390. tmp = parent->child;
  391. if (tmp != res) {
  392. while (tmp->sibling != res)
  393. tmp = tmp->sibling;
  394. if (start <= tmp->end)
  395. goto out;
  396. }
  397. res->start = start;
  398. res->end = end;
  399. result = 0;
  400. out:
  401. write_unlock(&resource_lock);
  402. return result;
  403. }
  404. EXPORT_SYMBOL(adjust_resource);
  405. /*
  406. * This is compatibility stuff for IO resources.
  407. *
  408. * Note how this, unlike the above, knows about
  409. * the IO flag meanings (busy etc).
  410. *
  411. * request_region creates a new busy region.
  412. *
  413. * check_region returns non-zero if the area is already busy.
  414. *
  415. * release_region releases a matching busy region.
  416. */
  417. /**
  418. * __request_region - create a new busy resource region
  419. * @parent: parent resource descriptor
  420. * @start: resource start address
  421. * @n: resource region size
  422. * @name: reserving caller's ID string
  423. */
  424. struct resource * __request_region(struct resource *parent,
  425. resource_size_t start, resource_size_t n,
  426. const char *name)
  427. {
  428. struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
  429. if (res) {
  430. res->name = name;
  431. res->start = start;
  432. res->end = start + n - 1;
  433. res->flags = IORESOURCE_BUSY;
  434. write_lock(&resource_lock);
  435. for (;;) {
  436. struct resource *conflict;
  437. conflict = __request_resource(parent, res);
  438. if (!conflict)
  439. break;
  440. if (conflict != parent) {
  441. parent = conflict;
  442. if (!(conflict->flags & IORESOURCE_BUSY))
  443. continue;
  444. }
  445. /* Uhhuh, that didn't work out.. */
  446. kfree(res);
  447. res = NULL;
  448. break;
  449. }
  450. write_unlock(&resource_lock);
  451. }
  452. return res;
  453. }
  454. EXPORT_SYMBOL(__request_region);
  455. /**
  456. * __check_region - check if a resource region is busy or free
  457. * @parent: parent resource descriptor
  458. * @start: resource start address
  459. * @n: resource region size
  460. *
  461. * Returns 0 if the region is free at the moment it is checked,
  462. * returns %-EBUSY if the region is busy.
  463. *
  464. * NOTE:
  465. * This function is deprecated because its use is racy.
  466. * Even if it returns 0, a subsequent call to request_region()
  467. * may fail because another driver etc. just allocated the region.
  468. * Do NOT use it. It will be removed from the kernel.
  469. */
  470. int __check_region(struct resource *parent, resource_size_t start,
  471. resource_size_t n)
  472. {
  473. struct resource * res;
  474. res = __request_region(parent, start, n, "check-region");
  475. if (!res)
  476. return -EBUSY;
  477. release_resource(res);
  478. kfree(res);
  479. return 0;
  480. }
  481. EXPORT_SYMBOL(__check_region);
  482. /**
  483. * __release_region - release a previously reserved resource region
  484. * @parent: parent resource descriptor
  485. * @start: resource start address
  486. * @n: resource region size
  487. *
  488. * The described resource region must match a currently busy region.
  489. */
  490. void __release_region(struct resource *parent, resource_size_t start,
  491. resource_size_t n)
  492. {
  493. struct resource **p;
  494. resource_size_t end;
  495. p = &parent->child;
  496. end = start + n - 1;
  497. write_lock(&resource_lock);
  498. for (;;) {
  499. struct resource *res = *p;
  500. if (!res)
  501. break;
  502. if (res->start <= start && res->end >= end) {
  503. if (!(res->flags & IORESOURCE_BUSY)) {
  504. p = &res->child;
  505. continue;
  506. }
  507. if (res->start != start || res->end != end)
  508. break;
  509. *p = res->sibling;
  510. write_unlock(&resource_lock);
  511. kfree(res);
  512. return;
  513. }
  514. p = &res->sibling;
  515. }
  516. write_unlock(&resource_lock);
  517. printk(KERN_WARNING "Trying to free nonexistent resource "
  518. "<%016llx-%016llx>\n", (unsigned long long)start,
  519. (unsigned long long)end);
  520. }
  521. EXPORT_SYMBOL(__release_region);
  522. /*
  523. * Managed region resource
  524. */
  525. struct region_devres {
  526. struct resource *parent;
  527. resource_size_t start;
  528. resource_size_t n;
  529. };
  530. static void devm_region_release(struct device *dev, void *res)
  531. {
  532. struct region_devres *this = res;
  533. __release_region(this->parent, this->start, this->n);
  534. }
  535. static int devm_region_match(struct device *dev, void *res, void *match_data)
  536. {
  537. struct region_devres *this = res, *match = match_data;
  538. return this->parent == match->parent &&
  539. this->start == match->start && this->n == match->n;
  540. }
  541. struct resource * __devm_request_region(struct device *dev,
  542. struct resource *parent, resource_size_t start,
  543. resource_size_t n, const char *name)
  544. {
  545. struct region_devres *dr = NULL;
  546. struct resource *res;
  547. dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
  548. GFP_KERNEL);
  549. if (!dr)
  550. return NULL;
  551. dr->parent = parent;
  552. dr->start = start;
  553. dr->n = n;
  554. res = __request_region(parent, start, n, name);
  555. if (res)
  556. devres_add(dev, dr);
  557. else
  558. devres_free(dr);
  559. return res;
  560. }
  561. EXPORT_SYMBOL(__devm_request_region);
  562. void __devm_release_region(struct device *dev, struct resource *parent,
  563. resource_size_t start, resource_size_t n)
  564. {
  565. struct region_devres match_data = { parent, start, n };
  566. __release_region(parent, start, n);
  567. WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
  568. &match_data));
  569. }
  570. EXPORT_SYMBOL(__devm_release_region);
  571. /*
  572. * Called from init/main.c to reserve IO ports.
  573. */
  574. #define MAXRESERVE 4
  575. static int __init reserve_setup(char *str)
  576. {
  577. static int reserved;
  578. static struct resource reserve[MAXRESERVE];
  579. for (;;) {
  580. int io_start, io_num;
  581. int x = reserved;
  582. if (get_option (&str, &io_start) != 2)
  583. break;
  584. if (get_option (&str, &io_num) == 0)
  585. break;
  586. if (x < MAXRESERVE) {
  587. struct resource *res = reserve + x;
  588. res->name = "reserved";
  589. res->start = io_start;
  590. res->end = io_start + io_num - 1;
  591. res->flags = IORESOURCE_BUSY;
  592. res->child = NULL;
  593. if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
  594. reserved = x+1;
  595. }
  596. }
  597. return 1;
  598. }
  599. __setup("reserve=", reserve_setup);