resource.c 23 KB

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