resource.c 37 KB

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