resource.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  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. * region_intersects() - determine intersection of region with known resources
  433. * @start: region start address
  434. * @size: size of region
  435. * @name: name of resource (in iomem_resource)
  436. *
  437. * Check if the specified region partially overlaps or fully eclipses a
  438. * resource identified by @name. Return REGION_DISJOINT if the region
  439. * does not overlap @name, return REGION_MIXED if the region overlaps
  440. * @type and another resource, and return REGION_INTERSECTS if the
  441. * region overlaps @type and no other defined resource. Note, that
  442. * REGION_INTERSECTS is also returned in the case when the specified
  443. * region overlaps RAM and undefined memory holes.
  444. *
  445. * region_intersect() is used by memory remapping functions to ensure
  446. * the user is not remapping RAM and is a vast speed up over walking
  447. * through the resource table page by page.
  448. */
  449. int region_intersects(resource_size_t start, size_t size, const char *name)
  450. {
  451. unsigned long flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  452. resource_size_t end = start + size - 1;
  453. int type = 0; int other = 0;
  454. struct resource *p;
  455. read_lock(&resource_lock);
  456. for (p = iomem_resource.child; p ; p = p->sibling) {
  457. bool is_type = strcmp(p->name, name) == 0 && p->flags == flags;
  458. if (start >= p->start && start <= p->end)
  459. is_type ? type++ : other++;
  460. if (end >= p->start && end <= p->end)
  461. is_type ? type++ : other++;
  462. if (p->start >= start && p->end <= end)
  463. is_type ? type++ : other++;
  464. }
  465. read_unlock(&resource_lock);
  466. if (other == 0)
  467. return type ? REGION_INTERSECTS : REGION_DISJOINT;
  468. if (type)
  469. return REGION_MIXED;
  470. return REGION_DISJOINT;
  471. }
  472. void __weak arch_remove_reservations(struct resource *avail)
  473. {
  474. }
  475. static resource_size_t simple_align_resource(void *data,
  476. const struct resource *avail,
  477. resource_size_t size,
  478. resource_size_t align)
  479. {
  480. return avail->start;
  481. }
  482. static void resource_clip(struct resource *res, resource_size_t min,
  483. resource_size_t max)
  484. {
  485. if (res->start < min)
  486. res->start = min;
  487. if (res->end > max)
  488. res->end = max;
  489. }
  490. /*
  491. * Find empty slot in the resource tree with the given range and
  492. * alignment constraints
  493. */
  494. static int __find_resource(struct resource *root, struct resource *old,
  495. struct resource *new,
  496. resource_size_t size,
  497. struct resource_constraint *constraint)
  498. {
  499. struct resource *this = root->child;
  500. struct resource tmp = *new, avail, alloc;
  501. tmp.start = root->start;
  502. /*
  503. * Skip past an allocated resource that starts at 0, since the assignment
  504. * of this->start - 1 to tmp->end below would cause an underflow.
  505. */
  506. if (this && this->start == root->start) {
  507. tmp.start = (this == old) ? old->start : this->end + 1;
  508. this = this->sibling;
  509. }
  510. for(;;) {
  511. if (this)
  512. tmp.end = (this == old) ? this->end : this->start - 1;
  513. else
  514. tmp.end = root->end;
  515. if (tmp.end < tmp.start)
  516. goto next;
  517. resource_clip(&tmp, constraint->min, constraint->max);
  518. arch_remove_reservations(&tmp);
  519. /* Check for overflow after ALIGN() */
  520. avail.start = ALIGN(tmp.start, constraint->align);
  521. avail.end = tmp.end;
  522. avail.flags = new->flags & ~IORESOURCE_UNSET;
  523. if (avail.start >= tmp.start) {
  524. alloc.flags = avail.flags;
  525. alloc.start = constraint->alignf(constraint->alignf_data, &avail,
  526. size, constraint->align);
  527. alloc.end = alloc.start + size - 1;
  528. if (resource_contains(&avail, &alloc)) {
  529. new->start = alloc.start;
  530. new->end = alloc.end;
  531. return 0;
  532. }
  533. }
  534. next: if (!this || this->end == root->end)
  535. break;
  536. if (this != old)
  537. tmp.start = this->end + 1;
  538. this = this->sibling;
  539. }
  540. return -EBUSY;
  541. }
  542. /*
  543. * Find empty slot in the resource tree given range and alignment.
  544. */
  545. static int find_resource(struct resource *root, struct resource *new,
  546. resource_size_t size,
  547. struct resource_constraint *constraint)
  548. {
  549. return __find_resource(root, NULL, new, size, constraint);
  550. }
  551. /**
  552. * reallocate_resource - allocate a slot in the resource tree given range & alignment.
  553. * The resource will be relocated if the new size cannot be reallocated in the
  554. * current location.
  555. *
  556. * @root: root resource descriptor
  557. * @old: resource descriptor desired by caller
  558. * @newsize: new size of the resource descriptor
  559. * @constraint: the size and alignment constraints to be met.
  560. */
  561. static int reallocate_resource(struct resource *root, struct resource *old,
  562. resource_size_t newsize,
  563. struct resource_constraint *constraint)
  564. {
  565. int err=0;
  566. struct resource new = *old;
  567. struct resource *conflict;
  568. write_lock(&resource_lock);
  569. if ((err = __find_resource(root, old, &new, newsize, constraint)))
  570. goto out;
  571. if (resource_contains(&new, old)) {
  572. old->start = new.start;
  573. old->end = new.end;
  574. goto out;
  575. }
  576. if (old->child) {
  577. err = -EBUSY;
  578. goto out;
  579. }
  580. if (resource_contains(old, &new)) {
  581. old->start = new.start;
  582. old->end = new.end;
  583. } else {
  584. __release_resource(old);
  585. *old = new;
  586. conflict = __request_resource(root, old);
  587. BUG_ON(conflict);
  588. }
  589. out:
  590. write_unlock(&resource_lock);
  591. return err;
  592. }
  593. /**
  594. * allocate_resource - allocate empty slot in the resource tree given range & alignment.
  595. * The resource will be reallocated with a new size if it was already allocated
  596. * @root: root resource descriptor
  597. * @new: resource descriptor desired by caller
  598. * @size: requested resource region size
  599. * @min: minimum boundary to allocate
  600. * @max: maximum boundary to allocate
  601. * @align: alignment requested, in bytes
  602. * @alignf: alignment function, optional, called if not NULL
  603. * @alignf_data: arbitrary data to pass to the @alignf function
  604. */
  605. int allocate_resource(struct resource *root, struct resource *new,
  606. resource_size_t size, resource_size_t min,
  607. resource_size_t max, resource_size_t align,
  608. resource_size_t (*alignf)(void *,
  609. const struct resource *,
  610. resource_size_t,
  611. resource_size_t),
  612. void *alignf_data)
  613. {
  614. int err;
  615. struct resource_constraint constraint;
  616. if (!alignf)
  617. alignf = simple_align_resource;
  618. constraint.min = min;
  619. constraint.max = max;
  620. constraint.align = align;
  621. constraint.alignf = alignf;
  622. constraint.alignf_data = alignf_data;
  623. if ( new->parent ) {
  624. /* resource is already allocated, try reallocating with
  625. the new constraints */
  626. return reallocate_resource(root, new, size, &constraint);
  627. }
  628. write_lock(&resource_lock);
  629. err = find_resource(root, new, size, &constraint);
  630. if (err >= 0 && __request_resource(root, new))
  631. err = -EBUSY;
  632. write_unlock(&resource_lock);
  633. return err;
  634. }
  635. EXPORT_SYMBOL(allocate_resource);
  636. /**
  637. * lookup_resource - find an existing resource by a resource start address
  638. * @root: root resource descriptor
  639. * @start: resource start address
  640. *
  641. * Returns a pointer to the resource if found, NULL otherwise
  642. */
  643. struct resource *lookup_resource(struct resource *root, resource_size_t start)
  644. {
  645. struct resource *res;
  646. read_lock(&resource_lock);
  647. for (res = root->child; res; res = res->sibling) {
  648. if (res->start == start)
  649. break;
  650. }
  651. read_unlock(&resource_lock);
  652. return res;
  653. }
  654. /*
  655. * Insert a resource into the resource tree. If successful, return NULL,
  656. * otherwise return the conflicting resource (compare to __request_resource())
  657. */
  658. static struct resource * __insert_resource(struct resource *parent, struct resource *new)
  659. {
  660. struct resource *first, *next;
  661. for (;; parent = first) {
  662. first = __request_resource(parent, new);
  663. if (!first)
  664. return first;
  665. if (first == parent)
  666. return first;
  667. if (WARN_ON(first == new)) /* duplicated insertion */
  668. return first;
  669. if ((first->start > new->start) || (first->end < new->end))
  670. break;
  671. if ((first->start == new->start) && (first->end == new->end))
  672. break;
  673. }
  674. for (next = first; ; next = next->sibling) {
  675. /* Partial overlap? Bad, and unfixable */
  676. if (next->start < new->start || next->end > new->end)
  677. return next;
  678. if (!next->sibling)
  679. break;
  680. if (next->sibling->start > new->end)
  681. break;
  682. }
  683. new->parent = parent;
  684. new->sibling = next->sibling;
  685. new->child = first;
  686. next->sibling = NULL;
  687. for (next = first; next; next = next->sibling)
  688. next->parent = new;
  689. if (parent->child == first) {
  690. parent->child = new;
  691. } else {
  692. next = parent->child;
  693. while (next->sibling != first)
  694. next = next->sibling;
  695. next->sibling = new;
  696. }
  697. return NULL;
  698. }
  699. /**
  700. * insert_resource_conflict - Inserts resource in the resource tree
  701. * @parent: parent of the new resource
  702. * @new: new resource to insert
  703. *
  704. * Returns 0 on success, conflict resource if the resource can't be inserted.
  705. *
  706. * This function is equivalent to request_resource_conflict when no conflict
  707. * happens. If a conflict happens, and the conflicting resources
  708. * entirely fit within the range of the new resource, then the new
  709. * resource is inserted and the conflicting resources become children of
  710. * the new resource.
  711. */
  712. struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
  713. {
  714. struct resource *conflict;
  715. write_lock(&resource_lock);
  716. conflict = __insert_resource(parent, new);
  717. write_unlock(&resource_lock);
  718. return conflict;
  719. }
  720. /**
  721. * insert_resource - Inserts a resource in the resource tree
  722. * @parent: parent of the new resource
  723. * @new: new resource to insert
  724. *
  725. * Returns 0 on success, -EBUSY if the resource can't be inserted.
  726. */
  727. int insert_resource(struct resource *parent, struct resource *new)
  728. {
  729. struct resource *conflict;
  730. conflict = insert_resource_conflict(parent, new);
  731. return conflict ? -EBUSY : 0;
  732. }
  733. /**
  734. * insert_resource_expand_to_fit - Insert a resource into the resource tree
  735. * @root: root resource descriptor
  736. * @new: new resource to insert
  737. *
  738. * Insert a resource into the resource tree, possibly expanding it in order
  739. * to make it encompass any conflicting resources.
  740. */
  741. void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
  742. {
  743. if (new->parent)
  744. return;
  745. write_lock(&resource_lock);
  746. for (;;) {
  747. struct resource *conflict;
  748. conflict = __insert_resource(root, new);
  749. if (!conflict)
  750. break;
  751. if (conflict == root)
  752. break;
  753. /* Ok, expand resource to cover the conflict, then try again .. */
  754. if (conflict->start < new->start)
  755. new->start = conflict->start;
  756. if (conflict->end > new->end)
  757. new->end = conflict->end;
  758. printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
  759. }
  760. write_unlock(&resource_lock);
  761. }
  762. static int __adjust_resource(struct resource *res, resource_size_t start,
  763. resource_size_t size)
  764. {
  765. struct resource *tmp, *parent = res->parent;
  766. resource_size_t end = start + size - 1;
  767. int result = -EBUSY;
  768. if (!parent)
  769. goto skip;
  770. if ((start < parent->start) || (end > parent->end))
  771. goto out;
  772. if (res->sibling && (res->sibling->start <= end))
  773. goto out;
  774. tmp = parent->child;
  775. if (tmp != res) {
  776. while (tmp->sibling != res)
  777. tmp = tmp->sibling;
  778. if (start <= tmp->end)
  779. goto out;
  780. }
  781. skip:
  782. for (tmp = res->child; tmp; tmp = tmp->sibling)
  783. if ((tmp->start < start) || (tmp->end > end))
  784. goto out;
  785. res->start = start;
  786. res->end = end;
  787. result = 0;
  788. out:
  789. return result;
  790. }
  791. /**
  792. * adjust_resource - modify a resource's start and size
  793. * @res: resource to modify
  794. * @start: new start value
  795. * @size: new size
  796. *
  797. * Given an existing resource, change its start and size to match the
  798. * arguments. Returns 0 on success, -EBUSY if it can't fit.
  799. * Existing children of the resource are assumed to be immutable.
  800. */
  801. int adjust_resource(struct resource *res, resource_size_t start,
  802. resource_size_t size)
  803. {
  804. int result;
  805. write_lock(&resource_lock);
  806. result = __adjust_resource(res, start, size);
  807. write_unlock(&resource_lock);
  808. return result;
  809. }
  810. EXPORT_SYMBOL(adjust_resource);
  811. static void __init __reserve_region_with_split(struct resource *root,
  812. resource_size_t start, resource_size_t end,
  813. const char *name)
  814. {
  815. struct resource *parent = root;
  816. struct resource *conflict;
  817. struct resource *res = alloc_resource(GFP_ATOMIC);
  818. struct resource *next_res = NULL;
  819. if (!res)
  820. return;
  821. res->name = name;
  822. res->start = start;
  823. res->end = end;
  824. res->flags = IORESOURCE_BUSY;
  825. while (1) {
  826. conflict = __request_resource(parent, res);
  827. if (!conflict) {
  828. if (!next_res)
  829. break;
  830. res = next_res;
  831. next_res = NULL;
  832. continue;
  833. }
  834. /* conflict covered whole area */
  835. if (conflict->start <= res->start &&
  836. conflict->end >= res->end) {
  837. free_resource(res);
  838. WARN_ON(next_res);
  839. break;
  840. }
  841. /* failed, split and try again */
  842. if (conflict->start > res->start) {
  843. end = res->end;
  844. res->end = conflict->start - 1;
  845. if (conflict->end < end) {
  846. next_res = alloc_resource(GFP_ATOMIC);
  847. if (!next_res) {
  848. free_resource(res);
  849. break;
  850. }
  851. next_res->name = name;
  852. next_res->start = conflict->end + 1;
  853. next_res->end = end;
  854. next_res->flags = IORESOURCE_BUSY;
  855. }
  856. } else {
  857. res->start = conflict->end + 1;
  858. }
  859. }
  860. }
  861. void __init reserve_region_with_split(struct resource *root,
  862. resource_size_t start, resource_size_t end,
  863. const char *name)
  864. {
  865. int abort = 0;
  866. write_lock(&resource_lock);
  867. if (root->start > start || root->end < end) {
  868. pr_err("requested range [0x%llx-0x%llx] not in root %pr\n",
  869. (unsigned long long)start, (unsigned long long)end,
  870. root);
  871. if (start > root->end || end < root->start)
  872. abort = 1;
  873. else {
  874. if (end > root->end)
  875. end = root->end;
  876. if (start < root->start)
  877. start = root->start;
  878. pr_err("fixing request to [0x%llx-0x%llx]\n",
  879. (unsigned long long)start,
  880. (unsigned long long)end);
  881. }
  882. dump_stack();
  883. }
  884. if (!abort)
  885. __reserve_region_with_split(root, start, end, name);
  886. write_unlock(&resource_lock);
  887. }
  888. /**
  889. * resource_alignment - calculate resource's alignment
  890. * @res: resource pointer
  891. *
  892. * Returns alignment on success, 0 (invalid alignment) on failure.
  893. */
  894. resource_size_t resource_alignment(struct resource *res)
  895. {
  896. switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
  897. case IORESOURCE_SIZEALIGN:
  898. return resource_size(res);
  899. case IORESOURCE_STARTALIGN:
  900. return res->start;
  901. default:
  902. return 0;
  903. }
  904. }
  905. /*
  906. * This is compatibility stuff for IO resources.
  907. *
  908. * Note how this, unlike the above, knows about
  909. * the IO flag meanings (busy etc).
  910. *
  911. * request_region creates a new busy region.
  912. *
  913. * release_region releases a matching busy region.
  914. */
  915. static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
  916. /**
  917. * __request_region - create a new busy resource region
  918. * @parent: parent resource descriptor
  919. * @start: resource start address
  920. * @n: resource region size
  921. * @name: reserving caller's ID string
  922. * @flags: IO resource flags
  923. */
  924. struct resource * __request_region(struct resource *parent,
  925. resource_size_t start, resource_size_t n,
  926. const char *name, int flags)
  927. {
  928. DECLARE_WAITQUEUE(wait, current);
  929. struct resource *res = alloc_resource(GFP_KERNEL);
  930. if (!res)
  931. return NULL;
  932. res->name = name;
  933. res->start = start;
  934. res->end = start + n - 1;
  935. res->flags = resource_type(parent);
  936. res->flags |= IORESOURCE_BUSY | flags;
  937. write_lock(&resource_lock);
  938. for (;;) {
  939. struct resource *conflict;
  940. conflict = __request_resource(parent, res);
  941. if (!conflict)
  942. break;
  943. if (conflict != parent) {
  944. parent = conflict;
  945. if (!(conflict->flags & IORESOURCE_BUSY))
  946. continue;
  947. }
  948. if (conflict->flags & flags & IORESOURCE_MUXED) {
  949. add_wait_queue(&muxed_resource_wait, &wait);
  950. write_unlock(&resource_lock);
  951. set_current_state(TASK_UNINTERRUPTIBLE);
  952. schedule();
  953. remove_wait_queue(&muxed_resource_wait, &wait);
  954. write_lock(&resource_lock);
  955. continue;
  956. }
  957. /* Uhhuh, that didn't work out.. */
  958. free_resource(res);
  959. res = NULL;
  960. break;
  961. }
  962. write_unlock(&resource_lock);
  963. return res;
  964. }
  965. EXPORT_SYMBOL(__request_region);
  966. /**
  967. * __release_region - release a previously reserved resource region
  968. * @parent: parent resource descriptor
  969. * @start: resource start address
  970. * @n: resource region size
  971. *
  972. * The described resource region must match a currently busy region.
  973. */
  974. void __release_region(struct resource *parent, resource_size_t start,
  975. resource_size_t n)
  976. {
  977. struct resource **p;
  978. resource_size_t end;
  979. p = &parent->child;
  980. end = start + n - 1;
  981. write_lock(&resource_lock);
  982. for (;;) {
  983. struct resource *res = *p;
  984. if (!res)
  985. break;
  986. if (res->start <= start && res->end >= end) {
  987. if (!(res->flags & IORESOURCE_BUSY)) {
  988. p = &res->child;
  989. continue;
  990. }
  991. if (res->start != start || res->end != end)
  992. break;
  993. *p = res->sibling;
  994. write_unlock(&resource_lock);
  995. if (res->flags & IORESOURCE_MUXED)
  996. wake_up(&muxed_resource_wait);
  997. free_resource(res);
  998. return;
  999. }
  1000. p = &res->sibling;
  1001. }
  1002. write_unlock(&resource_lock);
  1003. printk(KERN_WARNING "Trying to free nonexistent resource "
  1004. "<%016llx-%016llx>\n", (unsigned long long)start,
  1005. (unsigned long long)end);
  1006. }
  1007. EXPORT_SYMBOL(__release_region);
  1008. #ifdef CONFIG_MEMORY_HOTREMOVE
  1009. /**
  1010. * release_mem_region_adjustable - release a previously reserved memory region
  1011. * @parent: parent resource descriptor
  1012. * @start: resource start address
  1013. * @size: resource region size
  1014. *
  1015. * This interface is intended for memory hot-delete. The requested region
  1016. * is released from a currently busy memory resource. The requested region
  1017. * must either match exactly or fit into a single busy resource entry. In
  1018. * the latter case, the remaining resource is adjusted accordingly.
  1019. * Existing children of the busy memory resource must be immutable in the
  1020. * request.
  1021. *
  1022. * Note:
  1023. * - Additional release conditions, such as overlapping region, can be
  1024. * supported after they are confirmed as valid cases.
  1025. * - When a busy memory resource gets split into two entries, the code
  1026. * assumes that all children remain in the lower address entry for
  1027. * simplicity. Enhance this logic when necessary.
  1028. */
  1029. int release_mem_region_adjustable(struct resource *parent,
  1030. resource_size_t start, resource_size_t size)
  1031. {
  1032. struct resource **p;
  1033. struct resource *res;
  1034. struct resource *new_res;
  1035. resource_size_t end;
  1036. int ret = -EINVAL;
  1037. end = start + size - 1;
  1038. if ((start < parent->start) || (end > parent->end))
  1039. return ret;
  1040. /* The alloc_resource() result gets checked later */
  1041. new_res = alloc_resource(GFP_KERNEL);
  1042. p = &parent->child;
  1043. write_lock(&resource_lock);
  1044. while ((res = *p)) {
  1045. if (res->start >= end)
  1046. break;
  1047. /* look for the next resource if it does not fit into */
  1048. if (res->start > start || res->end < end) {
  1049. p = &res->sibling;
  1050. continue;
  1051. }
  1052. if (!(res->flags & IORESOURCE_MEM))
  1053. break;
  1054. if (!(res->flags & IORESOURCE_BUSY)) {
  1055. p = &res->child;
  1056. continue;
  1057. }
  1058. /* found the target resource; let's adjust accordingly */
  1059. if (res->start == start && res->end == end) {
  1060. /* free the whole entry */
  1061. *p = res->sibling;
  1062. free_resource(res);
  1063. ret = 0;
  1064. } else if (res->start == start && res->end != end) {
  1065. /* adjust the start */
  1066. ret = __adjust_resource(res, end + 1,
  1067. res->end - end);
  1068. } else if (res->start != start && res->end == end) {
  1069. /* adjust the end */
  1070. ret = __adjust_resource(res, res->start,
  1071. start - res->start);
  1072. } else {
  1073. /* split into two entries */
  1074. if (!new_res) {
  1075. ret = -ENOMEM;
  1076. break;
  1077. }
  1078. new_res->name = res->name;
  1079. new_res->start = end + 1;
  1080. new_res->end = res->end;
  1081. new_res->flags = res->flags;
  1082. new_res->parent = res->parent;
  1083. new_res->sibling = res->sibling;
  1084. new_res->child = NULL;
  1085. ret = __adjust_resource(res, res->start,
  1086. start - res->start);
  1087. if (ret)
  1088. break;
  1089. res->sibling = new_res;
  1090. new_res = NULL;
  1091. }
  1092. break;
  1093. }
  1094. write_unlock(&resource_lock);
  1095. free_resource(new_res);
  1096. return ret;
  1097. }
  1098. #endif /* CONFIG_MEMORY_HOTREMOVE */
  1099. /*
  1100. * Managed region resource
  1101. */
  1102. static void devm_resource_release(struct device *dev, void *ptr)
  1103. {
  1104. struct resource **r = ptr;
  1105. release_resource(*r);
  1106. }
  1107. /**
  1108. * devm_request_resource() - request and reserve an I/O or memory resource
  1109. * @dev: device for which to request the resource
  1110. * @root: root of the resource tree from which to request the resource
  1111. * @new: descriptor of the resource to request
  1112. *
  1113. * This is a device-managed version of request_resource(). There is usually
  1114. * no need to release resources requested by this function explicitly since
  1115. * that will be taken care of when the device is unbound from its driver.
  1116. * If for some reason the resource needs to be released explicitly, because
  1117. * of ordering issues for example, drivers must call devm_release_resource()
  1118. * rather than the regular release_resource().
  1119. *
  1120. * When a conflict is detected between any existing resources and the newly
  1121. * requested resource, an error message will be printed.
  1122. *
  1123. * Returns 0 on success or a negative error code on failure.
  1124. */
  1125. int devm_request_resource(struct device *dev, struct resource *root,
  1126. struct resource *new)
  1127. {
  1128. struct resource *conflict, **ptr;
  1129. ptr = devres_alloc(devm_resource_release, sizeof(*ptr), GFP_KERNEL);
  1130. if (!ptr)
  1131. return -ENOMEM;
  1132. *ptr = new;
  1133. conflict = request_resource_conflict(root, new);
  1134. if (conflict) {
  1135. dev_err(dev, "resource collision: %pR conflicts with %s %pR\n",
  1136. new, conflict->name, conflict);
  1137. devres_free(ptr);
  1138. return -EBUSY;
  1139. }
  1140. devres_add(dev, ptr);
  1141. return 0;
  1142. }
  1143. EXPORT_SYMBOL(devm_request_resource);
  1144. static int devm_resource_match(struct device *dev, void *res, void *data)
  1145. {
  1146. struct resource **ptr = res;
  1147. return *ptr == data;
  1148. }
  1149. /**
  1150. * devm_release_resource() - release a previously requested resource
  1151. * @dev: device for which to release the resource
  1152. * @new: descriptor of the resource to release
  1153. *
  1154. * Releases a resource previously requested using devm_request_resource().
  1155. */
  1156. void devm_release_resource(struct device *dev, struct resource *new)
  1157. {
  1158. WARN_ON(devres_release(dev, devm_resource_release, devm_resource_match,
  1159. new));
  1160. }
  1161. EXPORT_SYMBOL(devm_release_resource);
  1162. struct region_devres {
  1163. struct resource *parent;
  1164. resource_size_t start;
  1165. resource_size_t n;
  1166. };
  1167. static void devm_region_release(struct device *dev, void *res)
  1168. {
  1169. struct region_devres *this = res;
  1170. __release_region(this->parent, this->start, this->n);
  1171. }
  1172. static int devm_region_match(struct device *dev, void *res, void *match_data)
  1173. {
  1174. struct region_devres *this = res, *match = match_data;
  1175. return this->parent == match->parent &&
  1176. this->start == match->start && this->n == match->n;
  1177. }
  1178. struct resource * __devm_request_region(struct device *dev,
  1179. struct resource *parent, resource_size_t start,
  1180. resource_size_t n, const char *name)
  1181. {
  1182. struct region_devres *dr = NULL;
  1183. struct resource *res;
  1184. dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
  1185. GFP_KERNEL);
  1186. if (!dr)
  1187. return NULL;
  1188. dr->parent = parent;
  1189. dr->start = start;
  1190. dr->n = n;
  1191. res = __request_region(parent, start, n, name, 0);
  1192. if (res)
  1193. devres_add(dev, dr);
  1194. else
  1195. devres_free(dr);
  1196. return res;
  1197. }
  1198. EXPORT_SYMBOL(__devm_request_region);
  1199. void __devm_release_region(struct device *dev, struct resource *parent,
  1200. resource_size_t start, resource_size_t n)
  1201. {
  1202. struct region_devres match_data = { parent, start, n };
  1203. __release_region(parent, start, n);
  1204. WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
  1205. &match_data));
  1206. }
  1207. EXPORT_SYMBOL(__devm_release_region);
  1208. /*
  1209. * Called from init/main.c to reserve IO ports.
  1210. */
  1211. #define MAXRESERVE 4
  1212. static int __init reserve_setup(char *str)
  1213. {
  1214. static int reserved;
  1215. static struct resource reserve[MAXRESERVE];
  1216. for (;;) {
  1217. unsigned int io_start, io_num;
  1218. int x = reserved;
  1219. if (get_option (&str, &io_start) != 2)
  1220. break;
  1221. if (get_option (&str, &io_num) == 0)
  1222. break;
  1223. if (x < MAXRESERVE) {
  1224. struct resource *res = reserve + x;
  1225. res->name = "reserved";
  1226. res->start = io_start;
  1227. res->end = io_start + io_num - 1;
  1228. res->flags = IORESOURCE_BUSY;
  1229. res->child = NULL;
  1230. if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
  1231. reserved = x+1;
  1232. }
  1233. }
  1234. return 1;
  1235. }
  1236. __setup("reserve=", reserve_setup);
  1237. /*
  1238. * Check if the requested addr and size spans more than any slot in the
  1239. * iomem resource tree.
  1240. */
  1241. int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
  1242. {
  1243. struct resource *p = &iomem_resource;
  1244. int err = 0;
  1245. loff_t l;
  1246. read_lock(&resource_lock);
  1247. for (p = p->child; p ; p = r_next(NULL, p, &l)) {
  1248. /*
  1249. * We can probably skip the resources without
  1250. * IORESOURCE_IO attribute?
  1251. */
  1252. if (p->start >= addr + size)
  1253. continue;
  1254. if (p->end < addr)
  1255. continue;
  1256. if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
  1257. PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
  1258. continue;
  1259. /*
  1260. * if a resource is "BUSY", it's not a hardware resource
  1261. * but a driver mapping of such a resource; we don't want
  1262. * to warn for those; some drivers legitimately map only
  1263. * partial hardware resources. (example: vesafb)
  1264. */
  1265. if (p->flags & IORESOURCE_BUSY)
  1266. continue;
  1267. printk(KERN_WARNING "resource sanity check: requesting [mem %#010llx-%#010llx], which spans more than %s %pR\n",
  1268. (unsigned long long)addr,
  1269. (unsigned long long)(addr + size - 1),
  1270. p->name, p);
  1271. err = -1;
  1272. break;
  1273. }
  1274. read_unlock(&resource_lock);
  1275. return err;
  1276. }
  1277. #ifdef CONFIG_STRICT_DEVMEM
  1278. static int strict_iomem_checks = 1;
  1279. #else
  1280. static int strict_iomem_checks;
  1281. #endif
  1282. /*
  1283. * check if an address is reserved in the iomem resource tree
  1284. * returns 1 if reserved, 0 if not reserved.
  1285. */
  1286. int iomem_is_exclusive(u64 addr)
  1287. {
  1288. struct resource *p = &iomem_resource;
  1289. int err = 0;
  1290. loff_t l;
  1291. int size = PAGE_SIZE;
  1292. if (!strict_iomem_checks)
  1293. return 0;
  1294. addr = addr & PAGE_MASK;
  1295. read_lock(&resource_lock);
  1296. for (p = p->child; p ; p = r_next(NULL, p, &l)) {
  1297. /*
  1298. * We can probably skip the resources without
  1299. * IORESOURCE_IO attribute?
  1300. */
  1301. if (p->start >= addr + size)
  1302. break;
  1303. if (p->end < addr)
  1304. continue;
  1305. /*
  1306. * A resource is exclusive if IORESOURCE_EXCLUSIVE is set
  1307. * or CONFIG_IO_STRICT_DEVMEM is enabled and the
  1308. * resource is busy.
  1309. */
  1310. if ((p->flags & IORESOURCE_BUSY) == 0)
  1311. continue;
  1312. if (IS_ENABLED(CONFIG_IO_STRICT_DEVMEM)
  1313. || p->flags & IORESOURCE_EXCLUSIVE) {
  1314. err = 1;
  1315. break;
  1316. }
  1317. }
  1318. read_unlock(&resource_lock);
  1319. return err;
  1320. }
  1321. struct resource_entry *resource_list_create_entry(struct resource *res,
  1322. size_t extra_size)
  1323. {
  1324. struct resource_entry *entry;
  1325. entry = kzalloc(sizeof(*entry) + extra_size, GFP_KERNEL);
  1326. if (entry) {
  1327. INIT_LIST_HEAD(&entry->node);
  1328. entry->res = res ? res : &entry->__res;
  1329. }
  1330. return entry;
  1331. }
  1332. EXPORT_SYMBOL(resource_list_create_entry);
  1333. void resource_list_free(struct list_head *head)
  1334. {
  1335. struct resource_entry *entry, *tmp;
  1336. list_for_each_entry_safe(entry, tmp, head, node)
  1337. resource_list_destroy_entry(entry);
  1338. }
  1339. EXPORT_SYMBOL(resource_list_free);
  1340. static int __init strict_iomem(char *str)
  1341. {
  1342. if (strstr(str, "relaxed"))
  1343. strict_iomem_checks = 0;
  1344. if (strstr(str, "strict"))
  1345. strict_iomem_checks = 1;
  1346. return 1;
  1347. }
  1348. __setup("iomem=", strict_iomem);