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