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