resource.c 37 KB

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