resource.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  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. static int __adjust_resource(struct resource *res, resource_size_t start,
  618. resource_size_t size)
  619. {
  620. struct resource *tmp, *parent = res->parent;
  621. resource_size_t end = start + size - 1;
  622. int result = -EBUSY;
  623. if (!parent)
  624. goto skip;
  625. if ((start < parent->start) || (end > parent->end))
  626. goto out;
  627. if (res->sibling && (res->sibling->start <= end))
  628. goto out;
  629. tmp = parent->child;
  630. if (tmp != res) {
  631. while (tmp->sibling != res)
  632. tmp = tmp->sibling;
  633. if (start <= tmp->end)
  634. goto out;
  635. }
  636. skip:
  637. for (tmp = res->child; tmp; tmp = tmp->sibling)
  638. if ((tmp->start < start) || (tmp->end > end))
  639. goto out;
  640. res->start = start;
  641. res->end = end;
  642. result = 0;
  643. out:
  644. return result;
  645. }
  646. /**
  647. * adjust_resource - modify a resource's start and size
  648. * @res: resource to modify
  649. * @start: new start value
  650. * @size: new size
  651. *
  652. * Given an existing resource, change its start and size to match the
  653. * arguments. Returns 0 on success, -EBUSY if it can't fit.
  654. * Existing children of the resource are assumed to be immutable.
  655. */
  656. int adjust_resource(struct resource *res, resource_size_t start,
  657. resource_size_t size)
  658. {
  659. int result;
  660. write_lock(&resource_lock);
  661. result = __adjust_resource(res, start, size);
  662. write_unlock(&resource_lock);
  663. return result;
  664. }
  665. EXPORT_SYMBOL(adjust_resource);
  666. static void __init __reserve_region_with_split(struct resource *root,
  667. resource_size_t start, resource_size_t end,
  668. const char *name)
  669. {
  670. struct resource *parent = root;
  671. struct resource *conflict;
  672. struct resource *res = kzalloc(sizeof(*res), GFP_ATOMIC);
  673. struct resource *next_res = NULL;
  674. if (!res)
  675. return;
  676. res->name = name;
  677. res->start = start;
  678. res->end = end;
  679. res->flags = IORESOURCE_BUSY;
  680. while (1) {
  681. conflict = __request_resource(parent, res);
  682. if (!conflict) {
  683. if (!next_res)
  684. break;
  685. res = next_res;
  686. next_res = NULL;
  687. continue;
  688. }
  689. /* conflict covered whole area */
  690. if (conflict->start <= res->start &&
  691. conflict->end >= res->end) {
  692. kfree(res);
  693. WARN_ON(next_res);
  694. break;
  695. }
  696. /* failed, split and try again */
  697. if (conflict->start > res->start) {
  698. end = res->end;
  699. res->end = conflict->start - 1;
  700. if (conflict->end < end) {
  701. next_res = kzalloc(sizeof(*next_res),
  702. GFP_ATOMIC);
  703. if (!next_res) {
  704. kfree(res);
  705. break;
  706. }
  707. next_res->name = name;
  708. next_res->start = conflict->end + 1;
  709. next_res->end = end;
  710. next_res->flags = IORESOURCE_BUSY;
  711. }
  712. } else {
  713. res->start = conflict->end + 1;
  714. }
  715. }
  716. }
  717. void __init reserve_region_with_split(struct resource *root,
  718. resource_size_t start, resource_size_t end,
  719. const char *name)
  720. {
  721. int abort = 0;
  722. write_lock(&resource_lock);
  723. if (root->start > start || root->end < end) {
  724. pr_err("requested range [0x%llx-0x%llx] not in root %pr\n",
  725. (unsigned long long)start, (unsigned long long)end,
  726. root);
  727. if (start > root->end || end < root->start)
  728. abort = 1;
  729. else {
  730. if (end > root->end)
  731. end = root->end;
  732. if (start < root->start)
  733. start = root->start;
  734. pr_err("fixing request to [0x%llx-0x%llx]\n",
  735. (unsigned long long)start,
  736. (unsigned long long)end);
  737. }
  738. dump_stack();
  739. }
  740. if (!abort)
  741. __reserve_region_with_split(root, start, end, name);
  742. write_unlock(&resource_lock);
  743. }
  744. /**
  745. * resource_alignment - calculate resource's alignment
  746. * @res: resource pointer
  747. *
  748. * Returns alignment on success, 0 (invalid alignment) on failure.
  749. */
  750. resource_size_t resource_alignment(struct resource *res)
  751. {
  752. switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
  753. case IORESOURCE_SIZEALIGN:
  754. return resource_size(res);
  755. case IORESOURCE_STARTALIGN:
  756. return res->start;
  757. default:
  758. return 0;
  759. }
  760. }
  761. /*
  762. * This is compatibility stuff for IO resources.
  763. *
  764. * Note how this, unlike the above, knows about
  765. * the IO flag meanings (busy etc).
  766. *
  767. * request_region creates a new busy region.
  768. *
  769. * check_region returns non-zero if the area is already busy.
  770. *
  771. * release_region releases a matching busy region.
  772. */
  773. static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
  774. /**
  775. * __request_region - create a new busy resource region
  776. * @parent: parent resource descriptor
  777. * @start: resource start address
  778. * @n: resource region size
  779. * @name: reserving caller's ID string
  780. * @flags: IO resource flags
  781. */
  782. struct resource * __request_region(struct resource *parent,
  783. resource_size_t start, resource_size_t n,
  784. const char *name, int flags)
  785. {
  786. DECLARE_WAITQUEUE(wait, current);
  787. struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
  788. if (!res)
  789. return NULL;
  790. res->name = name;
  791. res->start = start;
  792. res->end = start + n - 1;
  793. res->flags = IORESOURCE_BUSY;
  794. res->flags |= flags;
  795. write_lock(&resource_lock);
  796. for (;;) {
  797. struct resource *conflict;
  798. conflict = __request_resource(parent, res);
  799. if (!conflict)
  800. break;
  801. if (conflict != parent) {
  802. parent = conflict;
  803. if (!(conflict->flags & IORESOURCE_BUSY))
  804. continue;
  805. }
  806. if (conflict->flags & flags & IORESOURCE_MUXED) {
  807. add_wait_queue(&muxed_resource_wait, &wait);
  808. write_unlock(&resource_lock);
  809. set_current_state(TASK_UNINTERRUPTIBLE);
  810. schedule();
  811. remove_wait_queue(&muxed_resource_wait, &wait);
  812. write_lock(&resource_lock);
  813. continue;
  814. }
  815. /* Uhhuh, that didn't work out.. */
  816. kfree(res);
  817. res = NULL;
  818. break;
  819. }
  820. write_unlock(&resource_lock);
  821. return res;
  822. }
  823. EXPORT_SYMBOL(__request_region);
  824. /**
  825. * __check_region - check if a resource region is busy or free
  826. * @parent: parent resource descriptor
  827. * @start: resource start address
  828. * @n: resource region size
  829. *
  830. * Returns 0 if the region is free at the moment it is checked,
  831. * returns %-EBUSY if the region is busy.
  832. *
  833. * NOTE:
  834. * This function is deprecated because its use is racy.
  835. * Even if it returns 0, a subsequent call to request_region()
  836. * may fail because another driver etc. just allocated the region.
  837. * Do NOT use it. It will be removed from the kernel.
  838. */
  839. int __check_region(struct resource *parent, resource_size_t start,
  840. resource_size_t n)
  841. {
  842. struct resource * res;
  843. res = __request_region(parent, start, n, "check-region", 0);
  844. if (!res)
  845. return -EBUSY;
  846. release_resource(res);
  847. kfree(res);
  848. return 0;
  849. }
  850. EXPORT_SYMBOL(__check_region);
  851. /**
  852. * __release_region - release a previously reserved resource region
  853. * @parent: parent resource descriptor
  854. * @start: resource start address
  855. * @n: resource region size
  856. *
  857. * The described resource region must match a currently busy region.
  858. */
  859. void __release_region(struct resource *parent, resource_size_t start,
  860. resource_size_t n)
  861. {
  862. struct resource **p;
  863. resource_size_t end;
  864. p = &parent->child;
  865. end = start + n - 1;
  866. write_lock(&resource_lock);
  867. for (;;) {
  868. struct resource *res = *p;
  869. if (!res)
  870. break;
  871. if (res->start <= start && res->end >= end) {
  872. if (!(res->flags & IORESOURCE_BUSY)) {
  873. p = &res->child;
  874. continue;
  875. }
  876. if (res->start != start || res->end != end)
  877. break;
  878. *p = res->sibling;
  879. write_unlock(&resource_lock);
  880. if (res->flags & IORESOURCE_MUXED)
  881. wake_up(&muxed_resource_wait);
  882. kfree(res);
  883. return;
  884. }
  885. p = &res->sibling;
  886. }
  887. write_unlock(&resource_lock);
  888. printk(KERN_WARNING "Trying to free nonexistent resource "
  889. "<%016llx-%016llx>\n", (unsigned long long)start,
  890. (unsigned long long)end);
  891. }
  892. EXPORT_SYMBOL(__release_region);
  893. /*
  894. * Managed region resource
  895. */
  896. struct region_devres {
  897. struct resource *parent;
  898. resource_size_t start;
  899. resource_size_t n;
  900. };
  901. static void devm_region_release(struct device *dev, void *res)
  902. {
  903. struct region_devres *this = res;
  904. __release_region(this->parent, this->start, this->n);
  905. }
  906. static int devm_region_match(struct device *dev, void *res, void *match_data)
  907. {
  908. struct region_devres *this = res, *match = match_data;
  909. return this->parent == match->parent &&
  910. this->start == match->start && this->n == match->n;
  911. }
  912. struct resource * __devm_request_region(struct device *dev,
  913. struct resource *parent, resource_size_t start,
  914. resource_size_t n, const char *name)
  915. {
  916. struct region_devres *dr = NULL;
  917. struct resource *res;
  918. dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
  919. GFP_KERNEL);
  920. if (!dr)
  921. return NULL;
  922. dr->parent = parent;
  923. dr->start = start;
  924. dr->n = n;
  925. res = __request_region(parent, start, n, name, 0);
  926. if (res)
  927. devres_add(dev, dr);
  928. else
  929. devres_free(dr);
  930. return res;
  931. }
  932. EXPORT_SYMBOL(__devm_request_region);
  933. void __devm_release_region(struct device *dev, struct resource *parent,
  934. resource_size_t start, resource_size_t n)
  935. {
  936. struct region_devres match_data = { parent, start, n };
  937. __release_region(parent, start, n);
  938. WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
  939. &match_data));
  940. }
  941. EXPORT_SYMBOL(__devm_release_region);
  942. /*
  943. * Called from init/main.c to reserve IO ports.
  944. */
  945. #define MAXRESERVE 4
  946. static int __init reserve_setup(char *str)
  947. {
  948. static int reserved;
  949. static struct resource reserve[MAXRESERVE];
  950. for (;;) {
  951. unsigned int io_start, io_num;
  952. int x = reserved;
  953. if (get_option (&str, &io_start) != 2)
  954. break;
  955. if (get_option (&str, &io_num) == 0)
  956. break;
  957. if (x < MAXRESERVE) {
  958. struct resource *res = reserve + x;
  959. res->name = "reserved";
  960. res->start = io_start;
  961. res->end = io_start + io_num - 1;
  962. res->flags = IORESOURCE_BUSY;
  963. res->child = NULL;
  964. if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
  965. reserved = x+1;
  966. }
  967. }
  968. return 1;
  969. }
  970. __setup("reserve=", reserve_setup);
  971. /*
  972. * Check if the requested addr and size spans more than any slot in the
  973. * iomem resource tree.
  974. */
  975. int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
  976. {
  977. struct resource *p = &iomem_resource;
  978. int err = 0;
  979. loff_t l;
  980. read_lock(&resource_lock);
  981. for (p = p->child; p ; p = r_next(NULL, p, &l)) {
  982. /*
  983. * We can probably skip the resources without
  984. * IORESOURCE_IO attribute?
  985. */
  986. if (p->start >= addr + size)
  987. continue;
  988. if (p->end < addr)
  989. continue;
  990. if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
  991. PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
  992. continue;
  993. /*
  994. * if a resource is "BUSY", it's not a hardware resource
  995. * but a driver mapping of such a resource; we don't want
  996. * to warn for those; some drivers legitimately map only
  997. * partial hardware resources. (example: vesafb)
  998. */
  999. if (p->flags & IORESOURCE_BUSY)
  1000. continue;
  1001. printk(KERN_WARNING "resource map sanity check conflict: "
  1002. "0x%llx 0x%llx 0x%llx 0x%llx %s\n",
  1003. (unsigned long long)addr,
  1004. (unsigned long long)(addr + size - 1),
  1005. (unsigned long long)p->start,
  1006. (unsigned long long)p->end,
  1007. p->name);
  1008. err = -1;
  1009. break;
  1010. }
  1011. read_unlock(&resource_lock);
  1012. return err;
  1013. }
  1014. #ifdef CONFIG_STRICT_DEVMEM
  1015. static int strict_iomem_checks = 1;
  1016. #else
  1017. static int strict_iomem_checks;
  1018. #endif
  1019. /*
  1020. * check if an address is reserved in the iomem resource tree
  1021. * returns 1 if reserved, 0 if not reserved.
  1022. */
  1023. int iomem_is_exclusive(u64 addr)
  1024. {
  1025. struct resource *p = &iomem_resource;
  1026. int err = 0;
  1027. loff_t l;
  1028. int size = PAGE_SIZE;
  1029. if (!strict_iomem_checks)
  1030. return 0;
  1031. addr = addr & PAGE_MASK;
  1032. read_lock(&resource_lock);
  1033. for (p = p->child; p ; p = r_next(NULL, p, &l)) {
  1034. /*
  1035. * We can probably skip the resources without
  1036. * IORESOURCE_IO attribute?
  1037. */
  1038. if (p->start >= addr + size)
  1039. break;
  1040. if (p->end < addr)
  1041. continue;
  1042. if (p->flags & IORESOURCE_BUSY &&
  1043. p->flags & IORESOURCE_EXCLUSIVE) {
  1044. err = 1;
  1045. break;
  1046. }
  1047. }
  1048. read_unlock(&resource_lock);
  1049. return err;
  1050. }
  1051. static int __init strict_iomem(char *str)
  1052. {
  1053. if (strstr(str, "relaxed"))
  1054. strict_iomem_checks = 0;
  1055. if (strstr(str, "strict"))
  1056. strict_iomem_checks = 1;
  1057. return 1;
  1058. }
  1059. __setup("iomem=", strict_iomem);