resource.c 40 KB

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