resource.c 36 KB

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