resource.c 24 KB

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