resource.c 39 KB

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