resource.c 26 KB

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