resource.c 37 KB

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