resource.c 21 KB

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