resource.c 32 KB

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