resource.c 27 KB

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