resource.c 40 KB

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