resource.c 22 KB

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