resource.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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. proc_create("ioports", 0, NULL, &proc_ioports_operations);
  117. proc_create("iomem", 0, NULL, &proc_iomem_operations);
  118. return 0;
  119. }
  120. __initcall(ioresources_init);
  121. #endif /* CONFIG_PROC_FS */
  122. /* Return the conflict entry if you can't request it */
  123. static struct resource * __request_resource(struct resource *root, struct resource *new)
  124. {
  125. resource_size_t start = new->start;
  126. resource_size_t end = new->end;
  127. struct resource *tmp, **p;
  128. if (end < start)
  129. return root;
  130. if (start < root->start)
  131. return root;
  132. if (end > root->end)
  133. return root;
  134. p = &root->child;
  135. for (;;) {
  136. tmp = *p;
  137. if (!tmp || tmp->start > end) {
  138. new->sibling = tmp;
  139. *p = new;
  140. new->parent = root;
  141. return NULL;
  142. }
  143. p = &tmp->sibling;
  144. if (tmp->end < start)
  145. continue;
  146. return tmp;
  147. }
  148. }
  149. static int __release_resource(struct resource *old)
  150. {
  151. struct resource *tmp, **p;
  152. p = &old->parent->child;
  153. for (;;) {
  154. tmp = *p;
  155. if (!tmp)
  156. break;
  157. if (tmp == old) {
  158. *p = tmp->sibling;
  159. old->parent = NULL;
  160. return 0;
  161. }
  162. p = &tmp->sibling;
  163. }
  164. return -EINVAL;
  165. }
  166. /**
  167. * request_resource - request and reserve an I/O or memory resource
  168. * @root: root resource descriptor
  169. * @new: resource descriptor desired by caller
  170. *
  171. * Returns 0 for success, negative error code on error.
  172. */
  173. int request_resource(struct resource *root, struct resource *new)
  174. {
  175. struct resource *conflict;
  176. write_lock(&resource_lock);
  177. conflict = __request_resource(root, new);
  178. write_unlock(&resource_lock);
  179. return conflict ? -EBUSY : 0;
  180. }
  181. EXPORT_SYMBOL(request_resource);
  182. /**
  183. * release_resource - release a previously reserved resource
  184. * @old: resource pointer
  185. */
  186. int release_resource(struct resource *old)
  187. {
  188. int retval;
  189. write_lock(&resource_lock);
  190. retval = __release_resource(old);
  191. write_unlock(&resource_lock);
  192. return retval;
  193. }
  194. EXPORT_SYMBOL(release_resource);
  195. #if defined(CONFIG_MEMORY_HOTPLUG) && !defined(CONFIG_ARCH_HAS_WALK_MEMORY)
  196. /*
  197. * Finds the lowest memory reosurce exists within [res->start.res->end)
  198. * the caller must specify res->start, res->end, res->flags.
  199. * If found, returns 0, res is overwritten, if not found, returns -1.
  200. */
  201. static int find_next_system_ram(struct resource *res)
  202. {
  203. resource_size_t start, end;
  204. struct resource *p;
  205. BUG_ON(!res);
  206. start = res->start;
  207. end = res->end;
  208. BUG_ON(start >= end);
  209. read_lock(&resource_lock);
  210. for (p = iomem_resource.child; p ; p = p->sibling) {
  211. /* system ram is just marked as IORESOURCE_MEM */
  212. if (p->flags != res->flags)
  213. continue;
  214. if (p->start > end) {
  215. p = NULL;
  216. break;
  217. }
  218. if ((p->end >= start) && (p->start < end))
  219. break;
  220. }
  221. read_unlock(&resource_lock);
  222. if (!p)
  223. return -1;
  224. /* copy data */
  225. if (res->start < p->start)
  226. res->start = p->start;
  227. if (res->end > p->end)
  228. res->end = p->end;
  229. return 0;
  230. }
  231. int
  232. walk_memory_resource(unsigned long start_pfn, unsigned long nr_pages, void *arg,
  233. int (*func)(unsigned long, unsigned long, void *))
  234. {
  235. struct resource res;
  236. unsigned long pfn, len;
  237. u64 orig_end;
  238. int ret = -1;
  239. res.start = (u64) start_pfn << PAGE_SHIFT;
  240. res.end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
  241. res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  242. orig_end = res.end;
  243. while ((res.start < res.end) && (find_next_system_ram(&res) >= 0)) {
  244. pfn = (unsigned long)(res.start >> PAGE_SHIFT);
  245. len = (unsigned long)((res.end + 1 - res.start) >> PAGE_SHIFT);
  246. ret = (*func)(pfn, len, arg);
  247. if (ret)
  248. break;
  249. res.start = res.end + 1;
  250. res.end = orig_end;
  251. }
  252. return ret;
  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. * resource_alignment - calculate resource's alignment
  425. * @res: resource pointer
  426. *
  427. * Returns alignment on success, 0 (invalid alignment) on failure.
  428. */
  429. resource_size_t resource_alignment(struct resource *res)
  430. {
  431. switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
  432. case IORESOURCE_SIZEALIGN:
  433. return res->end - res->start + 1;
  434. case IORESOURCE_STARTALIGN:
  435. return res->start;
  436. default:
  437. return 0;
  438. }
  439. }
  440. /*
  441. * This is compatibility stuff for IO resources.
  442. *
  443. * Note how this, unlike the above, knows about
  444. * the IO flag meanings (busy etc).
  445. *
  446. * request_region creates a new busy region.
  447. *
  448. * check_region returns non-zero if the area is already busy.
  449. *
  450. * release_region releases a matching busy region.
  451. */
  452. /**
  453. * __request_region - create a new busy resource region
  454. * @parent: parent resource descriptor
  455. * @start: resource start address
  456. * @n: resource region size
  457. * @name: reserving caller's ID string
  458. */
  459. struct resource * __request_region(struct resource *parent,
  460. resource_size_t start, resource_size_t n,
  461. const char *name)
  462. {
  463. struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
  464. if (res) {
  465. res->name = name;
  466. res->start = start;
  467. res->end = start + n - 1;
  468. res->flags = IORESOURCE_BUSY;
  469. write_lock(&resource_lock);
  470. for (;;) {
  471. struct resource *conflict;
  472. conflict = __request_resource(parent, res);
  473. if (!conflict)
  474. break;
  475. if (conflict != parent) {
  476. parent = conflict;
  477. if (!(conflict->flags & IORESOURCE_BUSY))
  478. continue;
  479. }
  480. /* Uhhuh, that didn't work out.. */
  481. kfree(res);
  482. res = NULL;
  483. break;
  484. }
  485. write_unlock(&resource_lock);
  486. }
  487. return res;
  488. }
  489. EXPORT_SYMBOL(__request_region);
  490. /**
  491. * __check_region - check if a resource region is busy or free
  492. * @parent: parent resource descriptor
  493. * @start: resource start address
  494. * @n: resource region size
  495. *
  496. * Returns 0 if the region is free at the moment it is checked,
  497. * returns %-EBUSY if the region is busy.
  498. *
  499. * NOTE:
  500. * This function is deprecated because its use is racy.
  501. * Even if it returns 0, a subsequent call to request_region()
  502. * may fail because another driver etc. just allocated the region.
  503. * Do NOT use it. It will be removed from the kernel.
  504. */
  505. int __check_region(struct resource *parent, resource_size_t start,
  506. resource_size_t n)
  507. {
  508. struct resource * res;
  509. res = __request_region(parent, start, n, "check-region");
  510. if (!res)
  511. return -EBUSY;
  512. release_resource(res);
  513. kfree(res);
  514. return 0;
  515. }
  516. EXPORT_SYMBOL(__check_region);
  517. /**
  518. * __release_region - release a previously reserved resource region
  519. * @parent: parent resource descriptor
  520. * @start: resource start address
  521. * @n: resource region size
  522. *
  523. * The described resource region must match a currently busy region.
  524. */
  525. void __release_region(struct resource *parent, resource_size_t start,
  526. resource_size_t n)
  527. {
  528. struct resource **p;
  529. resource_size_t end;
  530. p = &parent->child;
  531. end = start + n - 1;
  532. write_lock(&resource_lock);
  533. for (;;) {
  534. struct resource *res = *p;
  535. if (!res)
  536. break;
  537. if (res->start <= start && res->end >= end) {
  538. if (!(res->flags & IORESOURCE_BUSY)) {
  539. p = &res->child;
  540. continue;
  541. }
  542. if (res->start != start || res->end != end)
  543. break;
  544. *p = res->sibling;
  545. write_unlock(&resource_lock);
  546. kfree(res);
  547. return;
  548. }
  549. p = &res->sibling;
  550. }
  551. write_unlock(&resource_lock);
  552. printk(KERN_WARNING "Trying to free nonexistent resource "
  553. "<%016llx-%016llx>\n", (unsigned long long)start,
  554. (unsigned long long)end);
  555. }
  556. EXPORT_SYMBOL(__release_region);
  557. /*
  558. * Managed region resource
  559. */
  560. struct region_devres {
  561. struct resource *parent;
  562. resource_size_t start;
  563. resource_size_t n;
  564. };
  565. static void devm_region_release(struct device *dev, void *res)
  566. {
  567. struct region_devres *this = res;
  568. __release_region(this->parent, this->start, this->n);
  569. }
  570. static int devm_region_match(struct device *dev, void *res, void *match_data)
  571. {
  572. struct region_devres *this = res, *match = match_data;
  573. return this->parent == match->parent &&
  574. this->start == match->start && this->n == match->n;
  575. }
  576. struct resource * __devm_request_region(struct device *dev,
  577. struct resource *parent, resource_size_t start,
  578. resource_size_t n, const char *name)
  579. {
  580. struct region_devres *dr = NULL;
  581. struct resource *res;
  582. dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
  583. GFP_KERNEL);
  584. if (!dr)
  585. return NULL;
  586. dr->parent = parent;
  587. dr->start = start;
  588. dr->n = n;
  589. res = __request_region(parent, start, n, name);
  590. if (res)
  591. devres_add(dev, dr);
  592. else
  593. devres_free(dr);
  594. return res;
  595. }
  596. EXPORT_SYMBOL(__devm_request_region);
  597. void __devm_release_region(struct device *dev, struct resource *parent,
  598. resource_size_t start, resource_size_t n)
  599. {
  600. struct region_devres match_data = { parent, start, n };
  601. __release_region(parent, start, n);
  602. WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
  603. &match_data));
  604. }
  605. EXPORT_SYMBOL(__devm_release_region);
  606. /*
  607. * Called from init/main.c to reserve IO ports.
  608. */
  609. #define MAXRESERVE 4
  610. static int __init reserve_setup(char *str)
  611. {
  612. static int reserved;
  613. static struct resource reserve[MAXRESERVE];
  614. for (;;) {
  615. int io_start, io_num;
  616. int x = reserved;
  617. if (get_option (&str, &io_start) != 2)
  618. break;
  619. if (get_option (&str, &io_num) == 0)
  620. break;
  621. if (x < MAXRESERVE) {
  622. struct resource *res = reserve + x;
  623. res->name = "reserved";
  624. res->start = io_start;
  625. res->end = io_start + io_num - 1;
  626. res->flags = IORESOURCE_BUSY;
  627. res->child = NULL;
  628. if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
  629. reserved = x+1;
  630. }
  631. }
  632. return 1;
  633. }
  634. __setup("reserve=", reserve_setup);