resource.c 24 KB

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