resource.c 16 KB

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