resource.c 32 KB

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