resource.c 40 KB

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