resource.c 16 KB

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