resource.c 23 KB

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