resource.c 28 KB

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