resource.c 40 KB

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