resource.c 45 KB

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