resource.c 40 KB

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