resource.c 27 KB

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