resource.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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. static resource_size_t simple_align_resource(void *data,
  316. const struct resource *avail,
  317. resource_size_t size,
  318. resource_size_t align)
  319. {
  320. return avail->start;
  321. }
  322. /*
  323. * Find empty slot in the resource tree given range and alignment.
  324. */
  325. static int find_resource(struct resource *root, struct resource *new,
  326. resource_size_t size, resource_size_t min,
  327. resource_size_t max, resource_size_t align,
  328. resource_size_t (*alignf)(void *,
  329. const struct resource *,
  330. resource_size_t,
  331. resource_size_t),
  332. void *alignf_data)
  333. {
  334. struct resource *this = root->child;
  335. struct resource tmp = *new;
  336. tmp.start = root->start;
  337. /*
  338. * Skip past an allocated resource that starts at 0, since the assignment
  339. * of this->start - 1 to tmp->end below would cause an underflow.
  340. */
  341. if (this && this->start == 0) {
  342. tmp.start = this->end + 1;
  343. this = this->sibling;
  344. }
  345. for(;;) {
  346. if (this)
  347. tmp.end = this->start - 1;
  348. else
  349. tmp.end = root->end;
  350. if (tmp.start < min)
  351. tmp.start = min;
  352. if (tmp.end > max)
  353. tmp.end = max;
  354. tmp.start = ALIGN(tmp.start, align);
  355. tmp.start = alignf(alignf_data, &tmp, size, align);
  356. if (tmp.start < tmp.end && tmp.end - tmp.start >= size - 1) {
  357. new->start = tmp.start;
  358. new->end = tmp.start + size - 1;
  359. return 0;
  360. }
  361. if (!this)
  362. break;
  363. tmp.start = this->end + 1;
  364. this = this->sibling;
  365. }
  366. return -EBUSY;
  367. }
  368. /**
  369. * allocate_resource - allocate empty slot in the resource tree given range & alignment
  370. * @root: root resource descriptor
  371. * @new: resource descriptor desired by caller
  372. * @size: requested resource region size
  373. * @min: minimum size to allocate
  374. * @max: maximum size to allocate
  375. * @align: alignment requested, in bytes
  376. * @alignf: alignment function, optional, called if not NULL
  377. * @alignf_data: arbitrary data to pass to the @alignf function
  378. */
  379. int allocate_resource(struct resource *root, struct resource *new,
  380. resource_size_t size, resource_size_t min,
  381. resource_size_t max, resource_size_t align,
  382. resource_size_t (*alignf)(void *,
  383. const struct resource *,
  384. resource_size_t,
  385. resource_size_t),
  386. void *alignf_data)
  387. {
  388. int err;
  389. if (!alignf)
  390. alignf = simple_align_resource;
  391. write_lock(&resource_lock);
  392. err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
  393. if (err >= 0 && __request_resource(root, new))
  394. err = -EBUSY;
  395. write_unlock(&resource_lock);
  396. return err;
  397. }
  398. EXPORT_SYMBOL(allocate_resource);
  399. /*
  400. * Insert a resource into the resource tree. If successful, return NULL,
  401. * otherwise return the conflicting resource (compare to __request_resource())
  402. */
  403. static struct resource * __insert_resource(struct resource *parent, struct resource *new)
  404. {
  405. struct resource *first, *next;
  406. for (;; parent = first) {
  407. first = __request_resource(parent, new);
  408. if (!first)
  409. return first;
  410. if (first == parent)
  411. return first;
  412. if ((first->start > new->start) || (first->end < new->end))
  413. break;
  414. if ((first->start == new->start) && (first->end == new->end))
  415. break;
  416. }
  417. for (next = first; ; next = next->sibling) {
  418. /* Partial overlap? Bad, and unfixable */
  419. if (next->start < new->start || next->end > new->end)
  420. return next;
  421. if (!next->sibling)
  422. break;
  423. if (next->sibling->start > new->end)
  424. break;
  425. }
  426. new->parent = parent;
  427. new->sibling = next->sibling;
  428. new->child = first;
  429. next->sibling = NULL;
  430. for (next = first; next; next = next->sibling)
  431. next->parent = new;
  432. if (parent->child == first) {
  433. parent->child = new;
  434. } else {
  435. next = parent->child;
  436. while (next->sibling != first)
  437. next = next->sibling;
  438. next->sibling = new;
  439. }
  440. return NULL;
  441. }
  442. /**
  443. * insert_resource_conflict - Inserts resource in the resource tree
  444. * @parent: parent of the new resource
  445. * @new: new resource to insert
  446. *
  447. * Returns 0 on success, conflict resource if the resource can't be inserted.
  448. *
  449. * This function is equivalent to request_resource_conflict when no conflict
  450. * happens. If a conflict happens, and the conflicting resources
  451. * entirely fit within the range of the new resource, then the new
  452. * resource is inserted and the conflicting resources become children of
  453. * the new resource.
  454. */
  455. struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
  456. {
  457. struct resource *conflict;
  458. write_lock(&resource_lock);
  459. conflict = __insert_resource(parent, new);
  460. write_unlock(&resource_lock);
  461. return conflict;
  462. }
  463. /**
  464. * insert_resource - Inserts a resource in the resource tree
  465. * @parent: parent of the new resource
  466. * @new: new resource to insert
  467. *
  468. * Returns 0 on success, -EBUSY if the resource can't be inserted.
  469. */
  470. int insert_resource(struct resource *parent, struct resource *new)
  471. {
  472. struct resource *conflict;
  473. conflict = insert_resource_conflict(parent, new);
  474. return conflict ? -EBUSY : 0;
  475. }
  476. /**
  477. * insert_resource_expand_to_fit - Insert a resource into the resource tree
  478. * @root: root resource descriptor
  479. * @new: new resource to insert
  480. *
  481. * Insert a resource into the resource tree, possibly expanding it in order
  482. * to make it encompass any conflicting resources.
  483. */
  484. void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
  485. {
  486. if (new->parent)
  487. return;
  488. write_lock(&resource_lock);
  489. for (;;) {
  490. struct resource *conflict;
  491. conflict = __insert_resource(root, new);
  492. if (!conflict)
  493. break;
  494. if (conflict == root)
  495. break;
  496. /* Ok, expand resource to cover the conflict, then try again .. */
  497. if (conflict->start < new->start)
  498. new->start = conflict->start;
  499. if (conflict->end > new->end)
  500. new->end = conflict->end;
  501. printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
  502. }
  503. write_unlock(&resource_lock);
  504. }
  505. /**
  506. * adjust_resource - modify a resource's start and size
  507. * @res: resource to modify
  508. * @start: new start value
  509. * @size: new size
  510. *
  511. * Given an existing resource, change its start and size to match the
  512. * arguments. Returns 0 on success, -EBUSY if it can't fit.
  513. * Existing children of the resource are assumed to be immutable.
  514. */
  515. int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
  516. {
  517. struct resource *tmp, *parent = res->parent;
  518. resource_size_t end = start + size - 1;
  519. int result = -EBUSY;
  520. write_lock(&resource_lock);
  521. if ((start < parent->start) || (end > parent->end))
  522. goto out;
  523. for (tmp = res->child; tmp; tmp = tmp->sibling) {
  524. if ((tmp->start < start) || (tmp->end > end))
  525. goto out;
  526. }
  527. if (res->sibling && (res->sibling->start <= end))
  528. goto out;
  529. tmp = parent->child;
  530. if (tmp != res) {
  531. while (tmp->sibling != res)
  532. tmp = tmp->sibling;
  533. if (start <= tmp->end)
  534. goto out;
  535. }
  536. res->start = start;
  537. res->end = end;
  538. result = 0;
  539. out:
  540. write_unlock(&resource_lock);
  541. return result;
  542. }
  543. static void __init __reserve_region_with_split(struct resource *root,
  544. resource_size_t start, resource_size_t end,
  545. const char *name)
  546. {
  547. struct resource *parent = root;
  548. struct resource *conflict;
  549. struct resource *res = kzalloc(sizeof(*res), GFP_ATOMIC);
  550. if (!res)
  551. return;
  552. res->name = name;
  553. res->start = start;
  554. res->end = end;
  555. res->flags = IORESOURCE_BUSY;
  556. conflict = __request_resource(parent, res);
  557. if (!conflict)
  558. return;
  559. /* failed, split and try again */
  560. kfree(res);
  561. /* conflict covered whole area */
  562. if (conflict->start <= start && conflict->end >= end)
  563. return;
  564. if (conflict->start > start)
  565. __reserve_region_with_split(root, start, conflict->start-1, name);
  566. if (conflict->end < end)
  567. __reserve_region_with_split(root, conflict->end+1, end, name);
  568. }
  569. void __init reserve_region_with_split(struct resource *root,
  570. resource_size_t start, resource_size_t end,
  571. const char *name)
  572. {
  573. write_lock(&resource_lock);
  574. __reserve_region_with_split(root, start, end, name);
  575. write_unlock(&resource_lock);
  576. }
  577. EXPORT_SYMBOL(adjust_resource);
  578. /**
  579. * resource_alignment - calculate resource's alignment
  580. * @res: resource pointer
  581. *
  582. * Returns alignment on success, 0 (invalid alignment) on failure.
  583. */
  584. resource_size_t resource_alignment(struct resource *res)
  585. {
  586. switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
  587. case IORESOURCE_SIZEALIGN:
  588. return resource_size(res);
  589. case IORESOURCE_STARTALIGN:
  590. return res->start;
  591. default:
  592. return 0;
  593. }
  594. }
  595. /*
  596. * This is compatibility stuff for IO resources.
  597. *
  598. * Note how this, unlike the above, knows about
  599. * the IO flag meanings (busy etc).
  600. *
  601. * request_region creates a new busy region.
  602. *
  603. * check_region returns non-zero if the area is already busy.
  604. *
  605. * release_region releases a matching busy region.
  606. */
  607. static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
  608. /**
  609. * __request_region - create a new busy resource region
  610. * @parent: parent resource descriptor
  611. * @start: resource start address
  612. * @n: resource region size
  613. * @name: reserving caller's ID string
  614. * @flags: IO resource flags
  615. */
  616. struct resource * __request_region(struct resource *parent,
  617. resource_size_t start, resource_size_t n,
  618. const char *name, int flags)
  619. {
  620. DECLARE_WAITQUEUE(wait, current);
  621. struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
  622. if (!res)
  623. return NULL;
  624. res->name = name;
  625. res->start = start;
  626. res->end = start + n - 1;
  627. res->flags = IORESOURCE_BUSY;
  628. res->flags |= flags;
  629. write_lock(&resource_lock);
  630. for (;;) {
  631. struct resource *conflict;
  632. conflict = __request_resource(parent, res);
  633. if (!conflict)
  634. break;
  635. if (conflict != parent) {
  636. parent = conflict;
  637. if (!(conflict->flags & IORESOURCE_BUSY))
  638. continue;
  639. }
  640. if (conflict->flags & flags & IORESOURCE_MUXED) {
  641. add_wait_queue(&muxed_resource_wait, &wait);
  642. write_unlock(&resource_lock);
  643. set_current_state(TASK_UNINTERRUPTIBLE);
  644. schedule();
  645. remove_wait_queue(&muxed_resource_wait, &wait);
  646. write_lock(&resource_lock);
  647. continue;
  648. }
  649. /* Uhhuh, that didn't work out.. */
  650. kfree(res);
  651. res = NULL;
  652. break;
  653. }
  654. write_unlock(&resource_lock);
  655. return res;
  656. }
  657. EXPORT_SYMBOL(__request_region);
  658. /**
  659. * __check_region - check if a resource region is busy or free
  660. * @parent: parent resource descriptor
  661. * @start: resource start address
  662. * @n: resource region size
  663. *
  664. * Returns 0 if the region is free at the moment it is checked,
  665. * returns %-EBUSY if the region is busy.
  666. *
  667. * NOTE:
  668. * This function is deprecated because its use is racy.
  669. * Even if it returns 0, a subsequent call to request_region()
  670. * may fail because another driver etc. just allocated the region.
  671. * Do NOT use it. It will be removed from the kernel.
  672. */
  673. int __check_region(struct resource *parent, resource_size_t start,
  674. resource_size_t n)
  675. {
  676. struct resource * res;
  677. res = __request_region(parent, start, n, "check-region", 0);
  678. if (!res)
  679. return -EBUSY;
  680. release_resource(res);
  681. kfree(res);
  682. return 0;
  683. }
  684. EXPORT_SYMBOL(__check_region);
  685. /**
  686. * __release_region - release a previously reserved resource region
  687. * @parent: parent resource descriptor
  688. * @start: resource start address
  689. * @n: resource region size
  690. *
  691. * The described resource region must match a currently busy region.
  692. */
  693. void __release_region(struct resource *parent, resource_size_t start,
  694. resource_size_t n)
  695. {
  696. struct resource **p;
  697. resource_size_t end;
  698. p = &parent->child;
  699. end = start + n - 1;
  700. write_lock(&resource_lock);
  701. for (;;) {
  702. struct resource *res = *p;
  703. if (!res)
  704. break;
  705. if (res->start <= start && res->end >= end) {
  706. if (!(res->flags & IORESOURCE_BUSY)) {
  707. p = &res->child;
  708. continue;
  709. }
  710. if (res->start != start || res->end != end)
  711. break;
  712. *p = res->sibling;
  713. write_unlock(&resource_lock);
  714. if (res->flags & IORESOURCE_MUXED)
  715. wake_up(&muxed_resource_wait);
  716. kfree(res);
  717. return;
  718. }
  719. p = &res->sibling;
  720. }
  721. write_unlock(&resource_lock);
  722. printk(KERN_WARNING "Trying to free nonexistent resource "
  723. "<%016llx-%016llx>\n", (unsigned long long)start,
  724. (unsigned long long)end);
  725. }
  726. EXPORT_SYMBOL(__release_region);
  727. /*
  728. * Managed region resource
  729. */
  730. struct region_devres {
  731. struct resource *parent;
  732. resource_size_t start;
  733. resource_size_t n;
  734. };
  735. static void devm_region_release(struct device *dev, void *res)
  736. {
  737. struct region_devres *this = res;
  738. __release_region(this->parent, this->start, this->n);
  739. }
  740. static int devm_region_match(struct device *dev, void *res, void *match_data)
  741. {
  742. struct region_devres *this = res, *match = match_data;
  743. return this->parent == match->parent &&
  744. this->start == match->start && this->n == match->n;
  745. }
  746. struct resource * __devm_request_region(struct device *dev,
  747. struct resource *parent, resource_size_t start,
  748. resource_size_t n, const char *name)
  749. {
  750. struct region_devres *dr = NULL;
  751. struct resource *res;
  752. dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
  753. GFP_KERNEL);
  754. if (!dr)
  755. return NULL;
  756. dr->parent = parent;
  757. dr->start = start;
  758. dr->n = n;
  759. res = __request_region(parent, start, n, name, 0);
  760. if (res)
  761. devres_add(dev, dr);
  762. else
  763. devres_free(dr);
  764. return res;
  765. }
  766. EXPORT_SYMBOL(__devm_request_region);
  767. void __devm_release_region(struct device *dev, struct resource *parent,
  768. resource_size_t start, resource_size_t n)
  769. {
  770. struct region_devres match_data = { parent, start, n };
  771. __release_region(parent, start, n);
  772. WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
  773. &match_data));
  774. }
  775. EXPORT_SYMBOL(__devm_release_region);
  776. /*
  777. * Called from init/main.c to reserve IO ports.
  778. */
  779. #define MAXRESERVE 4
  780. static int __init reserve_setup(char *str)
  781. {
  782. static int reserved;
  783. static struct resource reserve[MAXRESERVE];
  784. for (;;) {
  785. unsigned int io_start, io_num;
  786. int x = reserved;
  787. if (get_option (&str, &io_start) != 2)
  788. break;
  789. if (get_option (&str, &io_num) == 0)
  790. break;
  791. if (x < MAXRESERVE) {
  792. struct resource *res = reserve + x;
  793. res->name = "reserved";
  794. res->start = io_start;
  795. res->end = io_start + io_num - 1;
  796. res->flags = IORESOURCE_BUSY;
  797. res->child = NULL;
  798. if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
  799. reserved = x+1;
  800. }
  801. }
  802. return 1;
  803. }
  804. __setup("reserve=", reserve_setup);
  805. /*
  806. * Check if the requested addr and size spans more than any slot in the
  807. * iomem resource tree.
  808. */
  809. int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
  810. {
  811. struct resource *p = &iomem_resource;
  812. int err = 0;
  813. loff_t l;
  814. read_lock(&resource_lock);
  815. for (p = p->child; p ; p = r_next(NULL, p, &l)) {
  816. /*
  817. * We can probably skip the resources without
  818. * IORESOURCE_IO attribute?
  819. */
  820. if (p->start >= addr + size)
  821. continue;
  822. if (p->end < addr)
  823. continue;
  824. if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
  825. PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
  826. continue;
  827. /*
  828. * if a resource is "BUSY", it's not a hardware resource
  829. * but a driver mapping of such a resource; we don't want
  830. * to warn for those; some drivers legitimately map only
  831. * partial hardware resources. (example: vesafb)
  832. */
  833. if (p->flags & IORESOURCE_BUSY)
  834. continue;
  835. printk(KERN_WARNING "resource map sanity check conflict: "
  836. "0x%llx 0x%llx 0x%llx 0x%llx %s\n",
  837. (unsigned long long)addr,
  838. (unsigned long long)(addr + size - 1),
  839. (unsigned long long)p->start,
  840. (unsigned long long)p->end,
  841. p->name);
  842. err = -1;
  843. break;
  844. }
  845. read_unlock(&resource_lock);
  846. return err;
  847. }
  848. #ifdef CONFIG_STRICT_DEVMEM
  849. static int strict_iomem_checks = 1;
  850. #else
  851. static int strict_iomem_checks;
  852. #endif
  853. /*
  854. * check if an address is reserved in the iomem resource tree
  855. * returns 1 if reserved, 0 if not reserved.
  856. */
  857. int iomem_is_exclusive(u64 addr)
  858. {
  859. struct resource *p = &iomem_resource;
  860. int err = 0;
  861. loff_t l;
  862. int size = PAGE_SIZE;
  863. if (!strict_iomem_checks)
  864. return 0;
  865. addr = addr & PAGE_MASK;
  866. read_lock(&resource_lock);
  867. for (p = p->child; p ; p = r_next(NULL, p, &l)) {
  868. /*
  869. * We can probably skip the resources without
  870. * IORESOURCE_IO attribute?
  871. */
  872. if (p->start >= addr + size)
  873. break;
  874. if (p->end < addr)
  875. continue;
  876. if (p->flags & IORESOURCE_BUSY &&
  877. p->flags & IORESOURCE_EXCLUSIVE) {
  878. err = 1;
  879. break;
  880. }
  881. }
  882. read_unlock(&resource_lock);
  883. return err;
  884. }
  885. static int __init strict_iomem(char *str)
  886. {
  887. if (strstr(str, "relaxed"))
  888. strict_iomem_checks = 0;
  889. if (strstr(str, "strict"))
  890. strict_iomem_checks = 1;
  891. return 1;
  892. }
  893. __setup("iomem=", strict_iomem);