resource.c 26 KB

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