resource.c 39 KB

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