resource.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  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. * Search for a resouce entry that fully contains the specified region.
  433. * If found, return 1 if it is RAM, 0 if not.
  434. * If not found, or region is not fully contained, return -1
  435. *
  436. * Used by the ioremap functions to ensure the user is not remapping RAM and is
  437. * a vast speed up over walking through the resource table page by page.
  438. */
  439. int region_is_ram(resource_size_t start, unsigned long size)
  440. {
  441. struct resource *p;
  442. resource_size_t end = start + size - 1;
  443. int flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  444. const char *name = "System RAM";
  445. int ret = -1;
  446. read_lock(&resource_lock);
  447. for (p = iomem_resource.child; p ; p = p->sibling) {
  448. if (end < p->start)
  449. continue;
  450. if (p->start <= start && end <= p->end) {
  451. /* resource fully contains region */
  452. if ((p->flags != flags) || strcmp(p->name, name))
  453. ret = 0;
  454. else
  455. ret = 1;
  456. break;
  457. }
  458. if (p->end < start)
  459. break; /* not found */
  460. }
  461. read_unlock(&resource_lock);
  462. return ret;
  463. }
  464. void __weak arch_remove_reservations(struct resource *avail)
  465. {
  466. }
  467. static resource_size_t simple_align_resource(void *data,
  468. const struct resource *avail,
  469. resource_size_t size,
  470. resource_size_t align)
  471. {
  472. return avail->start;
  473. }
  474. static void resource_clip(struct resource *res, resource_size_t min,
  475. resource_size_t max)
  476. {
  477. if (res->start < min)
  478. res->start = min;
  479. if (res->end > max)
  480. res->end = max;
  481. }
  482. /*
  483. * Find empty slot in the resource tree with the given range and
  484. * alignment constraints
  485. */
  486. static int __find_resource(struct resource *root, struct resource *old,
  487. struct resource *new,
  488. resource_size_t size,
  489. struct resource_constraint *constraint)
  490. {
  491. struct resource *this = root->child;
  492. struct resource tmp = *new, avail, alloc;
  493. tmp.start = root->start;
  494. /*
  495. * Skip past an allocated resource that starts at 0, since the assignment
  496. * of this->start - 1 to tmp->end below would cause an underflow.
  497. */
  498. if (this && this->start == root->start) {
  499. tmp.start = (this == old) ? old->start : this->end + 1;
  500. this = this->sibling;
  501. }
  502. for(;;) {
  503. if (this)
  504. tmp.end = (this == old) ? this->end : this->start - 1;
  505. else
  506. tmp.end = root->end;
  507. if (tmp.end < tmp.start)
  508. goto next;
  509. resource_clip(&tmp, constraint->min, constraint->max);
  510. arch_remove_reservations(&tmp);
  511. /* Check for overflow after ALIGN() */
  512. avail.start = ALIGN(tmp.start, constraint->align);
  513. avail.end = tmp.end;
  514. avail.flags = new->flags & ~IORESOURCE_UNSET;
  515. if (avail.start >= tmp.start) {
  516. alloc.flags = avail.flags;
  517. alloc.start = constraint->alignf(constraint->alignf_data, &avail,
  518. size, constraint->align);
  519. alloc.end = alloc.start + size - 1;
  520. if (resource_contains(&avail, &alloc)) {
  521. new->start = alloc.start;
  522. new->end = alloc.end;
  523. return 0;
  524. }
  525. }
  526. next: if (!this || this->end == root->end)
  527. break;
  528. if (this != old)
  529. tmp.start = this->end + 1;
  530. this = this->sibling;
  531. }
  532. return -EBUSY;
  533. }
  534. /*
  535. * Find empty slot in the resource tree given range and alignment.
  536. */
  537. static int find_resource(struct resource *root, struct resource *new,
  538. resource_size_t size,
  539. struct resource_constraint *constraint)
  540. {
  541. return __find_resource(root, NULL, new, size, constraint);
  542. }
  543. /**
  544. * reallocate_resource - allocate a slot in the resource tree given range & alignment.
  545. * The resource will be relocated if the new size cannot be reallocated in the
  546. * current location.
  547. *
  548. * @root: root resource descriptor
  549. * @old: resource descriptor desired by caller
  550. * @newsize: new size of the resource descriptor
  551. * @constraint: the size and alignment constraints to be met.
  552. */
  553. static int reallocate_resource(struct resource *root, struct resource *old,
  554. resource_size_t newsize,
  555. struct resource_constraint *constraint)
  556. {
  557. int err=0;
  558. struct resource new = *old;
  559. struct resource *conflict;
  560. write_lock(&resource_lock);
  561. if ((err = __find_resource(root, old, &new, newsize, constraint)))
  562. goto out;
  563. if (resource_contains(&new, old)) {
  564. old->start = new.start;
  565. old->end = new.end;
  566. goto out;
  567. }
  568. if (old->child) {
  569. err = -EBUSY;
  570. goto out;
  571. }
  572. if (resource_contains(old, &new)) {
  573. old->start = new.start;
  574. old->end = new.end;
  575. } else {
  576. __release_resource(old);
  577. *old = new;
  578. conflict = __request_resource(root, old);
  579. BUG_ON(conflict);
  580. }
  581. out:
  582. write_unlock(&resource_lock);
  583. return err;
  584. }
  585. /**
  586. * allocate_resource - allocate empty slot in the resource tree given range & alignment.
  587. * The resource will be reallocated with a new size if it was already allocated
  588. * @root: root resource descriptor
  589. * @new: resource descriptor desired by caller
  590. * @size: requested resource region size
  591. * @min: minimum boundary to allocate
  592. * @max: maximum boundary to allocate
  593. * @align: alignment requested, in bytes
  594. * @alignf: alignment function, optional, called if not NULL
  595. * @alignf_data: arbitrary data to pass to the @alignf function
  596. */
  597. int allocate_resource(struct resource *root, struct resource *new,
  598. resource_size_t size, resource_size_t min,
  599. resource_size_t max, resource_size_t align,
  600. resource_size_t (*alignf)(void *,
  601. const struct resource *,
  602. resource_size_t,
  603. resource_size_t),
  604. void *alignf_data)
  605. {
  606. int err;
  607. struct resource_constraint constraint;
  608. if (!alignf)
  609. alignf = simple_align_resource;
  610. constraint.min = min;
  611. constraint.max = max;
  612. constraint.align = align;
  613. constraint.alignf = alignf;
  614. constraint.alignf_data = alignf_data;
  615. if ( new->parent ) {
  616. /* resource is already allocated, try reallocating with
  617. the new constraints */
  618. return reallocate_resource(root, new, size, &constraint);
  619. }
  620. write_lock(&resource_lock);
  621. err = find_resource(root, new, size, &constraint);
  622. if (err >= 0 && __request_resource(root, new))
  623. err = -EBUSY;
  624. write_unlock(&resource_lock);
  625. return err;
  626. }
  627. EXPORT_SYMBOL(allocate_resource);
  628. /**
  629. * lookup_resource - find an existing resource by a resource start address
  630. * @root: root resource descriptor
  631. * @start: resource start address
  632. *
  633. * Returns a pointer to the resource if found, NULL otherwise
  634. */
  635. struct resource *lookup_resource(struct resource *root, resource_size_t start)
  636. {
  637. struct resource *res;
  638. read_lock(&resource_lock);
  639. for (res = root->child; res; res = res->sibling) {
  640. if (res->start == start)
  641. break;
  642. }
  643. read_unlock(&resource_lock);
  644. return res;
  645. }
  646. /*
  647. * Insert a resource into the resource tree. If successful, return NULL,
  648. * otherwise return the conflicting resource (compare to __request_resource())
  649. */
  650. static struct resource * __insert_resource(struct resource *parent, struct resource *new)
  651. {
  652. struct resource *first, *next;
  653. for (;; parent = first) {
  654. first = __request_resource(parent, new);
  655. if (!first)
  656. return first;
  657. if (first == parent)
  658. return first;
  659. if (WARN_ON(first == new)) /* duplicated insertion */
  660. return first;
  661. if ((first->start > new->start) || (first->end < new->end))
  662. break;
  663. if ((first->start == new->start) && (first->end == new->end))
  664. break;
  665. }
  666. for (next = first; ; next = next->sibling) {
  667. /* Partial overlap? Bad, and unfixable */
  668. if (next->start < new->start || next->end > new->end)
  669. return next;
  670. if (!next->sibling)
  671. break;
  672. if (next->sibling->start > new->end)
  673. break;
  674. }
  675. new->parent = parent;
  676. new->sibling = next->sibling;
  677. new->child = first;
  678. next->sibling = NULL;
  679. for (next = first; next; next = next->sibling)
  680. next->parent = new;
  681. if (parent->child == first) {
  682. parent->child = new;
  683. } else {
  684. next = parent->child;
  685. while (next->sibling != first)
  686. next = next->sibling;
  687. next->sibling = new;
  688. }
  689. return NULL;
  690. }
  691. /**
  692. * insert_resource_conflict - Inserts resource in the resource tree
  693. * @parent: parent of the new resource
  694. * @new: new resource to insert
  695. *
  696. * Returns 0 on success, conflict resource if the resource can't be inserted.
  697. *
  698. * This function is equivalent to request_resource_conflict when no conflict
  699. * happens. If a conflict happens, and the conflicting resources
  700. * entirely fit within the range of the new resource, then the new
  701. * resource is inserted and the conflicting resources become children of
  702. * the new resource.
  703. */
  704. struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
  705. {
  706. struct resource *conflict;
  707. write_lock(&resource_lock);
  708. conflict = __insert_resource(parent, new);
  709. write_unlock(&resource_lock);
  710. return conflict;
  711. }
  712. /**
  713. * insert_resource - Inserts a resource in the resource tree
  714. * @parent: parent of the new resource
  715. * @new: new resource to insert
  716. *
  717. * Returns 0 on success, -EBUSY if the resource can't be inserted.
  718. */
  719. int insert_resource(struct resource *parent, struct resource *new)
  720. {
  721. struct resource *conflict;
  722. conflict = insert_resource_conflict(parent, new);
  723. return conflict ? -EBUSY : 0;
  724. }
  725. /**
  726. * insert_resource_expand_to_fit - Insert a resource into the resource tree
  727. * @root: root resource descriptor
  728. * @new: new resource to insert
  729. *
  730. * Insert a resource into the resource tree, possibly expanding it in order
  731. * to make it encompass any conflicting resources.
  732. */
  733. void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
  734. {
  735. if (new->parent)
  736. return;
  737. write_lock(&resource_lock);
  738. for (;;) {
  739. struct resource *conflict;
  740. conflict = __insert_resource(root, new);
  741. if (!conflict)
  742. break;
  743. if (conflict == root)
  744. break;
  745. /* Ok, expand resource to cover the conflict, then try again .. */
  746. if (conflict->start < new->start)
  747. new->start = conflict->start;
  748. if (conflict->end > new->end)
  749. new->end = conflict->end;
  750. printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
  751. }
  752. write_unlock(&resource_lock);
  753. }
  754. static int __adjust_resource(struct resource *res, resource_size_t start,
  755. resource_size_t size)
  756. {
  757. struct resource *tmp, *parent = res->parent;
  758. resource_size_t end = start + size - 1;
  759. int result = -EBUSY;
  760. if (!parent)
  761. goto skip;
  762. if ((start < parent->start) || (end > parent->end))
  763. goto out;
  764. if (res->sibling && (res->sibling->start <= end))
  765. goto out;
  766. tmp = parent->child;
  767. if (tmp != res) {
  768. while (tmp->sibling != res)
  769. tmp = tmp->sibling;
  770. if (start <= tmp->end)
  771. goto out;
  772. }
  773. skip:
  774. for (tmp = res->child; tmp; tmp = tmp->sibling)
  775. if ((tmp->start < start) || (tmp->end > end))
  776. goto out;
  777. res->start = start;
  778. res->end = end;
  779. result = 0;
  780. out:
  781. return result;
  782. }
  783. /**
  784. * adjust_resource - modify a resource's start and size
  785. * @res: resource to modify
  786. * @start: new start value
  787. * @size: new size
  788. *
  789. * Given an existing resource, change its start and size to match the
  790. * arguments. Returns 0 on success, -EBUSY if it can't fit.
  791. * Existing children of the resource are assumed to be immutable.
  792. */
  793. int adjust_resource(struct resource *res, resource_size_t start,
  794. resource_size_t size)
  795. {
  796. int result;
  797. write_lock(&resource_lock);
  798. result = __adjust_resource(res, start, size);
  799. write_unlock(&resource_lock);
  800. return result;
  801. }
  802. EXPORT_SYMBOL(adjust_resource);
  803. static void __init __reserve_region_with_split(struct resource *root,
  804. resource_size_t start, resource_size_t end,
  805. const char *name)
  806. {
  807. struct resource *parent = root;
  808. struct resource *conflict;
  809. struct resource *res = alloc_resource(GFP_ATOMIC);
  810. struct resource *next_res = NULL;
  811. if (!res)
  812. return;
  813. res->name = name;
  814. res->start = start;
  815. res->end = end;
  816. res->flags = IORESOURCE_BUSY;
  817. while (1) {
  818. conflict = __request_resource(parent, res);
  819. if (!conflict) {
  820. if (!next_res)
  821. break;
  822. res = next_res;
  823. next_res = NULL;
  824. continue;
  825. }
  826. /* conflict covered whole area */
  827. if (conflict->start <= res->start &&
  828. conflict->end >= res->end) {
  829. free_resource(res);
  830. WARN_ON(next_res);
  831. break;
  832. }
  833. /* failed, split and try again */
  834. if (conflict->start > res->start) {
  835. end = res->end;
  836. res->end = conflict->start - 1;
  837. if (conflict->end < end) {
  838. next_res = alloc_resource(GFP_ATOMIC);
  839. if (!next_res) {
  840. free_resource(res);
  841. break;
  842. }
  843. next_res->name = name;
  844. next_res->start = conflict->end + 1;
  845. next_res->end = end;
  846. next_res->flags = IORESOURCE_BUSY;
  847. }
  848. } else {
  849. res->start = conflict->end + 1;
  850. }
  851. }
  852. }
  853. void __init reserve_region_with_split(struct resource *root,
  854. resource_size_t start, resource_size_t end,
  855. const char *name)
  856. {
  857. int abort = 0;
  858. write_lock(&resource_lock);
  859. if (root->start > start || root->end < end) {
  860. pr_err("requested range [0x%llx-0x%llx] not in root %pr\n",
  861. (unsigned long long)start, (unsigned long long)end,
  862. root);
  863. if (start > root->end || end < root->start)
  864. abort = 1;
  865. else {
  866. if (end > root->end)
  867. end = root->end;
  868. if (start < root->start)
  869. start = root->start;
  870. pr_err("fixing request to [0x%llx-0x%llx]\n",
  871. (unsigned long long)start,
  872. (unsigned long long)end);
  873. }
  874. dump_stack();
  875. }
  876. if (!abort)
  877. __reserve_region_with_split(root, start, end, name);
  878. write_unlock(&resource_lock);
  879. }
  880. /**
  881. * resource_alignment - calculate resource's alignment
  882. * @res: resource pointer
  883. *
  884. * Returns alignment on success, 0 (invalid alignment) on failure.
  885. */
  886. resource_size_t resource_alignment(struct resource *res)
  887. {
  888. switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
  889. case IORESOURCE_SIZEALIGN:
  890. return resource_size(res);
  891. case IORESOURCE_STARTALIGN:
  892. return res->start;
  893. default:
  894. return 0;
  895. }
  896. }
  897. /*
  898. * This is compatibility stuff for IO resources.
  899. *
  900. * Note how this, unlike the above, knows about
  901. * the IO flag meanings (busy etc).
  902. *
  903. * request_region creates a new busy region.
  904. *
  905. * check_region returns non-zero if the area is already busy.
  906. *
  907. * release_region releases a matching busy region.
  908. */
  909. static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
  910. /**
  911. * __request_region - create a new busy resource region
  912. * @parent: parent resource descriptor
  913. * @start: resource start address
  914. * @n: resource region size
  915. * @name: reserving caller's ID string
  916. * @flags: IO resource flags
  917. */
  918. struct resource * __request_region(struct resource *parent,
  919. resource_size_t start, resource_size_t n,
  920. const char *name, int flags)
  921. {
  922. DECLARE_WAITQUEUE(wait, current);
  923. struct resource *res = alloc_resource(GFP_KERNEL);
  924. if (!res)
  925. return NULL;
  926. res->name = name;
  927. res->start = start;
  928. res->end = start + n - 1;
  929. res->flags = resource_type(parent);
  930. res->flags |= IORESOURCE_BUSY | flags;
  931. write_lock(&resource_lock);
  932. for (;;) {
  933. struct resource *conflict;
  934. conflict = __request_resource(parent, res);
  935. if (!conflict)
  936. break;
  937. if (conflict != parent) {
  938. parent = conflict;
  939. if (!(conflict->flags & IORESOURCE_BUSY))
  940. continue;
  941. }
  942. if (conflict->flags & flags & IORESOURCE_MUXED) {
  943. add_wait_queue(&muxed_resource_wait, &wait);
  944. write_unlock(&resource_lock);
  945. set_current_state(TASK_UNINTERRUPTIBLE);
  946. schedule();
  947. remove_wait_queue(&muxed_resource_wait, &wait);
  948. write_lock(&resource_lock);
  949. continue;
  950. }
  951. /* Uhhuh, that didn't work out.. */
  952. free_resource(res);
  953. res = NULL;
  954. break;
  955. }
  956. write_unlock(&resource_lock);
  957. return res;
  958. }
  959. EXPORT_SYMBOL(__request_region);
  960. /**
  961. * __check_region - check if a resource region is busy or free
  962. * @parent: parent resource descriptor
  963. * @start: resource start address
  964. * @n: resource region size
  965. *
  966. * Returns 0 if the region is free at the moment it is checked,
  967. * returns %-EBUSY if the region is busy.
  968. *
  969. * NOTE:
  970. * This function is deprecated because its use is racy.
  971. * Even if it returns 0, a subsequent call to request_region()
  972. * may fail because another driver etc. just allocated the region.
  973. * Do NOT use it. It will be removed from the kernel.
  974. */
  975. int __check_region(struct resource *parent, resource_size_t start,
  976. resource_size_t n)
  977. {
  978. struct resource * res;
  979. res = __request_region(parent, start, n, "check-region", 0);
  980. if (!res)
  981. return -EBUSY;
  982. release_resource(res);
  983. free_resource(res);
  984. return 0;
  985. }
  986. EXPORT_SYMBOL(__check_region);
  987. /**
  988. * __release_region - release a previously reserved resource region
  989. * @parent: parent resource descriptor
  990. * @start: resource start address
  991. * @n: resource region size
  992. *
  993. * The described resource region must match a currently busy region.
  994. */
  995. void __release_region(struct resource *parent, resource_size_t start,
  996. resource_size_t n)
  997. {
  998. struct resource **p;
  999. resource_size_t end;
  1000. p = &parent->child;
  1001. end = start + n - 1;
  1002. write_lock(&resource_lock);
  1003. for (;;) {
  1004. struct resource *res = *p;
  1005. if (!res)
  1006. break;
  1007. if (res->start <= start && res->end >= end) {
  1008. if (!(res->flags & IORESOURCE_BUSY)) {
  1009. p = &res->child;
  1010. continue;
  1011. }
  1012. if (res->start != start || res->end != end)
  1013. break;
  1014. *p = res->sibling;
  1015. write_unlock(&resource_lock);
  1016. if (res->flags & IORESOURCE_MUXED)
  1017. wake_up(&muxed_resource_wait);
  1018. free_resource(res);
  1019. return;
  1020. }
  1021. p = &res->sibling;
  1022. }
  1023. write_unlock(&resource_lock);
  1024. printk(KERN_WARNING "Trying to free nonexistent resource "
  1025. "<%016llx-%016llx>\n", (unsigned long long)start,
  1026. (unsigned long long)end);
  1027. }
  1028. EXPORT_SYMBOL(__release_region);
  1029. #ifdef CONFIG_MEMORY_HOTREMOVE
  1030. /**
  1031. * release_mem_region_adjustable - release a previously reserved memory region
  1032. * @parent: parent resource descriptor
  1033. * @start: resource start address
  1034. * @size: resource region size
  1035. *
  1036. * This interface is intended for memory hot-delete. The requested region
  1037. * is released from a currently busy memory resource. The requested region
  1038. * must either match exactly or fit into a single busy resource entry. In
  1039. * the latter case, the remaining resource is adjusted accordingly.
  1040. * Existing children of the busy memory resource must be immutable in the
  1041. * request.
  1042. *
  1043. * Note:
  1044. * - Additional release conditions, such as overlapping region, can be
  1045. * supported after they are confirmed as valid cases.
  1046. * - When a busy memory resource gets split into two entries, the code
  1047. * assumes that all children remain in the lower address entry for
  1048. * simplicity. Enhance this logic when necessary.
  1049. */
  1050. int release_mem_region_adjustable(struct resource *parent,
  1051. resource_size_t start, resource_size_t size)
  1052. {
  1053. struct resource **p;
  1054. struct resource *res;
  1055. struct resource *new_res;
  1056. resource_size_t end;
  1057. int ret = -EINVAL;
  1058. end = start + size - 1;
  1059. if ((start < parent->start) || (end > parent->end))
  1060. return ret;
  1061. /* The alloc_resource() result gets checked later */
  1062. new_res = alloc_resource(GFP_KERNEL);
  1063. p = &parent->child;
  1064. write_lock(&resource_lock);
  1065. while ((res = *p)) {
  1066. if (res->start >= end)
  1067. break;
  1068. /* look for the next resource if it does not fit into */
  1069. if (res->start > start || res->end < end) {
  1070. p = &res->sibling;
  1071. continue;
  1072. }
  1073. if (!(res->flags & IORESOURCE_MEM))
  1074. break;
  1075. if (!(res->flags & IORESOURCE_BUSY)) {
  1076. p = &res->child;
  1077. continue;
  1078. }
  1079. /* found the target resource; let's adjust accordingly */
  1080. if (res->start == start && res->end == end) {
  1081. /* free the whole entry */
  1082. *p = res->sibling;
  1083. free_resource(res);
  1084. ret = 0;
  1085. } else if (res->start == start && res->end != end) {
  1086. /* adjust the start */
  1087. ret = __adjust_resource(res, end + 1,
  1088. res->end - end);
  1089. } else if (res->start != start && res->end == end) {
  1090. /* adjust the end */
  1091. ret = __adjust_resource(res, res->start,
  1092. start - res->start);
  1093. } else {
  1094. /* split into two entries */
  1095. if (!new_res) {
  1096. ret = -ENOMEM;
  1097. break;
  1098. }
  1099. new_res->name = res->name;
  1100. new_res->start = end + 1;
  1101. new_res->end = res->end;
  1102. new_res->flags = res->flags;
  1103. new_res->parent = res->parent;
  1104. new_res->sibling = res->sibling;
  1105. new_res->child = NULL;
  1106. ret = __adjust_resource(res, res->start,
  1107. start - res->start);
  1108. if (ret)
  1109. break;
  1110. res->sibling = new_res;
  1111. new_res = NULL;
  1112. }
  1113. break;
  1114. }
  1115. write_unlock(&resource_lock);
  1116. free_resource(new_res);
  1117. return ret;
  1118. }
  1119. #endif /* CONFIG_MEMORY_HOTREMOVE */
  1120. /*
  1121. * Managed region resource
  1122. */
  1123. static void devm_resource_release(struct device *dev, void *ptr)
  1124. {
  1125. struct resource **r = ptr;
  1126. release_resource(*r);
  1127. }
  1128. /**
  1129. * devm_request_resource() - request and reserve an I/O or memory resource
  1130. * @dev: device for which to request the resource
  1131. * @root: root of the resource tree from which to request the resource
  1132. * @new: descriptor of the resource to request
  1133. *
  1134. * This is a device-managed version of request_resource(). There is usually
  1135. * no need to release resources requested by this function explicitly since
  1136. * that will be taken care of when the device is unbound from its driver.
  1137. * If for some reason the resource needs to be released explicitly, because
  1138. * of ordering issues for example, drivers must call devm_release_resource()
  1139. * rather than the regular release_resource().
  1140. *
  1141. * When a conflict is detected between any existing resources and the newly
  1142. * requested resource, an error message will be printed.
  1143. *
  1144. * Returns 0 on success or a negative error code on failure.
  1145. */
  1146. int devm_request_resource(struct device *dev, struct resource *root,
  1147. struct resource *new)
  1148. {
  1149. struct resource *conflict, **ptr;
  1150. ptr = devres_alloc(devm_resource_release, sizeof(*ptr), GFP_KERNEL);
  1151. if (!ptr)
  1152. return -ENOMEM;
  1153. *ptr = new;
  1154. conflict = request_resource_conflict(root, new);
  1155. if (conflict) {
  1156. dev_err(dev, "resource collision: %pR conflicts with %s %pR\n",
  1157. new, conflict->name, conflict);
  1158. devres_free(ptr);
  1159. return -EBUSY;
  1160. }
  1161. devres_add(dev, ptr);
  1162. return 0;
  1163. }
  1164. EXPORT_SYMBOL(devm_request_resource);
  1165. static int devm_resource_match(struct device *dev, void *res, void *data)
  1166. {
  1167. struct resource **ptr = res;
  1168. return *ptr == data;
  1169. }
  1170. /**
  1171. * devm_release_resource() - release a previously requested resource
  1172. * @dev: device for which to release the resource
  1173. * @new: descriptor of the resource to release
  1174. *
  1175. * Releases a resource previously requested using devm_request_resource().
  1176. */
  1177. void devm_release_resource(struct device *dev, struct resource *new)
  1178. {
  1179. WARN_ON(devres_release(dev, devm_resource_release, devm_resource_match,
  1180. new));
  1181. }
  1182. EXPORT_SYMBOL(devm_release_resource);
  1183. struct region_devres {
  1184. struct resource *parent;
  1185. resource_size_t start;
  1186. resource_size_t n;
  1187. };
  1188. static void devm_region_release(struct device *dev, void *res)
  1189. {
  1190. struct region_devres *this = res;
  1191. __release_region(this->parent, this->start, this->n);
  1192. }
  1193. static int devm_region_match(struct device *dev, void *res, void *match_data)
  1194. {
  1195. struct region_devres *this = res, *match = match_data;
  1196. return this->parent == match->parent &&
  1197. this->start == match->start && this->n == match->n;
  1198. }
  1199. struct resource * __devm_request_region(struct device *dev,
  1200. struct resource *parent, resource_size_t start,
  1201. resource_size_t n, const char *name)
  1202. {
  1203. struct region_devres *dr = NULL;
  1204. struct resource *res;
  1205. dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
  1206. GFP_KERNEL);
  1207. if (!dr)
  1208. return NULL;
  1209. dr->parent = parent;
  1210. dr->start = start;
  1211. dr->n = n;
  1212. res = __request_region(parent, start, n, name, 0);
  1213. if (res)
  1214. devres_add(dev, dr);
  1215. else
  1216. devres_free(dr);
  1217. return res;
  1218. }
  1219. EXPORT_SYMBOL(__devm_request_region);
  1220. void __devm_release_region(struct device *dev, struct resource *parent,
  1221. resource_size_t start, resource_size_t n)
  1222. {
  1223. struct region_devres match_data = { parent, start, n };
  1224. __release_region(parent, start, n);
  1225. WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
  1226. &match_data));
  1227. }
  1228. EXPORT_SYMBOL(__devm_release_region);
  1229. /*
  1230. * Called from init/main.c to reserve IO ports.
  1231. */
  1232. #define MAXRESERVE 4
  1233. static int __init reserve_setup(char *str)
  1234. {
  1235. static int reserved;
  1236. static struct resource reserve[MAXRESERVE];
  1237. for (;;) {
  1238. unsigned int io_start, io_num;
  1239. int x = reserved;
  1240. if (get_option (&str, &io_start) != 2)
  1241. break;
  1242. if (get_option (&str, &io_num) == 0)
  1243. break;
  1244. if (x < MAXRESERVE) {
  1245. struct resource *res = reserve + x;
  1246. res->name = "reserved";
  1247. res->start = io_start;
  1248. res->end = io_start + io_num - 1;
  1249. res->flags = IORESOURCE_BUSY;
  1250. res->child = NULL;
  1251. if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
  1252. reserved = x+1;
  1253. }
  1254. }
  1255. return 1;
  1256. }
  1257. __setup("reserve=", reserve_setup);
  1258. /*
  1259. * Check if the requested addr and size spans more than any slot in the
  1260. * iomem resource tree.
  1261. */
  1262. int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
  1263. {
  1264. struct resource *p = &iomem_resource;
  1265. int err = 0;
  1266. loff_t l;
  1267. read_lock(&resource_lock);
  1268. for (p = p->child; p ; p = r_next(NULL, p, &l)) {
  1269. /*
  1270. * We can probably skip the resources without
  1271. * IORESOURCE_IO attribute?
  1272. */
  1273. if (p->start >= addr + size)
  1274. continue;
  1275. if (p->end < addr)
  1276. continue;
  1277. if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
  1278. PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
  1279. continue;
  1280. /*
  1281. * if a resource is "BUSY", it's not a hardware resource
  1282. * but a driver mapping of such a resource; we don't want
  1283. * to warn for those; some drivers legitimately map only
  1284. * partial hardware resources. (example: vesafb)
  1285. */
  1286. if (p->flags & IORESOURCE_BUSY)
  1287. continue;
  1288. printk(KERN_WARNING "resource sanity check: requesting [mem %#010llx-%#010llx], which spans more than %s %pR\n",
  1289. (unsigned long long)addr,
  1290. (unsigned long long)(addr + size - 1),
  1291. p->name, p);
  1292. err = -1;
  1293. break;
  1294. }
  1295. read_unlock(&resource_lock);
  1296. return err;
  1297. }
  1298. #ifdef CONFIG_STRICT_DEVMEM
  1299. static int strict_iomem_checks = 1;
  1300. #else
  1301. static int strict_iomem_checks;
  1302. #endif
  1303. /*
  1304. * check if an address is reserved in the iomem resource tree
  1305. * returns 1 if reserved, 0 if not reserved.
  1306. */
  1307. int iomem_is_exclusive(u64 addr)
  1308. {
  1309. struct resource *p = &iomem_resource;
  1310. int err = 0;
  1311. loff_t l;
  1312. int size = PAGE_SIZE;
  1313. if (!strict_iomem_checks)
  1314. return 0;
  1315. addr = addr & PAGE_MASK;
  1316. read_lock(&resource_lock);
  1317. for (p = p->child; p ; p = r_next(NULL, p, &l)) {
  1318. /*
  1319. * We can probably skip the resources without
  1320. * IORESOURCE_IO attribute?
  1321. */
  1322. if (p->start >= addr + size)
  1323. break;
  1324. if (p->end < addr)
  1325. continue;
  1326. if (p->flags & IORESOURCE_BUSY &&
  1327. p->flags & IORESOURCE_EXCLUSIVE) {
  1328. err = 1;
  1329. break;
  1330. }
  1331. }
  1332. read_unlock(&resource_lock);
  1333. return err;
  1334. }
  1335. struct resource_entry *resource_list_create_entry(struct resource *res,
  1336. size_t extra_size)
  1337. {
  1338. struct resource_entry *entry;
  1339. entry = kzalloc(sizeof(*entry) + extra_size, GFP_KERNEL);
  1340. if (entry) {
  1341. INIT_LIST_HEAD(&entry->node);
  1342. entry->res = res ? res : &entry->__res;
  1343. }
  1344. return entry;
  1345. }
  1346. EXPORT_SYMBOL(resource_list_create_entry);
  1347. void resource_list_free(struct list_head *head)
  1348. {
  1349. struct resource_entry *entry, *tmp;
  1350. list_for_each_entry_safe(entry, tmp, head, node)
  1351. resource_list_destroy_entry(entry);
  1352. }
  1353. EXPORT_SYMBOL(resource_list_free);
  1354. static int __init strict_iomem(char *str)
  1355. {
  1356. if (strstr(str, "relaxed"))
  1357. strict_iomem_checks = 0;
  1358. if (strstr(str, "strict"))
  1359. strict_iomem_checks = 1;
  1360. return 1;
  1361. }
  1362. __setup("iomem=", strict_iomem);