resource.c 39 KB

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