resource.c 38 KB

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