resource.c 40 KB

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