resource.c 34 KB

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