resource.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  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. unsigned long long start, end;
  77. int width = root->end < 0x10000 ? 4 : 8;
  78. int depth;
  79. for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
  80. if (p->parent == root)
  81. break;
  82. if (capable(CAP_SYS_ADMIN)) {
  83. start = r->start;
  84. end = r->end;
  85. } else {
  86. start = end = 0;
  87. }
  88. seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
  89. depth * 2, "",
  90. width, start,
  91. width, 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", S_IRUSR, 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. * locate_resource - locate an already reserved I/O or memory resource
  239. * @root: root resource descriptor
  240. * @search: resource descriptor to be located
  241. *
  242. * Returns pointer to desired resource or NULL if not found.
  243. */
  244. struct resource *locate_resource(struct resource *root, struct resource *search)
  245. {
  246. struct resource *found;
  247. write_lock(&resource_lock);
  248. found = __request_resource(root, search);
  249. write_unlock(&resource_lock);
  250. return found;
  251. }
  252. EXPORT_SYMBOL(locate_resource);
  253. /**
  254. * release_resource - release a previously reserved resource
  255. * @old: resource pointer
  256. */
  257. int release_resource(struct resource *old)
  258. {
  259. int retval;
  260. write_lock(&resource_lock);
  261. retval = __release_resource(old);
  262. write_unlock(&resource_lock);
  263. return retval;
  264. }
  265. EXPORT_SYMBOL(release_resource);
  266. #if !defined(CONFIG_ARCH_HAS_WALK_MEMORY)
  267. /*
  268. * Finds the lowest memory reosurce exists within [res->start.res->end)
  269. * the caller must specify res->start, res->end, res->flags and "name".
  270. * If found, returns 0, res is overwritten, if not found, returns -1.
  271. */
  272. static int find_next_system_ram(struct resource *res, char *name)
  273. {
  274. resource_size_t start, end;
  275. struct resource *p;
  276. BUG_ON(!res);
  277. start = res->start;
  278. end = res->end;
  279. BUG_ON(start >= end);
  280. read_lock(&resource_lock);
  281. for (p = iomem_resource.child; p ; p = p->sibling) {
  282. /* system ram is just marked as IORESOURCE_MEM */
  283. if (p->flags != res->flags)
  284. continue;
  285. if (name && strcmp(p->name, name))
  286. continue;
  287. if (p->start > end) {
  288. p = NULL;
  289. break;
  290. }
  291. if ((p->end >= start) && (p->start < end))
  292. break;
  293. }
  294. read_unlock(&resource_lock);
  295. if (!p)
  296. return -1;
  297. /* copy data */
  298. if (res->start < p->start)
  299. res->start = p->start;
  300. if (res->end > p->end)
  301. res->end = p->end;
  302. return 0;
  303. }
  304. /*
  305. * This function calls callback against all memory range of "System RAM"
  306. * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY.
  307. * Now, this function is only for "System RAM".
  308. */
  309. int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
  310. void *arg, int (*func)(unsigned long, unsigned long, void *))
  311. {
  312. struct resource res;
  313. unsigned long pfn, end_pfn;
  314. u64 orig_end;
  315. int ret = -1;
  316. res.start = (u64) start_pfn << PAGE_SHIFT;
  317. res.end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
  318. res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  319. orig_end = res.end;
  320. while ((res.start < res.end) &&
  321. (find_next_system_ram(&res, "System RAM") >= 0)) {
  322. pfn = (res.start + PAGE_SIZE - 1) >> PAGE_SHIFT;
  323. if (res.end + 1 <= 0)
  324. end_pfn = res.end >> PAGE_SHIFT;
  325. else
  326. end_pfn = (res.end + 1) >> PAGE_SHIFT;
  327. if (end_pfn > pfn)
  328. ret = (*func)(pfn, end_pfn - pfn, arg);
  329. if (ret)
  330. break;
  331. if (res.end + 1 > res.start)
  332. res.start = res.end + 1;
  333. else
  334. res.start = res.end;
  335. res.end = orig_end;
  336. }
  337. return ret;
  338. }
  339. #endif
  340. static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
  341. {
  342. return 1;
  343. }
  344. /*
  345. * This generic page_is_ram() returns true if specified address is
  346. * registered as "System RAM" in iomem_resource list.
  347. */
  348. int __weak page_is_ram(unsigned long pfn)
  349. {
  350. return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
  351. }
  352. void __weak arch_remove_reservations(struct resource *avail)
  353. {
  354. }
  355. static resource_size_t simple_align_resource(void *data,
  356. const struct resource *avail,
  357. resource_size_t size,
  358. resource_size_t align)
  359. {
  360. return avail->start;
  361. }
  362. static void resource_clip(struct resource *res, resource_size_t min,
  363. resource_size_t max)
  364. {
  365. if (res->start < min)
  366. res->start = min;
  367. if (res->end > max)
  368. res->end = max;
  369. }
  370. static bool resource_contains(struct resource *res1, struct resource *res2)
  371. {
  372. return res1->start <= res2->start && res1->end >= res2->end;
  373. }
  374. /*
  375. * Find empty slot in the resource tree with the given range and
  376. * alignment constraints
  377. */
  378. static int __find_resource(struct resource *root, struct resource *old,
  379. struct resource *new,
  380. resource_size_t size,
  381. struct resource_constraint *constraint)
  382. {
  383. struct resource *this = root->child;
  384. struct resource tmp = *new, avail, alloc;
  385. tmp.flags = new->flags;
  386. tmp.start = root->start;
  387. /*
  388. * Skip past an allocated resource that starts at 0, since the assignment
  389. * of this->start - 1 to tmp->end below would cause an underflow.
  390. */
  391. if (this && this->start == root->start) {
  392. tmp.start = (this == old) ? old->start : this->end + 1;
  393. this = this->sibling;
  394. }
  395. for(;;) {
  396. if (this)
  397. tmp.end = (this == old) ? this->end : this->start - 1;
  398. else
  399. tmp.end = root->end;
  400. if (tmp.end < tmp.start)
  401. goto next;
  402. resource_clip(&tmp, constraint->min, constraint->max);
  403. arch_remove_reservations(&tmp);
  404. /* Check for overflow after ALIGN() */
  405. avail = *new;
  406. avail.start = ALIGN(tmp.start, constraint->align);
  407. avail.end = tmp.end;
  408. if (avail.start >= tmp.start) {
  409. alloc.start = constraint->alignf(constraint->alignf_data, &avail,
  410. size, constraint->align);
  411. alloc.end = alloc.start + size - 1;
  412. if (resource_contains(&avail, &alloc)) {
  413. new->start = alloc.start;
  414. new->end = alloc.end;
  415. return 0;
  416. }
  417. }
  418. next: if (!this || this->end == root->end)
  419. break;
  420. if (this != old)
  421. tmp.start = this->end + 1;
  422. this = this->sibling;
  423. }
  424. return -EBUSY;
  425. }
  426. /*
  427. * Find empty slot in the resource tree given range and alignment.
  428. */
  429. static int find_resource(struct resource *root, struct resource *new,
  430. resource_size_t size,
  431. struct resource_constraint *constraint)
  432. {
  433. return __find_resource(root, NULL, new, size, constraint);
  434. }
  435. /**
  436. * reallocate_resource - allocate a slot in the resource tree given range & alignment.
  437. * The resource will be relocated if the new size cannot be reallocated in the
  438. * current location.
  439. *
  440. * @root: root resource descriptor
  441. * @old: resource descriptor desired by caller
  442. * @newsize: new size of the resource descriptor
  443. * @constraint: the size and alignment constraints to be met.
  444. */
  445. int reallocate_resource(struct resource *root, struct resource *old,
  446. resource_size_t newsize,
  447. struct resource_constraint *constraint)
  448. {
  449. int err=0;
  450. struct resource new = *old;
  451. struct resource *conflict;
  452. write_lock(&resource_lock);
  453. if ((err = __find_resource(root, old, &new, newsize, constraint)))
  454. goto out;
  455. if (resource_contains(&new, old)) {
  456. old->start = new.start;
  457. old->end = new.end;
  458. goto out;
  459. }
  460. if (old->child) {
  461. err = -EBUSY;
  462. goto out;
  463. }
  464. if (resource_contains(old, &new)) {
  465. old->start = new.start;
  466. old->end = new.end;
  467. } else {
  468. __release_resource(old);
  469. *old = new;
  470. conflict = __request_resource(root, old);
  471. BUG_ON(conflict);
  472. }
  473. out:
  474. write_unlock(&resource_lock);
  475. return err;
  476. }
  477. /**
  478. * allocate_resource - allocate empty slot in the resource tree given range & alignment.
  479. * The resource will be reallocated with a new size if it was already allocated
  480. * @root: root resource descriptor
  481. * @new: resource descriptor desired by caller
  482. * @size: requested resource region size
  483. * @min: minimum boundary to allocate
  484. * @max: maximum boundary to allocate
  485. * @align: alignment requested, in bytes
  486. * @alignf: alignment function, optional, called if not NULL
  487. * @alignf_data: arbitrary data to pass to the @alignf function
  488. */
  489. int allocate_resource(struct resource *root, struct resource *new,
  490. resource_size_t size, resource_size_t min,
  491. resource_size_t max, resource_size_t align,
  492. resource_size_t (*alignf)(void *,
  493. const struct resource *,
  494. resource_size_t,
  495. resource_size_t),
  496. void *alignf_data)
  497. {
  498. int err;
  499. struct resource_constraint constraint;
  500. if (!alignf)
  501. alignf = simple_align_resource;
  502. constraint.min = min;
  503. constraint.max = max;
  504. constraint.align = align;
  505. constraint.alignf = alignf;
  506. constraint.alignf_data = alignf_data;
  507. if ( new->parent ) {
  508. /* resource is already allocated, try reallocating with
  509. the new constraints */
  510. return reallocate_resource(root, new, size, &constraint);
  511. }
  512. write_lock(&resource_lock);
  513. err = find_resource(root, new, size, &constraint);
  514. if (err >= 0 && __request_resource(root, new))
  515. err = -EBUSY;
  516. write_unlock(&resource_lock);
  517. return err;
  518. }
  519. EXPORT_SYMBOL(allocate_resource);
  520. /**
  521. * lookup_resource - find an existing resource by a resource start address
  522. * @root: root resource descriptor
  523. * @start: resource start address
  524. *
  525. * Returns a pointer to the resource if found, NULL otherwise
  526. */
  527. struct resource *lookup_resource(struct resource *root, resource_size_t start)
  528. {
  529. struct resource *res;
  530. read_lock(&resource_lock);
  531. for (res = root->child; res; res = res->sibling) {
  532. if (res->start == start)
  533. break;
  534. }
  535. read_unlock(&resource_lock);
  536. return res;
  537. }
  538. /*
  539. * Insert a resource into the resource tree. If successful, return NULL,
  540. * otherwise return the conflicting resource (compare to __request_resource())
  541. */
  542. static struct resource * __insert_resource(struct resource *parent, struct resource *new)
  543. {
  544. struct resource *first, *next;
  545. for (;; parent = first) {
  546. first = __request_resource(parent, new);
  547. if (!first)
  548. return first;
  549. if (first == parent)
  550. return first;
  551. if (WARN_ON(first == new)) /* duplicated insertion */
  552. return first;
  553. if ((first->start > new->start) || (first->end < new->end))
  554. break;
  555. if ((first->start == new->start) && (first->end == new->end))
  556. break;
  557. }
  558. for (next = first; ; next = next->sibling) {
  559. /* Partial overlap? Bad, and unfixable */
  560. if (next->start < new->start || next->end > new->end)
  561. return next;
  562. if (!next->sibling)
  563. break;
  564. if (next->sibling->start > new->end)
  565. break;
  566. }
  567. new->parent = parent;
  568. new->sibling = next->sibling;
  569. new->child = first;
  570. next->sibling = NULL;
  571. for (next = first; next; next = next->sibling)
  572. next->parent = new;
  573. if (parent->child == first) {
  574. parent->child = new;
  575. } else {
  576. next = parent->child;
  577. while (next->sibling != first)
  578. next = next->sibling;
  579. next->sibling = new;
  580. }
  581. return NULL;
  582. }
  583. /**
  584. * insert_resource_conflict - Inserts resource in the resource tree
  585. * @parent: parent of the new resource
  586. * @new: new resource to insert
  587. *
  588. * Returns 0 on success, conflict resource if the resource can't be inserted.
  589. *
  590. * This function is equivalent to request_resource_conflict when no conflict
  591. * happens. If a conflict happens, and the conflicting resources
  592. * entirely fit within the range of the new resource, then the new
  593. * resource is inserted and the conflicting resources become children of
  594. * the new resource.
  595. */
  596. struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
  597. {
  598. struct resource *conflict;
  599. write_lock(&resource_lock);
  600. conflict = __insert_resource(parent, new);
  601. write_unlock(&resource_lock);
  602. return conflict;
  603. }
  604. /**
  605. * insert_resource - Inserts a resource in the resource tree
  606. * @parent: parent of the new resource
  607. * @new: new resource to insert
  608. *
  609. * Returns 0 on success, -EBUSY if the resource can't be inserted.
  610. */
  611. int insert_resource(struct resource *parent, struct resource *new)
  612. {
  613. struct resource *conflict;
  614. conflict = insert_resource_conflict(parent, new);
  615. return conflict ? -EBUSY : 0;
  616. }
  617. /**
  618. * insert_resource_expand_to_fit - Insert a resource into the resource tree
  619. * @root: root resource descriptor
  620. * @new: new resource to insert
  621. *
  622. * Insert a resource into the resource tree, possibly expanding it in order
  623. * to make it encompass any conflicting resources.
  624. */
  625. void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
  626. {
  627. if (new->parent)
  628. return;
  629. write_lock(&resource_lock);
  630. for (;;) {
  631. struct resource *conflict;
  632. conflict = __insert_resource(root, new);
  633. if (!conflict)
  634. break;
  635. if (conflict == root)
  636. break;
  637. /* Ok, expand resource to cover the conflict, then try again .. */
  638. if (conflict->start < new->start)
  639. new->start = conflict->start;
  640. if (conflict->end > new->end)
  641. new->end = conflict->end;
  642. printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
  643. }
  644. write_unlock(&resource_lock);
  645. }
  646. /**
  647. * adjust_resource - modify a resource's start and size
  648. * @res: resource to modify
  649. * @start: new start value
  650. * @size: new size
  651. *
  652. * Given an existing resource, change its start and size to match the
  653. * arguments. Returns 0 on success, -EBUSY if it can't fit.
  654. * Existing children of the resource are assumed to be immutable.
  655. */
  656. int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
  657. {
  658. struct resource *tmp, *parent = res->parent;
  659. resource_size_t end = start + size - 1;
  660. int result = -EBUSY;
  661. write_lock(&resource_lock);
  662. if ((start < parent->start) || (end > parent->end))
  663. goto out;
  664. for (tmp = res->child; tmp; tmp = tmp->sibling) {
  665. if ((tmp->start < start) || (tmp->end > end))
  666. goto out;
  667. }
  668. if (res->sibling && (res->sibling->start <= end))
  669. goto out;
  670. tmp = parent->child;
  671. if (tmp != res) {
  672. while (tmp->sibling != res)
  673. tmp = tmp->sibling;
  674. if (start <= tmp->end)
  675. goto out;
  676. }
  677. res->start = start;
  678. res->end = end;
  679. result = 0;
  680. out:
  681. write_unlock(&resource_lock);
  682. return result;
  683. }
  684. EXPORT_SYMBOL(adjust_resource);
  685. static void __init __reserve_region_with_split(struct resource *root,
  686. resource_size_t start, resource_size_t end,
  687. const char *name)
  688. {
  689. struct resource *parent = root;
  690. struct resource *conflict;
  691. struct resource *res = kzalloc(sizeof(*res), GFP_ATOMIC);
  692. struct resource *next_res = NULL;
  693. if (!res)
  694. return;
  695. res->name = name;
  696. res->start = start;
  697. res->end = end;
  698. res->flags = IORESOURCE_BUSY;
  699. while (1) {
  700. conflict = __request_resource(parent, res);
  701. if (!conflict) {
  702. if (!next_res)
  703. break;
  704. res = next_res;
  705. next_res = NULL;
  706. continue;
  707. }
  708. /* conflict covered whole area */
  709. if (conflict->start <= res->start &&
  710. conflict->end >= res->end) {
  711. kfree(res);
  712. WARN_ON(next_res);
  713. break;
  714. }
  715. /* failed, split and try again */
  716. if (conflict->start > res->start) {
  717. end = res->end;
  718. res->end = conflict->start - 1;
  719. if (conflict->end < end) {
  720. next_res = kzalloc(sizeof(*next_res),
  721. GFP_ATOMIC);
  722. if (!next_res) {
  723. kfree(res);
  724. break;
  725. }
  726. next_res->name = name;
  727. next_res->start = conflict->end + 1;
  728. next_res->end = end;
  729. next_res->flags = IORESOURCE_BUSY;
  730. }
  731. } else {
  732. res->start = conflict->end + 1;
  733. }
  734. }
  735. }
  736. void __init reserve_region_with_split(struct resource *root,
  737. resource_size_t start, resource_size_t end,
  738. const char *name)
  739. {
  740. int abort = 0;
  741. write_lock(&resource_lock);
  742. if (root->start > start || root->end < end) {
  743. pr_err("requested range [0x%llx-0x%llx] not in root %pr\n",
  744. (unsigned long long)start, (unsigned long long)end,
  745. root);
  746. if (start > root->end || end < root->start)
  747. abort = 1;
  748. else {
  749. if (end > root->end)
  750. end = root->end;
  751. if (start < root->start)
  752. start = root->start;
  753. pr_err("fixing request to [0x%llx-0x%llx]\n",
  754. (unsigned long long)start,
  755. (unsigned long long)end);
  756. }
  757. dump_stack();
  758. }
  759. if (!abort)
  760. __reserve_region_with_split(root, start, end, name);
  761. write_unlock(&resource_lock);
  762. }
  763. /**
  764. * resource_alignment - calculate resource's alignment
  765. * @res: resource pointer
  766. *
  767. * Returns alignment on success, 0 (invalid alignment) on failure.
  768. */
  769. resource_size_t resource_alignment(struct resource *res)
  770. {
  771. switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
  772. case IORESOURCE_SIZEALIGN:
  773. return resource_size(res);
  774. case IORESOURCE_STARTALIGN:
  775. return res->start;
  776. default:
  777. return 0;
  778. }
  779. }
  780. /*
  781. * This is compatibility stuff for IO resources.
  782. *
  783. * Note how this, unlike the above, knows about
  784. * the IO flag meanings (busy etc).
  785. *
  786. * request_region creates a new busy region.
  787. *
  788. * check_region returns non-zero if the area is already busy.
  789. *
  790. * release_region releases a matching busy region.
  791. */
  792. static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
  793. /**
  794. * __request_region - create a new busy resource region
  795. * @parent: parent resource descriptor
  796. * @start: resource start address
  797. * @n: resource region size
  798. * @name: reserving caller's ID string
  799. * @flags: IO resource flags
  800. */
  801. struct resource * __request_region(struct resource *parent,
  802. resource_size_t start, resource_size_t n,
  803. const char *name, int flags)
  804. {
  805. DECLARE_WAITQUEUE(wait, current);
  806. struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
  807. if (!res)
  808. return NULL;
  809. res->name = name;
  810. res->start = start;
  811. res->end = start + n - 1;
  812. res->flags = IORESOURCE_BUSY;
  813. res->flags |= flags;
  814. write_lock(&resource_lock);
  815. for (;;) {
  816. struct resource *conflict;
  817. conflict = __request_resource(parent, res);
  818. if (!conflict)
  819. break;
  820. if (conflict != parent) {
  821. parent = conflict;
  822. if (!(conflict->flags & IORESOURCE_BUSY))
  823. continue;
  824. }
  825. if (conflict->flags & flags & IORESOURCE_MUXED) {
  826. add_wait_queue(&muxed_resource_wait, &wait);
  827. write_unlock(&resource_lock);
  828. set_current_state(TASK_UNINTERRUPTIBLE);
  829. schedule();
  830. remove_wait_queue(&muxed_resource_wait, &wait);
  831. write_lock(&resource_lock);
  832. continue;
  833. }
  834. /* Uhhuh, that didn't work out.. */
  835. kfree(res);
  836. res = NULL;
  837. break;
  838. }
  839. write_unlock(&resource_lock);
  840. return res;
  841. }
  842. EXPORT_SYMBOL(__request_region);
  843. /**
  844. * __check_region - check if a resource region is busy or free
  845. * @parent: parent resource descriptor
  846. * @start: resource start address
  847. * @n: resource region size
  848. *
  849. * Returns 0 if the region is free at the moment it is checked,
  850. * returns %-EBUSY if the region is busy.
  851. *
  852. * NOTE:
  853. * This function is deprecated because its use is racy.
  854. * Even if it returns 0, a subsequent call to request_region()
  855. * may fail because another driver etc. just allocated the region.
  856. * Do NOT use it. It will be removed from the kernel.
  857. */
  858. int __check_region(struct resource *parent, resource_size_t start,
  859. resource_size_t n)
  860. {
  861. struct resource * res;
  862. res = __request_region(parent, start, n, "check-region", 0);
  863. if (!res)
  864. return -EBUSY;
  865. release_resource(res);
  866. kfree(res);
  867. return 0;
  868. }
  869. EXPORT_SYMBOL(__check_region);
  870. /**
  871. * __release_region - release a previously reserved resource region
  872. * @parent: parent resource descriptor
  873. * @start: resource start address
  874. * @n: resource region size
  875. *
  876. * The described resource region must match a currently busy region.
  877. */
  878. void __release_region(struct resource *parent, resource_size_t start,
  879. resource_size_t n)
  880. {
  881. struct resource **p;
  882. resource_size_t end;
  883. p = &parent->child;
  884. end = start + n - 1;
  885. write_lock(&resource_lock);
  886. for (;;) {
  887. struct resource *res = *p;
  888. if (!res)
  889. break;
  890. if (res->start <= start && res->end >= end) {
  891. if (!(res->flags & IORESOURCE_BUSY)) {
  892. p = &res->child;
  893. continue;
  894. }
  895. if (res->start != start || res->end != end)
  896. break;
  897. *p = res->sibling;
  898. write_unlock(&resource_lock);
  899. if (res->flags & IORESOURCE_MUXED)
  900. wake_up(&muxed_resource_wait);
  901. kfree(res);
  902. return;
  903. }
  904. p = &res->sibling;
  905. }
  906. write_unlock(&resource_lock);
  907. printk(KERN_WARNING "Trying to free nonexistent resource "
  908. "<%016llx-%016llx>\n", (unsigned long long)start,
  909. (unsigned long long)end);
  910. }
  911. EXPORT_SYMBOL(__release_region);
  912. /*
  913. * Managed region resource
  914. */
  915. struct region_devres {
  916. struct resource *parent;
  917. resource_size_t start;
  918. resource_size_t n;
  919. };
  920. static void devm_region_release(struct device *dev, void *res)
  921. {
  922. struct region_devres *this = res;
  923. __release_region(this->parent, this->start, this->n);
  924. }
  925. static int devm_region_match(struct device *dev, void *res, void *match_data)
  926. {
  927. struct region_devres *this = res, *match = match_data;
  928. return this->parent == match->parent &&
  929. this->start == match->start && this->n == match->n;
  930. }
  931. struct resource * __devm_request_region(struct device *dev,
  932. struct resource *parent, resource_size_t start,
  933. resource_size_t n, const char *name)
  934. {
  935. struct region_devres *dr = NULL;
  936. struct resource *res;
  937. dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
  938. GFP_KERNEL);
  939. if (!dr)
  940. return NULL;
  941. dr->parent = parent;
  942. dr->start = start;
  943. dr->n = n;
  944. res = __request_region(parent, start, n, name, 0);
  945. if (res)
  946. devres_add(dev, dr);
  947. else
  948. devres_free(dr);
  949. return res;
  950. }
  951. EXPORT_SYMBOL(__devm_request_region);
  952. void __devm_release_region(struct device *dev, struct resource *parent,
  953. resource_size_t start, resource_size_t n)
  954. {
  955. struct region_devres match_data = { parent, start, n };
  956. __release_region(parent, start, n);
  957. WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
  958. &match_data));
  959. }
  960. EXPORT_SYMBOL(__devm_release_region);
  961. /*
  962. * Called from init/main.c to reserve IO ports.
  963. */
  964. #define MAXRESERVE 4
  965. static int __init reserve_setup(char *str)
  966. {
  967. static int reserved;
  968. static struct resource reserve[MAXRESERVE];
  969. for (;;) {
  970. unsigned int io_start, io_num;
  971. int x = reserved;
  972. if (get_option (&str, &io_start) != 2)
  973. break;
  974. if (get_option (&str, &io_num) == 0)
  975. break;
  976. if (x < MAXRESERVE) {
  977. struct resource *res = reserve + x;
  978. res->name = "reserved";
  979. res->start = io_start;
  980. res->end = io_start + io_num - 1;
  981. res->flags = IORESOURCE_BUSY;
  982. res->child = NULL;
  983. if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
  984. reserved = x+1;
  985. }
  986. }
  987. return 1;
  988. }
  989. __setup("reserve=", reserve_setup);
  990. /*
  991. * Check if the requested addr and size spans more than any slot in the
  992. * iomem resource tree.
  993. */
  994. int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
  995. {
  996. struct resource *p = &iomem_resource;
  997. int err = 0;
  998. loff_t l = 0;
  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. continue;
  1007. if (p->end < addr)
  1008. continue;
  1009. if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
  1010. PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
  1011. continue;
  1012. /*
  1013. * if a resource is "BUSY", it's not a hardware resource
  1014. * but a driver mapping of such a resource; we don't want
  1015. * to warn for those; some drivers legitimately map only
  1016. * partial hardware resources. (example: vesafb)
  1017. */
  1018. if (p->flags & IORESOURCE_BUSY)
  1019. continue;
  1020. printk(KERN_WARNING "resource map sanity check conflict: "
  1021. "0x%llx 0x%llx 0x%llx 0x%llx %s\n",
  1022. (unsigned long long)addr,
  1023. (unsigned long long)(addr + size - 1),
  1024. (unsigned long long)p->start,
  1025. (unsigned long long)p->end,
  1026. p->name);
  1027. err = -1;
  1028. break;
  1029. }
  1030. read_unlock(&resource_lock);
  1031. return err;
  1032. }
  1033. #ifdef CONFIG_STRICT_DEVMEM
  1034. static int strict_iomem_checks = 1;
  1035. #else
  1036. static int strict_iomem_checks;
  1037. #endif
  1038. /*
  1039. * check if an address is reserved in the iomem resource tree
  1040. * returns 1 if reserved, 0 if not reserved.
  1041. */
  1042. int iomem_is_exclusive(u64 addr)
  1043. {
  1044. struct resource *p = &iomem_resource;
  1045. int err = 0;
  1046. loff_t l = 0;
  1047. int size = PAGE_SIZE;
  1048. if (!strict_iomem_checks)
  1049. return 0;
  1050. addr = addr & PAGE_MASK;
  1051. read_lock(&resource_lock);
  1052. for (p = p->child; p ; p = r_next(NULL, p, &l)) {
  1053. /*
  1054. * We can probably skip the resources without
  1055. * IORESOURCE_IO attribute?
  1056. */
  1057. if (p->start >= addr + size)
  1058. break;
  1059. if (p->end < addr)
  1060. continue;
  1061. if (p->flags & IORESOURCE_BUSY &&
  1062. p->flags & IORESOURCE_EXCLUSIVE) {
  1063. err = 1;
  1064. break;
  1065. }
  1066. }
  1067. read_unlock(&resource_lock);
  1068. return err;
  1069. }
  1070. static int __init strict_iomem(char *str)
  1071. {
  1072. if (strstr(str, "relaxed"))
  1073. strict_iomem_checks = 0;
  1074. if (strstr(str, "strict"))
  1075. strict_iomem_checks = 1;
  1076. return 1;
  1077. }
  1078. __setup("iomem=", strict_iomem);