resource.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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/sched.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/device.h>
  20. #include <linux/pfn.h>
  21. #include <asm/io.h>
  22. struct resource ioport_resource = {
  23. .name = "PCI IO",
  24. .start = 0,
  25. .end = IO_SPACE_LIMIT,
  26. .flags = IORESOURCE_IO,
  27. };
  28. EXPORT_SYMBOL(ioport_resource);
  29. struct resource iomem_resource = {
  30. .name = "PCI mem",
  31. .start = 0,
  32. .end = -1,
  33. .flags = IORESOURCE_MEM,
  34. };
  35. EXPORT_SYMBOL(iomem_resource);
  36. static DEFINE_RWLOCK(resource_lock);
  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. #ifdef CONFIG_PROC_FS
  48. enum { MAX_IORES_LEVEL = 5 };
  49. static void *r_start(struct seq_file *m, loff_t *pos)
  50. __acquires(resource_lock)
  51. {
  52. struct resource *p = m->private;
  53. loff_t l = 0;
  54. read_lock(&resource_lock);
  55. for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
  56. ;
  57. return p;
  58. }
  59. static void r_stop(struct seq_file *m, void *v)
  60. __releases(resource_lock)
  61. {
  62. read_unlock(&resource_lock);
  63. }
  64. static int r_show(struct seq_file *m, void *v)
  65. {
  66. struct resource *root = m->private;
  67. struct resource *r = v, *p;
  68. int width = root->end < 0x10000 ? 4 : 8;
  69. int depth;
  70. for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
  71. if (p->parent == root)
  72. break;
  73. seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
  74. depth * 2, "",
  75. width, (unsigned long long) r->start,
  76. width, (unsigned long long) r->end,
  77. r->name ? r->name : "<BAD>");
  78. return 0;
  79. }
  80. static const struct seq_operations resource_op = {
  81. .start = r_start,
  82. .next = r_next,
  83. .stop = r_stop,
  84. .show = r_show,
  85. };
  86. static int ioports_open(struct inode *inode, struct file *file)
  87. {
  88. int res = seq_open(file, &resource_op);
  89. if (!res) {
  90. struct seq_file *m = file->private_data;
  91. m->private = &ioport_resource;
  92. }
  93. return res;
  94. }
  95. static int iomem_open(struct inode *inode, struct file *file)
  96. {
  97. int res = seq_open(file, &resource_op);
  98. if (!res) {
  99. struct seq_file *m = file->private_data;
  100. m->private = &iomem_resource;
  101. }
  102. return res;
  103. }
  104. static const struct file_operations proc_ioports_operations = {
  105. .open = ioports_open,
  106. .read = seq_read,
  107. .llseek = seq_lseek,
  108. .release = seq_release,
  109. };
  110. static const struct file_operations proc_iomem_operations = {
  111. .open = iomem_open,
  112. .read = seq_read,
  113. .llseek = seq_lseek,
  114. .release = seq_release,
  115. };
  116. static int __init ioresources_init(void)
  117. {
  118. proc_create("ioports", 0, NULL, &proc_ioports_operations);
  119. proc_create("iomem", 0, NULL, &proc_iomem_operations);
  120. return 0;
  121. }
  122. __initcall(ioresources_init);
  123. #endif /* CONFIG_PROC_FS */
  124. /* Return the conflict entry if you can't request it */
  125. static struct resource * __request_resource(struct resource *root, struct resource *new)
  126. {
  127. resource_size_t start = new->start;
  128. resource_size_t end = new->end;
  129. struct resource *tmp, **p;
  130. if (end < start)
  131. return root;
  132. if (start < root->start)
  133. return root;
  134. if (end > root->end)
  135. return root;
  136. p = &root->child;
  137. for (;;) {
  138. tmp = *p;
  139. if (!tmp || tmp->start > end) {
  140. new->sibling = tmp;
  141. *p = new;
  142. new->parent = root;
  143. return NULL;
  144. }
  145. p = &tmp->sibling;
  146. if (tmp->end < start)
  147. continue;
  148. return tmp;
  149. }
  150. }
  151. static int __release_resource(struct resource *old)
  152. {
  153. struct resource *tmp, **p;
  154. p = &old->parent->child;
  155. for (;;) {
  156. tmp = *p;
  157. if (!tmp)
  158. break;
  159. if (tmp == old) {
  160. *p = tmp->sibling;
  161. old->parent = NULL;
  162. return 0;
  163. }
  164. p = &tmp->sibling;
  165. }
  166. return -EINVAL;
  167. }
  168. static void __release_child_resources(struct resource *r)
  169. {
  170. struct resource *tmp, *p;
  171. resource_size_t size;
  172. p = r->child;
  173. r->child = NULL;
  174. while (p) {
  175. tmp = p;
  176. p = p->sibling;
  177. tmp->parent = NULL;
  178. tmp->sibling = NULL;
  179. __release_child_resources(tmp);
  180. printk(KERN_DEBUG "release child resource %pR\n", tmp);
  181. /* need to restore size, and keep flags */
  182. size = resource_size(tmp);
  183. tmp->start = 0;
  184. tmp->end = size - 1;
  185. }
  186. }
  187. void release_child_resources(struct resource *r)
  188. {
  189. write_lock(&resource_lock);
  190. __release_child_resources(r);
  191. write_unlock(&resource_lock);
  192. }
  193. /**
  194. * request_resource_conflict - request and reserve an I/O or memory resource
  195. * @root: root resource descriptor
  196. * @new: resource descriptor desired by caller
  197. *
  198. * Returns 0 for success, conflict resource on error.
  199. */
  200. struct resource *request_resource_conflict(struct resource *root, struct resource *new)
  201. {
  202. struct resource *conflict;
  203. write_lock(&resource_lock);
  204. conflict = __request_resource(root, new);
  205. write_unlock(&resource_lock);
  206. return conflict;
  207. }
  208. /**
  209. * request_resource - request and reserve an I/O or memory resource
  210. * @root: root resource descriptor
  211. * @new: resource descriptor desired by caller
  212. *
  213. * Returns 0 for success, negative error code on error.
  214. */
  215. int request_resource(struct resource *root, struct resource *new)
  216. {
  217. struct resource *conflict;
  218. conflict = request_resource_conflict(root, new);
  219. return conflict ? -EBUSY : 0;
  220. }
  221. EXPORT_SYMBOL(request_resource);
  222. /**
  223. * release_resource - release a previously reserved resource
  224. * @old: resource pointer
  225. */
  226. int release_resource(struct resource *old)
  227. {
  228. int retval;
  229. write_lock(&resource_lock);
  230. retval = __release_resource(old);
  231. write_unlock(&resource_lock);
  232. return retval;
  233. }
  234. EXPORT_SYMBOL(release_resource);
  235. #if !defined(CONFIG_ARCH_HAS_WALK_MEMORY)
  236. /*
  237. * Finds the lowest memory reosurce exists within [res->start.res->end)
  238. * the caller must specify res->start, res->end, res->flags and "name".
  239. * If found, returns 0, res is overwritten, if not found, returns -1.
  240. */
  241. static int find_next_system_ram(struct resource *res, char *name)
  242. {
  243. resource_size_t start, end;
  244. struct resource *p;
  245. BUG_ON(!res);
  246. start = res->start;
  247. end = res->end;
  248. BUG_ON(start >= end);
  249. read_lock(&resource_lock);
  250. for (p = iomem_resource.child; p ; p = p->sibling) {
  251. /* system ram is just marked as IORESOURCE_MEM */
  252. if (p->flags != res->flags)
  253. continue;
  254. if (name && strcmp(p->name, name))
  255. continue;
  256. if (p->start > end) {
  257. p = NULL;
  258. break;
  259. }
  260. if ((p->end >= start) && (p->start < end))
  261. break;
  262. }
  263. read_unlock(&resource_lock);
  264. if (!p)
  265. return -1;
  266. /* copy data */
  267. if (res->start < p->start)
  268. res->start = p->start;
  269. if (res->end > p->end)
  270. res->end = p->end;
  271. return 0;
  272. }
  273. /*
  274. * This function calls callback against all memory range of "System RAM"
  275. * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY.
  276. * Now, this function is only for "System RAM".
  277. */
  278. int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
  279. void *arg, int (*func)(unsigned long, unsigned long, void *))
  280. {
  281. struct resource res;
  282. unsigned long pfn, end_pfn;
  283. u64 orig_end;
  284. int ret = -1;
  285. res.start = (u64) start_pfn << PAGE_SHIFT;
  286. res.end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
  287. res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  288. orig_end = res.end;
  289. while ((res.start < res.end) &&
  290. (find_next_system_ram(&res, "System RAM") >= 0)) {
  291. pfn = (res.start + PAGE_SIZE - 1) >> PAGE_SHIFT;
  292. end_pfn = (res.end + 1) >> PAGE_SHIFT;
  293. if (end_pfn > pfn)
  294. ret = (*func)(pfn, end_pfn - pfn, arg);
  295. if (ret)
  296. break;
  297. res.start = res.end + 1;
  298. res.end = orig_end;
  299. }
  300. return ret;
  301. }
  302. #endif
  303. static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
  304. {
  305. return 1;
  306. }
  307. /*
  308. * This generic page_is_ram() returns true if specified address is
  309. * registered as "System RAM" in iomem_resource list.
  310. */
  311. int __weak page_is_ram(unsigned long pfn)
  312. {
  313. return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
  314. }
  315. /*
  316. * Find empty slot in the resource tree given range and alignment.
  317. */
  318. static int find_resource(struct resource *root, struct resource *new,
  319. resource_size_t size, resource_size_t min,
  320. resource_size_t max, resource_size_t align,
  321. resource_size_t (*alignf)(void *,
  322. const struct resource *,
  323. resource_size_t,
  324. resource_size_t),
  325. void *alignf_data)
  326. {
  327. struct resource *this = root->child;
  328. struct resource tmp = *new;
  329. tmp.start = root->start;
  330. /*
  331. * Skip past an allocated resource that starts at 0, since the assignment
  332. * of this->start - 1 to tmp->end below would cause an underflow.
  333. */
  334. if (this && this->start == 0) {
  335. tmp.start = this->end + 1;
  336. this = this->sibling;
  337. }
  338. for(;;) {
  339. if (this)
  340. tmp.end = this->start - 1;
  341. else
  342. tmp.end = root->end;
  343. if (tmp.start < min)
  344. tmp.start = min;
  345. if (tmp.end > max)
  346. tmp.end = max;
  347. tmp.start = ALIGN(tmp.start, align);
  348. if (alignf)
  349. tmp.start = alignf(alignf_data, &tmp, size, align);
  350. if (tmp.start < tmp.end && tmp.end - tmp.start >= size - 1) {
  351. new->start = tmp.start;
  352. new->end = tmp.start + size - 1;
  353. return 0;
  354. }
  355. if (!this)
  356. break;
  357. tmp.start = this->end + 1;
  358. this = this->sibling;
  359. }
  360. return -EBUSY;
  361. }
  362. /**
  363. * allocate_resource - allocate empty slot in the resource tree given range & alignment
  364. * @root: root resource descriptor
  365. * @new: resource descriptor desired by caller
  366. * @size: requested resource region size
  367. * @min: minimum size to allocate
  368. * @max: maximum size to allocate
  369. * @align: alignment requested, in bytes
  370. * @alignf: alignment function, optional, called if not NULL
  371. * @alignf_data: arbitrary data to pass to the @alignf function
  372. */
  373. int allocate_resource(struct resource *root, struct resource *new,
  374. resource_size_t size, resource_size_t min,
  375. resource_size_t max, resource_size_t align,
  376. resource_size_t (*alignf)(void *,
  377. const struct resource *,
  378. resource_size_t,
  379. resource_size_t),
  380. void *alignf_data)
  381. {
  382. int err;
  383. write_lock(&resource_lock);
  384. err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
  385. if (err >= 0 && __request_resource(root, new))
  386. err = -EBUSY;
  387. write_unlock(&resource_lock);
  388. return err;
  389. }
  390. EXPORT_SYMBOL(allocate_resource);
  391. /*
  392. * Insert a resource into the resource tree. If successful, return NULL,
  393. * otherwise return the conflicting resource (compare to __request_resource())
  394. */
  395. static struct resource * __insert_resource(struct resource *parent, struct resource *new)
  396. {
  397. struct resource *first, *next;
  398. for (;; parent = first) {
  399. first = __request_resource(parent, new);
  400. if (!first)
  401. return first;
  402. if (first == parent)
  403. return first;
  404. if (WARN_ON(first == new)) /* duplicated insertion */
  405. return first;
  406. if ((first->start > new->start) || (first->end < new->end))
  407. break;
  408. if ((first->start == new->start) && (first->end == new->end))
  409. break;
  410. }
  411. for (next = first; ; next = next->sibling) {
  412. /* Partial overlap? Bad, and unfixable */
  413. if (next->start < new->start || next->end > new->end)
  414. return next;
  415. if (!next->sibling)
  416. break;
  417. if (next->sibling->start > new->end)
  418. break;
  419. }
  420. new->parent = parent;
  421. new->sibling = next->sibling;
  422. new->child = first;
  423. next->sibling = NULL;
  424. for (next = first; next; next = next->sibling)
  425. next->parent = new;
  426. if (parent->child == first) {
  427. parent->child = new;
  428. } else {
  429. next = parent->child;
  430. while (next->sibling != first)
  431. next = next->sibling;
  432. next->sibling = new;
  433. }
  434. return NULL;
  435. }
  436. /**
  437. * insert_resource_conflict - Inserts resource in the resource tree
  438. * @parent: parent of the new resource
  439. * @new: new resource to insert
  440. *
  441. * Returns 0 on success, conflict resource if the resource can't be inserted.
  442. *
  443. * This function is equivalent to request_resource_conflict when no conflict
  444. * happens. If a conflict happens, and the conflicting resources
  445. * entirely fit within the range of the new resource, then the new
  446. * resource is inserted and the conflicting resources become children of
  447. * the new resource.
  448. */
  449. struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
  450. {
  451. struct resource *conflict;
  452. write_lock(&resource_lock);
  453. conflict = __insert_resource(parent, new);
  454. write_unlock(&resource_lock);
  455. return conflict;
  456. }
  457. /**
  458. * insert_resource - Inserts a resource in the resource tree
  459. * @parent: parent of the new resource
  460. * @new: new resource to insert
  461. *
  462. * Returns 0 on success, -EBUSY if the resource can't be inserted.
  463. */
  464. int insert_resource(struct resource *parent, struct resource *new)
  465. {
  466. struct resource *conflict;
  467. conflict = insert_resource_conflict(parent, new);
  468. return conflict ? -EBUSY : 0;
  469. }
  470. /**
  471. * insert_resource_expand_to_fit - Insert a resource into the resource tree
  472. * @root: root resource descriptor
  473. * @new: new resource to insert
  474. *
  475. * Insert a resource into the resource tree, possibly expanding it in order
  476. * to make it encompass any conflicting resources.
  477. */
  478. void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
  479. {
  480. if (new->parent)
  481. return;
  482. write_lock(&resource_lock);
  483. for (;;) {
  484. struct resource *conflict;
  485. conflict = __insert_resource(root, new);
  486. if (!conflict)
  487. break;
  488. if (conflict == root)
  489. break;
  490. /* Ok, expand resource to cover the conflict, then try again .. */
  491. if (conflict->start < new->start)
  492. new->start = conflict->start;
  493. if (conflict->end > new->end)
  494. new->end = conflict->end;
  495. printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
  496. }
  497. write_unlock(&resource_lock);
  498. }
  499. /**
  500. * adjust_resource - modify a resource's start and size
  501. * @res: resource to modify
  502. * @start: new start value
  503. * @size: new size
  504. *
  505. * Given an existing resource, change its start and size to match the
  506. * arguments. Returns 0 on success, -EBUSY if it can't fit.
  507. * Existing children of the resource are assumed to be immutable.
  508. */
  509. int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
  510. {
  511. struct resource *tmp, *parent = res->parent;
  512. resource_size_t end = start + size - 1;
  513. int result = -EBUSY;
  514. write_lock(&resource_lock);
  515. if ((start < parent->start) || (end > parent->end))
  516. goto out;
  517. for (tmp = res->child; tmp; tmp = tmp->sibling) {
  518. if ((tmp->start < start) || (tmp->end > end))
  519. goto out;
  520. }
  521. if (res->sibling && (res->sibling->start <= end))
  522. goto out;
  523. tmp = parent->child;
  524. if (tmp != res) {
  525. while (tmp->sibling != res)
  526. tmp = tmp->sibling;
  527. if (start <= tmp->end)
  528. goto out;
  529. }
  530. res->start = start;
  531. res->end = end;
  532. result = 0;
  533. out:
  534. write_unlock(&resource_lock);
  535. return result;
  536. }
  537. static void __init __reserve_region_with_split(struct resource *root,
  538. resource_size_t start, resource_size_t end,
  539. const char *name)
  540. {
  541. struct resource *parent = root;
  542. struct resource *conflict;
  543. struct resource *res = kzalloc(sizeof(*res), GFP_ATOMIC);
  544. if (!res)
  545. return;
  546. res->name = name;
  547. res->start = start;
  548. res->end = end;
  549. res->flags = IORESOURCE_BUSY;
  550. conflict = __request_resource(parent, res);
  551. if (!conflict)
  552. return;
  553. /* failed, split and try again */
  554. kfree(res);
  555. /* conflict covered whole area */
  556. if (conflict->start <= start && conflict->end >= end)
  557. return;
  558. if (conflict->start > start)
  559. __reserve_region_with_split(root, start, conflict->start-1, name);
  560. if (conflict->end < end)
  561. __reserve_region_with_split(root, conflict->end+1, end, name);
  562. }
  563. void __init reserve_region_with_split(struct resource *root,
  564. resource_size_t start, resource_size_t end,
  565. const char *name)
  566. {
  567. write_lock(&resource_lock);
  568. __reserve_region_with_split(root, start, end, name);
  569. write_unlock(&resource_lock);
  570. }
  571. EXPORT_SYMBOL(adjust_resource);
  572. /**
  573. * resource_alignment - calculate resource's alignment
  574. * @res: resource pointer
  575. *
  576. * Returns alignment on success, 0 (invalid alignment) on failure.
  577. */
  578. resource_size_t resource_alignment(struct resource *res)
  579. {
  580. switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
  581. case IORESOURCE_SIZEALIGN:
  582. return resource_size(res);
  583. case IORESOURCE_STARTALIGN:
  584. return res->start;
  585. default:
  586. return 0;
  587. }
  588. }
  589. /*
  590. * This is compatibility stuff for IO resources.
  591. *
  592. * Note how this, unlike the above, knows about
  593. * the IO flag meanings (busy etc).
  594. *
  595. * request_region creates a new busy region.
  596. *
  597. * check_region returns non-zero if the area is already busy.
  598. *
  599. * release_region releases a matching busy region.
  600. */
  601. static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
  602. /**
  603. * __request_region - create a new busy resource region
  604. * @parent: parent resource descriptor
  605. * @start: resource start address
  606. * @n: resource region size
  607. * @name: reserving caller's ID string
  608. * @flags: IO resource flags
  609. */
  610. struct resource * __request_region(struct resource *parent,
  611. resource_size_t start, resource_size_t n,
  612. const char *name, int flags)
  613. {
  614. DECLARE_WAITQUEUE(wait, current);
  615. struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
  616. if (!res)
  617. return NULL;
  618. res->name = name;
  619. res->start = start;
  620. res->end = start + n - 1;
  621. res->flags = IORESOURCE_BUSY;
  622. res->flags |= flags;
  623. write_lock(&resource_lock);
  624. for (;;) {
  625. struct resource *conflict;
  626. conflict = __request_resource(parent, res);
  627. if (!conflict)
  628. break;
  629. if (conflict != parent) {
  630. parent = conflict;
  631. if (!(conflict->flags & IORESOURCE_BUSY))
  632. continue;
  633. }
  634. if (conflict->flags & flags & IORESOURCE_MUXED) {
  635. add_wait_queue(&muxed_resource_wait, &wait);
  636. write_unlock(&resource_lock);
  637. set_current_state(TASK_UNINTERRUPTIBLE);
  638. schedule();
  639. remove_wait_queue(&muxed_resource_wait, &wait);
  640. write_lock(&resource_lock);
  641. continue;
  642. }
  643. /* Uhhuh, that didn't work out.. */
  644. kfree(res);
  645. res = NULL;
  646. break;
  647. }
  648. write_unlock(&resource_lock);
  649. return res;
  650. }
  651. EXPORT_SYMBOL(__request_region);
  652. /**
  653. * __check_region - check if a resource region is busy or free
  654. * @parent: parent resource descriptor
  655. * @start: resource start address
  656. * @n: resource region size
  657. *
  658. * Returns 0 if the region is free at the moment it is checked,
  659. * returns %-EBUSY if the region is busy.
  660. *
  661. * NOTE:
  662. * This function is deprecated because its use is racy.
  663. * Even if it returns 0, a subsequent call to request_region()
  664. * may fail because another driver etc. just allocated the region.
  665. * Do NOT use it. It will be removed from the kernel.
  666. */
  667. int __check_region(struct resource *parent, resource_size_t start,
  668. resource_size_t n)
  669. {
  670. struct resource * res;
  671. res = __request_region(parent, start, n, "check-region", 0);
  672. if (!res)
  673. return -EBUSY;
  674. release_resource(res);
  675. kfree(res);
  676. return 0;
  677. }
  678. EXPORT_SYMBOL(__check_region);
  679. /**
  680. * __release_region - release a previously reserved resource region
  681. * @parent: parent resource descriptor
  682. * @start: resource start address
  683. * @n: resource region size
  684. *
  685. * The described resource region must match a currently busy region.
  686. */
  687. void __release_region(struct resource *parent, resource_size_t start,
  688. resource_size_t n)
  689. {
  690. struct resource **p;
  691. resource_size_t end;
  692. p = &parent->child;
  693. end = start + n - 1;
  694. write_lock(&resource_lock);
  695. for (;;) {
  696. struct resource *res = *p;
  697. if (!res)
  698. break;
  699. if (res->start <= start && res->end >= end) {
  700. if (!(res->flags & IORESOURCE_BUSY)) {
  701. p = &res->child;
  702. continue;
  703. }
  704. if (res->start != start || res->end != end)
  705. break;
  706. *p = res->sibling;
  707. write_unlock(&resource_lock);
  708. if (res->flags & IORESOURCE_MUXED)
  709. wake_up(&muxed_resource_wait);
  710. kfree(res);
  711. return;
  712. }
  713. p = &res->sibling;
  714. }
  715. write_unlock(&resource_lock);
  716. printk(KERN_WARNING "Trying to free nonexistent resource "
  717. "<%016llx-%016llx>\n", (unsigned long long)start,
  718. (unsigned long long)end);
  719. }
  720. EXPORT_SYMBOL(__release_region);
  721. /*
  722. * Managed region resource
  723. */
  724. struct region_devres {
  725. struct resource *parent;
  726. resource_size_t start;
  727. resource_size_t n;
  728. };
  729. static void devm_region_release(struct device *dev, void *res)
  730. {
  731. struct region_devres *this = res;
  732. __release_region(this->parent, this->start, this->n);
  733. }
  734. static int devm_region_match(struct device *dev, void *res, void *match_data)
  735. {
  736. struct region_devres *this = res, *match = match_data;
  737. return this->parent == match->parent &&
  738. this->start == match->start && this->n == match->n;
  739. }
  740. struct resource * __devm_request_region(struct device *dev,
  741. struct resource *parent, resource_size_t start,
  742. resource_size_t n, const char *name)
  743. {
  744. struct region_devres *dr = NULL;
  745. struct resource *res;
  746. dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
  747. GFP_KERNEL);
  748. if (!dr)
  749. return NULL;
  750. dr->parent = parent;
  751. dr->start = start;
  752. dr->n = n;
  753. res = __request_region(parent, start, n, name, 0);
  754. if (res)
  755. devres_add(dev, dr);
  756. else
  757. devres_free(dr);
  758. return res;
  759. }
  760. EXPORT_SYMBOL(__devm_request_region);
  761. void __devm_release_region(struct device *dev, struct resource *parent,
  762. resource_size_t start, resource_size_t n)
  763. {
  764. struct region_devres match_data = { parent, start, n };
  765. __release_region(parent, start, n);
  766. WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
  767. &match_data));
  768. }
  769. EXPORT_SYMBOL(__devm_release_region);
  770. /*
  771. * Called from init/main.c to reserve IO ports.
  772. */
  773. #define MAXRESERVE 4
  774. static int __init reserve_setup(char *str)
  775. {
  776. static int reserved;
  777. static struct resource reserve[MAXRESERVE];
  778. for (;;) {
  779. unsigned int io_start, io_num;
  780. int x = reserved;
  781. if (get_option (&str, &io_start) != 2)
  782. break;
  783. if (get_option (&str, &io_num) == 0)
  784. break;
  785. if (x < MAXRESERVE) {
  786. struct resource *res = reserve + x;
  787. res->name = "reserved";
  788. res->start = io_start;
  789. res->end = io_start + io_num - 1;
  790. res->flags = IORESOURCE_BUSY;
  791. res->child = NULL;
  792. if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
  793. reserved = x+1;
  794. }
  795. }
  796. return 1;
  797. }
  798. __setup("reserve=", reserve_setup);
  799. /*
  800. * Check if the requested addr and size spans more than any slot in the
  801. * iomem resource tree.
  802. */
  803. int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
  804. {
  805. struct resource *p = &iomem_resource;
  806. int err = 0;
  807. loff_t l;
  808. read_lock(&resource_lock);
  809. for (p = p->child; p ; p = r_next(NULL, p, &l)) {
  810. /*
  811. * We can probably skip the resources without
  812. * IORESOURCE_IO attribute?
  813. */
  814. if (p->start >= addr + size)
  815. continue;
  816. if (p->end < addr)
  817. continue;
  818. if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
  819. PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
  820. continue;
  821. /*
  822. * if a resource is "BUSY", it's not a hardware resource
  823. * but a driver mapping of such a resource; we don't want
  824. * to warn for those; some drivers legitimately map only
  825. * partial hardware resources. (example: vesafb)
  826. */
  827. if (p->flags & IORESOURCE_BUSY)
  828. continue;
  829. printk(KERN_WARNING "resource map sanity check conflict: "
  830. "0x%llx 0x%llx 0x%llx 0x%llx %s\n",
  831. (unsigned long long)addr,
  832. (unsigned long long)(addr + size - 1),
  833. (unsigned long long)p->start,
  834. (unsigned long long)p->end,
  835. p->name);
  836. err = -1;
  837. break;
  838. }
  839. read_unlock(&resource_lock);
  840. return err;
  841. }
  842. #ifdef CONFIG_STRICT_DEVMEM
  843. static int strict_iomem_checks = 1;
  844. #else
  845. static int strict_iomem_checks;
  846. #endif
  847. /*
  848. * check if an address is reserved in the iomem resource tree
  849. * returns 1 if reserved, 0 if not reserved.
  850. */
  851. int iomem_is_exclusive(u64 addr)
  852. {
  853. struct resource *p = &iomem_resource;
  854. int err = 0;
  855. loff_t l;
  856. int size = PAGE_SIZE;
  857. if (!strict_iomem_checks)
  858. return 0;
  859. addr = addr & PAGE_MASK;
  860. read_lock(&resource_lock);
  861. for (p = p->child; p ; p = r_next(NULL, p, &l)) {
  862. /*
  863. * We can probably skip the resources without
  864. * IORESOURCE_IO attribute?
  865. */
  866. if (p->start >= addr + size)
  867. break;
  868. if (p->end < addr)
  869. continue;
  870. if (p->flags & IORESOURCE_BUSY &&
  871. p->flags & IORESOURCE_EXCLUSIVE) {
  872. err = 1;
  873. break;
  874. }
  875. }
  876. read_unlock(&resource_lock);
  877. return err;
  878. }
  879. static int __init strict_iomem(char *str)
  880. {
  881. if (strstr(str, "relaxed"))
  882. strict_iomem_checks = 0;
  883. if (strstr(str, "strict"))
  884. strict_iomem_checks = 1;
  885. return 1;
  886. }
  887. __setup("iomem=", strict_iomem);