resource.c 27 KB

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