resource.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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, end_pfn;
  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 = (res.start + PAGE_SIZE - 1) >> PAGE_SHIFT;
  253. end_pfn = (res.end + 1) >> PAGE_SHIFT;
  254. if (end_pfn > pfn)
  255. ret = (*func)(pfn, end_pfn - pfn, arg);
  256. if (ret)
  257. break;
  258. res.start = res.end + 1;
  259. res.end = orig_end;
  260. }
  261. return ret;
  262. }
  263. #endif
  264. static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
  265. {
  266. return 1;
  267. }
  268. /*
  269. * This generic page_is_ram() returns true if specified address is
  270. * registered as "System RAM" in iomem_resource list.
  271. */
  272. int __weak page_is_ram(unsigned long pfn)
  273. {
  274. return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
  275. }
  276. /*
  277. * Find empty slot in the resource tree given range and alignment.
  278. */
  279. static int find_resource(struct resource *root, struct resource *new,
  280. resource_size_t size, resource_size_t min,
  281. resource_size_t max, resource_size_t align,
  282. void (*alignf)(void *, struct resource *,
  283. resource_size_t, resource_size_t),
  284. void *alignf_data)
  285. {
  286. struct resource *this = root->child;
  287. struct resource tmp = *new;
  288. tmp.start = root->start;
  289. /*
  290. * Skip past an allocated resource that starts at 0, since the assignment
  291. * of this->start - 1 to tmp->end below would cause an underflow.
  292. */
  293. if (this && this->start == 0) {
  294. tmp.start = this->end + 1;
  295. this = this->sibling;
  296. }
  297. for(;;) {
  298. if (this)
  299. tmp.end = this->start - 1;
  300. else
  301. tmp.end = root->end;
  302. if (tmp.start < min)
  303. tmp.start = min;
  304. if (tmp.end > max)
  305. tmp.end = max;
  306. tmp.start = ALIGN(tmp.start, align);
  307. if (alignf)
  308. alignf(alignf_data, &tmp, size, align);
  309. if (tmp.start < tmp.end && tmp.end - tmp.start >= size - 1) {
  310. new->start = tmp.start;
  311. new->end = tmp.start + size - 1;
  312. return 0;
  313. }
  314. if (!this)
  315. break;
  316. tmp.start = this->end + 1;
  317. this = this->sibling;
  318. }
  319. return -EBUSY;
  320. }
  321. /**
  322. * allocate_resource - allocate empty slot in the resource tree given range & alignment
  323. * @root: root resource descriptor
  324. * @new: resource descriptor desired by caller
  325. * @size: requested resource region size
  326. * @min: minimum size to allocate
  327. * @max: maximum size to allocate
  328. * @align: alignment requested, in bytes
  329. * @alignf: alignment function, optional, called if not NULL
  330. * @alignf_data: arbitrary data to pass to the @alignf function
  331. */
  332. int allocate_resource(struct resource *root, struct resource *new,
  333. resource_size_t size, resource_size_t min,
  334. resource_size_t max, resource_size_t align,
  335. void (*alignf)(void *, struct resource *,
  336. resource_size_t, resource_size_t),
  337. void *alignf_data)
  338. {
  339. int err;
  340. write_lock(&resource_lock);
  341. err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
  342. if (err >= 0 && __request_resource(root, new))
  343. err = -EBUSY;
  344. write_unlock(&resource_lock);
  345. return err;
  346. }
  347. EXPORT_SYMBOL(allocate_resource);
  348. /*
  349. * Insert a resource into the resource tree. If successful, return NULL,
  350. * otherwise return the conflicting resource (compare to __request_resource())
  351. */
  352. static struct resource * __insert_resource(struct resource *parent, struct resource *new)
  353. {
  354. struct resource *first, *next;
  355. for (;; parent = first) {
  356. first = __request_resource(parent, new);
  357. if (!first)
  358. return first;
  359. if (first == parent)
  360. return first;
  361. if ((first->start > new->start) || (first->end < new->end))
  362. break;
  363. if ((first->start == new->start) && (first->end == new->end))
  364. break;
  365. }
  366. for (next = first; ; next = next->sibling) {
  367. /* Partial overlap? Bad, and unfixable */
  368. if (next->start < new->start || next->end > new->end)
  369. return next;
  370. if (!next->sibling)
  371. break;
  372. if (next->sibling->start > new->end)
  373. break;
  374. }
  375. new->parent = parent;
  376. new->sibling = next->sibling;
  377. new->child = first;
  378. next->sibling = NULL;
  379. for (next = first; next; next = next->sibling)
  380. next->parent = new;
  381. if (parent->child == first) {
  382. parent->child = new;
  383. } else {
  384. next = parent->child;
  385. while (next->sibling != first)
  386. next = next->sibling;
  387. next->sibling = new;
  388. }
  389. return NULL;
  390. }
  391. /**
  392. * insert_resource - Inserts a resource in the resource tree
  393. * @parent: parent of the new resource
  394. * @new: new resource to insert
  395. *
  396. * Returns 0 on success, -EBUSY if the resource can't be inserted.
  397. *
  398. * This function is equivalent to request_resource when no conflict
  399. * happens. If a conflict happens, and the conflicting resources
  400. * entirely fit within the range of the new resource, then the new
  401. * resource is inserted and the conflicting resources become children of
  402. * the new resource.
  403. */
  404. int insert_resource(struct resource *parent, struct resource *new)
  405. {
  406. struct resource *conflict;
  407. write_lock(&resource_lock);
  408. conflict = __insert_resource(parent, new);
  409. write_unlock(&resource_lock);
  410. return conflict ? -EBUSY : 0;
  411. }
  412. /**
  413. * insert_resource_expand_to_fit - Insert a resource into the resource tree
  414. * @root: root resource descriptor
  415. * @new: new resource to insert
  416. *
  417. * Insert a resource into the resource tree, possibly expanding it in order
  418. * to make it encompass any conflicting resources.
  419. */
  420. void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
  421. {
  422. if (new->parent)
  423. return;
  424. write_lock(&resource_lock);
  425. for (;;) {
  426. struct resource *conflict;
  427. conflict = __insert_resource(root, new);
  428. if (!conflict)
  429. break;
  430. if (conflict == root)
  431. break;
  432. /* Ok, expand resource to cover the conflict, then try again .. */
  433. if (conflict->start < new->start)
  434. new->start = conflict->start;
  435. if (conflict->end > new->end)
  436. new->end = conflict->end;
  437. printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
  438. }
  439. write_unlock(&resource_lock);
  440. }
  441. /**
  442. * adjust_resource - modify a resource's start and size
  443. * @res: resource to modify
  444. * @start: new start value
  445. * @size: new size
  446. *
  447. * Given an existing resource, change its start and size to match the
  448. * arguments. Returns 0 on success, -EBUSY if it can't fit.
  449. * Existing children of the resource are assumed to be immutable.
  450. */
  451. int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
  452. {
  453. struct resource *tmp, *parent = res->parent;
  454. resource_size_t end = start + size - 1;
  455. int result = -EBUSY;
  456. write_lock(&resource_lock);
  457. if ((start < parent->start) || (end > parent->end))
  458. goto out;
  459. for (tmp = res->child; tmp; tmp = tmp->sibling) {
  460. if ((tmp->start < start) || (tmp->end > end))
  461. goto out;
  462. }
  463. if (res->sibling && (res->sibling->start <= end))
  464. goto out;
  465. tmp = parent->child;
  466. if (tmp != res) {
  467. while (tmp->sibling != res)
  468. tmp = tmp->sibling;
  469. if (start <= tmp->end)
  470. goto out;
  471. }
  472. res->start = start;
  473. res->end = end;
  474. result = 0;
  475. out:
  476. write_unlock(&resource_lock);
  477. return result;
  478. }
  479. static void __init __reserve_region_with_split(struct resource *root,
  480. resource_size_t start, resource_size_t end,
  481. const char *name)
  482. {
  483. struct resource *parent = root;
  484. struct resource *conflict;
  485. struct resource *res = kzalloc(sizeof(*res), GFP_ATOMIC);
  486. if (!res)
  487. return;
  488. res->name = name;
  489. res->start = start;
  490. res->end = end;
  491. res->flags = IORESOURCE_BUSY;
  492. conflict = __request_resource(parent, res);
  493. if (!conflict)
  494. return;
  495. /* failed, split and try again */
  496. kfree(res);
  497. /* conflict covered whole area */
  498. if (conflict->start <= start && conflict->end >= end)
  499. return;
  500. if (conflict->start > start)
  501. __reserve_region_with_split(root, start, conflict->start-1, name);
  502. if (conflict->end < end)
  503. __reserve_region_with_split(root, conflict->end+1, end, name);
  504. }
  505. void __init reserve_region_with_split(struct resource *root,
  506. resource_size_t start, resource_size_t end,
  507. const char *name)
  508. {
  509. write_lock(&resource_lock);
  510. __reserve_region_with_split(root, start, end, name);
  511. write_unlock(&resource_lock);
  512. }
  513. EXPORT_SYMBOL(adjust_resource);
  514. /**
  515. * resource_alignment - calculate resource's alignment
  516. * @res: resource pointer
  517. *
  518. * Returns alignment on success, 0 (invalid alignment) on failure.
  519. */
  520. resource_size_t resource_alignment(struct resource *res)
  521. {
  522. switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
  523. case IORESOURCE_SIZEALIGN:
  524. return resource_size(res);
  525. case IORESOURCE_STARTALIGN:
  526. return res->start;
  527. default:
  528. return 0;
  529. }
  530. }
  531. /*
  532. * This is compatibility stuff for IO resources.
  533. *
  534. * Note how this, unlike the above, knows about
  535. * the IO flag meanings (busy etc).
  536. *
  537. * request_region creates a new busy region.
  538. *
  539. * check_region returns non-zero if the area is already busy.
  540. *
  541. * release_region releases a matching busy region.
  542. */
  543. /**
  544. * __request_region - create a new busy resource region
  545. * @parent: parent resource descriptor
  546. * @start: resource start address
  547. * @n: resource region size
  548. * @name: reserving caller's ID string
  549. * @flags: IO resource flags
  550. */
  551. struct resource * __request_region(struct resource *parent,
  552. resource_size_t start, resource_size_t n,
  553. const char *name, int flags)
  554. {
  555. struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
  556. if (!res)
  557. return NULL;
  558. res->name = name;
  559. res->start = start;
  560. res->end = start + n - 1;
  561. res->flags = IORESOURCE_BUSY;
  562. res->flags |= flags;
  563. write_lock(&resource_lock);
  564. for (;;) {
  565. struct resource *conflict;
  566. conflict = __request_resource(parent, res);
  567. if (!conflict)
  568. break;
  569. if (conflict != parent) {
  570. parent = conflict;
  571. if (!(conflict->flags & IORESOURCE_BUSY))
  572. continue;
  573. }
  574. /* Uhhuh, that didn't work out.. */
  575. kfree(res);
  576. res = NULL;
  577. break;
  578. }
  579. write_unlock(&resource_lock);
  580. return res;
  581. }
  582. EXPORT_SYMBOL(__request_region);
  583. /**
  584. * __check_region - check if a resource region is busy or free
  585. * @parent: parent resource descriptor
  586. * @start: resource start address
  587. * @n: resource region size
  588. *
  589. * Returns 0 if the region is free at the moment it is checked,
  590. * returns %-EBUSY if the region is busy.
  591. *
  592. * NOTE:
  593. * This function is deprecated because its use is racy.
  594. * Even if it returns 0, a subsequent call to request_region()
  595. * may fail because another driver etc. just allocated the region.
  596. * Do NOT use it. It will be removed from the kernel.
  597. */
  598. int __check_region(struct resource *parent, resource_size_t start,
  599. resource_size_t n)
  600. {
  601. struct resource * res;
  602. res = __request_region(parent, start, n, "check-region", 0);
  603. if (!res)
  604. return -EBUSY;
  605. release_resource(res);
  606. kfree(res);
  607. return 0;
  608. }
  609. EXPORT_SYMBOL(__check_region);
  610. /**
  611. * __release_region - release a previously reserved resource region
  612. * @parent: parent resource descriptor
  613. * @start: resource start address
  614. * @n: resource region size
  615. *
  616. * The described resource region must match a currently busy region.
  617. */
  618. void __release_region(struct resource *parent, resource_size_t start,
  619. resource_size_t n)
  620. {
  621. struct resource **p;
  622. resource_size_t end;
  623. p = &parent->child;
  624. end = start + n - 1;
  625. write_lock(&resource_lock);
  626. for (;;) {
  627. struct resource *res = *p;
  628. if (!res)
  629. break;
  630. if (res->start <= start && res->end >= end) {
  631. if (!(res->flags & IORESOURCE_BUSY)) {
  632. p = &res->child;
  633. continue;
  634. }
  635. if (res->start != start || res->end != end)
  636. break;
  637. *p = res->sibling;
  638. write_unlock(&resource_lock);
  639. kfree(res);
  640. return;
  641. }
  642. p = &res->sibling;
  643. }
  644. write_unlock(&resource_lock);
  645. printk(KERN_WARNING "Trying to free nonexistent resource "
  646. "<%016llx-%016llx>\n", (unsigned long long)start,
  647. (unsigned long long)end);
  648. }
  649. EXPORT_SYMBOL(__release_region);
  650. /*
  651. * Managed region resource
  652. */
  653. struct region_devres {
  654. struct resource *parent;
  655. resource_size_t start;
  656. resource_size_t n;
  657. };
  658. static void devm_region_release(struct device *dev, void *res)
  659. {
  660. struct region_devres *this = res;
  661. __release_region(this->parent, this->start, this->n);
  662. }
  663. static int devm_region_match(struct device *dev, void *res, void *match_data)
  664. {
  665. struct region_devres *this = res, *match = match_data;
  666. return this->parent == match->parent &&
  667. this->start == match->start && this->n == match->n;
  668. }
  669. struct resource * __devm_request_region(struct device *dev,
  670. struct resource *parent, resource_size_t start,
  671. resource_size_t n, const char *name)
  672. {
  673. struct region_devres *dr = NULL;
  674. struct resource *res;
  675. dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
  676. GFP_KERNEL);
  677. if (!dr)
  678. return NULL;
  679. dr->parent = parent;
  680. dr->start = start;
  681. dr->n = n;
  682. res = __request_region(parent, start, n, name, 0);
  683. if (res)
  684. devres_add(dev, dr);
  685. else
  686. devres_free(dr);
  687. return res;
  688. }
  689. EXPORT_SYMBOL(__devm_request_region);
  690. void __devm_release_region(struct device *dev, struct resource *parent,
  691. resource_size_t start, resource_size_t n)
  692. {
  693. struct region_devres match_data = { parent, start, n };
  694. __release_region(parent, start, n);
  695. WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
  696. &match_data));
  697. }
  698. EXPORT_SYMBOL(__devm_release_region);
  699. /*
  700. * Called from init/main.c to reserve IO ports.
  701. */
  702. #define MAXRESERVE 4
  703. static int __init reserve_setup(char *str)
  704. {
  705. static int reserved;
  706. static struct resource reserve[MAXRESERVE];
  707. for (;;) {
  708. unsigned int io_start, io_num;
  709. int x = reserved;
  710. if (get_option (&str, &io_start) != 2)
  711. break;
  712. if (get_option (&str, &io_num) == 0)
  713. break;
  714. if (x < MAXRESERVE) {
  715. struct resource *res = reserve + x;
  716. res->name = "reserved";
  717. res->start = io_start;
  718. res->end = io_start + io_num - 1;
  719. res->flags = IORESOURCE_BUSY;
  720. res->child = NULL;
  721. if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
  722. reserved = x+1;
  723. }
  724. }
  725. return 1;
  726. }
  727. __setup("reserve=", reserve_setup);
  728. /*
  729. * Check if the requested addr and size spans more than any slot in the
  730. * iomem resource tree.
  731. */
  732. int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
  733. {
  734. struct resource *p = &iomem_resource;
  735. int err = 0;
  736. loff_t l;
  737. read_lock(&resource_lock);
  738. for (p = p->child; p ; p = r_next(NULL, p, &l)) {
  739. /*
  740. * We can probably skip the resources without
  741. * IORESOURCE_IO attribute?
  742. */
  743. if (p->start >= addr + size)
  744. continue;
  745. if (p->end < addr)
  746. continue;
  747. if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
  748. PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
  749. continue;
  750. /*
  751. * if a resource is "BUSY", it's not a hardware resource
  752. * but a driver mapping of such a resource; we don't want
  753. * to warn for those; some drivers legitimately map only
  754. * partial hardware resources. (example: vesafb)
  755. */
  756. if (p->flags & IORESOURCE_BUSY)
  757. continue;
  758. printk(KERN_WARNING "resource map sanity check conflict: "
  759. "0x%llx 0x%llx 0x%llx 0x%llx %s\n",
  760. (unsigned long long)addr,
  761. (unsigned long long)(addr + size - 1),
  762. (unsigned long long)p->start,
  763. (unsigned long long)p->end,
  764. p->name);
  765. err = -1;
  766. break;
  767. }
  768. read_unlock(&resource_lock);
  769. return err;
  770. }
  771. #ifdef CONFIG_STRICT_DEVMEM
  772. static int strict_iomem_checks = 1;
  773. #else
  774. static int strict_iomem_checks;
  775. #endif
  776. /*
  777. * check if an address is reserved in the iomem resource tree
  778. * returns 1 if reserved, 0 if not reserved.
  779. */
  780. int iomem_is_exclusive(u64 addr)
  781. {
  782. struct resource *p = &iomem_resource;
  783. int err = 0;
  784. loff_t l;
  785. int size = PAGE_SIZE;
  786. if (!strict_iomem_checks)
  787. return 0;
  788. addr = addr & PAGE_MASK;
  789. read_lock(&resource_lock);
  790. for (p = p->child; p ; p = r_next(NULL, p, &l)) {
  791. /*
  792. * We can probably skip the resources without
  793. * IORESOURCE_IO attribute?
  794. */
  795. if (p->start >= addr + size)
  796. break;
  797. if (p->end < addr)
  798. continue;
  799. if (p->flags & IORESOURCE_BUSY &&
  800. p->flags & IORESOURCE_EXCLUSIVE) {
  801. err = 1;
  802. break;
  803. }
  804. }
  805. read_unlock(&resource_lock);
  806. return err;
  807. }
  808. static int __init strict_iomem(char *str)
  809. {
  810. if (strstr(str, "relaxed"))
  811. strict_iomem_checks = 0;
  812. if (strstr(str, "strict"))
  813. strict_iomem_checks = 1;
  814. return 1;
  815. }
  816. __setup("iomem=", strict_iomem);