resource.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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 (WARN_ON(first == new)) /* duplicated insertion */
  504. return first;
  505. if ((first->start > new->start) || (first->end < new->end))
  506. break;
  507. if ((first->start == new->start) && (first->end == new->end))
  508. break;
  509. }
  510. for (next = first; ; next = next->sibling) {
  511. /* Partial overlap? Bad, and unfixable */
  512. if (next->start < new->start || next->end > new->end)
  513. return next;
  514. if (!next->sibling)
  515. break;
  516. if (next->sibling->start > new->end)
  517. break;
  518. }
  519. new->parent = parent;
  520. new->sibling = next->sibling;
  521. new->child = first;
  522. next->sibling = NULL;
  523. for (next = first; next; next = next->sibling)
  524. next->parent = new;
  525. if (parent->child == first) {
  526. parent->child = new;
  527. } else {
  528. next = parent->child;
  529. while (next->sibling != first)
  530. next = next->sibling;
  531. next->sibling = new;
  532. }
  533. return NULL;
  534. }
  535. /**
  536. * insert_resource_conflict - Inserts resource in the resource tree
  537. * @parent: parent of the new resource
  538. * @new: new resource to insert
  539. *
  540. * Returns 0 on success, conflict resource if the resource can't be inserted.
  541. *
  542. * This function is equivalent to request_resource_conflict when no conflict
  543. * happens. If a conflict happens, and the conflicting resources
  544. * entirely fit within the range of the new resource, then the new
  545. * resource is inserted and the conflicting resources become children of
  546. * the new resource.
  547. */
  548. struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
  549. {
  550. struct resource *conflict;
  551. write_lock(&resource_lock);
  552. conflict = __insert_resource(parent, new);
  553. write_unlock(&resource_lock);
  554. return conflict;
  555. }
  556. /**
  557. * insert_resource - Inserts a resource in the resource tree
  558. * @parent: parent of the new resource
  559. * @new: new resource to insert
  560. *
  561. * Returns 0 on success, -EBUSY if the resource can't be inserted.
  562. */
  563. int insert_resource(struct resource *parent, struct resource *new)
  564. {
  565. struct resource *conflict;
  566. conflict = insert_resource_conflict(parent, new);
  567. return conflict ? -EBUSY : 0;
  568. }
  569. /**
  570. * insert_resource_expand_to_fit - Insert a resource into the resource tree
  571. * @root: root resource descriptor
  572. * @new: new resource to insert
  573. *
  574. * Insert a resource into the resource tree, possibly expanding it in order
  575. * to make it encompass any conflicting resources.
  576. */
  577. void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
  578. {
  579. if (new->parent)
  580. return;
  581. write_lock(&resource_lock);
  582. for (;;) {
  583. struct resource *conflict;
  584. conflict = __insert_resource(root, new);
  585. if (!conflict)
  586. break;
  587. if (conflict == root)
  588. break;
  589. /* Ok, expand resource to cover the conflict, then try again .. */
  590. if (conflict->start < new->start)
  591. new->start = conflict->start;
  592. if (conflict->end > new->end)
  593. new->end = conflict->end;
  594. printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
  595. }
  596. write_unlock(&resource_lock);
  597. }
  598. /**
  599. * adjust_resource - modify a resource's start and size
  600. * @res: resource to modify
  601. * @start: new start value
  602. * @size: new size
  603. *
  604. * Given an existing resource, change its start and size to match the
  605. * arguments. Returns 0 on success, -EBUSY if it can't fit.
  606. * Existing children of the resource are assumed to be immutable.
  607. */
  608. int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
  609. {
  610. struct resource *tmp, *parent = res->parent;
  611. resource_size_t end = start + size - 1;
  612. int result = -EBUSY;
  613. write_lock(&resource_lock);
  614. if ((start < parent->start) || (end > parent->end))
  615. goto out;
  616. for (tmp = res->child; tmp; tmp = tmp->sibling) {
  617. if ((tmp->start < start) || (tmp->end > end))
  618. goto out;
  619. }
  620. if (res->sibling && (res->sibling->start <= end))
  621. goto out;
  622. tmp = parent->child;
  623. if (tmp != res) {
  624. while (tmp->sibling != res)
  625. tmp = tmp->sibling;
  626. if (start <= tmp->end)
  627. goto out;
  628. }
  629. res->start = start;
  630. res->end = end;
  631. result = 0;
  632. out:
  633. write_unlock(&resource_lock);
  634. return result;
  635. }
  636. static void __init __reserve_region_with_split(struct resource *root,
  637. resource_size_t start, resource_size_t end,
  638. const char *name)
  639. {
  640. struct resource *parent = root;
  641. struct resource *conflict;
  642. struct resource *res = kzalloc(sizeof(*res), GFP_ATOMIC);
  643. if (!res)
  644. return;
  645. res->name = name;
  646. res->start = start;
  647. res->end = end;
  648. res->flags = IORESOURCE_BUSY;
  649. conflict = __request_resource(parent, res);
  650. if (!conflict)
  651. return;
  652. /* failed, split and try again */
  653. kfree(res);
  654. /* conflict covered whole area */
  655. if (conflict->start <= start && conflict->end >= end)
  656. return;
  657. if (conflict->start > start)
  658. __reserve_region_with_split(root, start, conflict->start-1, name);
  659. if (conflict->end < end)
  660. __reserve_region_with_split(root, conflict->end+1, end, name);
  661. }
  662. void __init reserve_region_with_split(struct resource *root,
  663. resource_size_t start, resource_size_t end,
  664. const char *name)
  665. {
  666. write_lock(&resource_lock);
  667. __reserve_region_with_split(root, start, end, name);
  668. write_unlock(&resource_lock);
  669. }
  670. EXPORT_SYMBOL(adjust_resource);
  671. /**
  672. * resource_alignment - calculate resource's alignment
  673. * @res: resource pointer
  674. *
  675. * Returns alignment on success, 0 (invalid alignment) on failure.
  676. */
  677. resource_size_t resource_alignment(struct resource *res)
  678. {
  679. switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
  680. case IORESOURCE_SIZEALIGN:
  681. return resource_size(res);
  682. case IORESOURCE_STARTALIGN:
  683. return res->start;
  684. default:
  685. return 0;
  686. }
  687. }
  688. /*
  689. * This is compatibility stuff for IO resources.
  690. *
  691. * Note how this, unlike the above, knows about
  692. * the IO flag meanings (busy etc).
  693. *
  694. * request_region creates a new busy region.
  695. *
  696. * check_region returns non-zero if the area is already busy.
  697. *
  698. * release_region releases a matching busy region.
  699. */
  700. static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
  701. /**
  702. * __request_region - create a new busy resource region
  703. * @parent: parent resource descriptor
  704. * @start: resource start address
  705. * @n: resource region size
  706. * @name: reserving caller's ID string
  707. * @flags: IO resource flags
  708. */
  709. struct resource * __request_region(struct resource *parent,
  710. resource_size_t start, resource_size_t n,
  711. const char *name, int flags)
  712. {
  713. DECLARE_WAITQUEUE(wait, current);
  714. struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
  715. if (!res)
  716. return NULL;
  717. res->name = name;
  718. res->start = start;
  719. res->end = start + n - 1;
  720. res->flags = IORESOURCE_BUSY;
  721. res->flags |= flags;
  722. write_lock(&resource_lock);
  723. for (;;) {
  724. struct resource *conflict;
  725. conflict = __request_resource(parent, res);
  726. if (!conflict)
  727. break;
  728. if (conflict != parent) {
  729. parent = conflict;
  730. if (!(conflict->flags & IORESOURCE_BUSY))
  731. continue;
  732. }
  733. if (conflict->flags & flags & IORESOURCE_MUXED) {
  734. add_wait_queue(&muxed_resource_wait, &wait);
  735. write_unlock(&resource_lock);
  736. set_current_state(TASK_UNINTERRUPTIBLE);
  737. schedule();
  738. remove_wait_queue(&muxed_resource_wait, &wait);
  739. write_lock(&resource_lock);
  740. continue;
  741. }
  742. /* Uhhuh, that didn't work out.. */
  743. kfree(res);
  744. res = NULL;
  745. break;
  746. }
  747. write_unlock(&resource_lock);
  748. return res;
  749. }
  750. EXPORT_SYMBOL(__request_region);
  751. /**
  752. * __check_region - check if a resource region is busy or free
  753. * @parent: parent resource descriptor
  754. * @start: resource start address
  755. * @n: resource region size
  756. *
  757. * Returns 0 if the region is free at the moment it is checked,
  758. * returns %-EBUSY if the region is busy.
  759. *
  760. * NOTE:
  761. * This function is deprecated because its use is racy.
  762. * Even if it returns 0, a subsequent call to request_region()
  763. * may fail because another driver etc. just allocated the region.
  764. * Do NOT use it. It will be removed from the kernel.
  765. */
  766. int __check_region(struct resource *parent, resource_size_t start,
  767. resource_size_t n)
  768. {
  769. struct resource * res;
  770. res = __request_region(parent, start, n, "check-region", 0);
  771. if (!res)
  772. return -EBUSY;
  773. release_resource(res);
  774. kfree(res);
  775. return 0;
  776. }
  777. EXPORT_SYMBOL(__check_region);
  778. /**
  779. * __release_region - release a previously reserved resource region
  780. * @parent: parent resource descriptor
  781. * @start: resource start address
  782. * @n: resource region size
  783. *
  784. * The described resource region must match a currently busy region.
  785. */
  786. void __release_region(struct resource *parent, resource_size_t start,
  787. resource_size_t n)
  788. {
  789. struct resource **p;
  790. resource_size_t end;
  791. p = &parent->child;
  792. end = start + n - 1;
  793. write_lock(&resource_lock);
  794. for (;;) {
  795. struct resource *res = *p;
  796. if (!res)
  797. break;
  798. if (res->start <= start && res->end >= end) {
  799. if (!(res->flags & IORESOURCE_BUSY)) {
  800. p = &res->child;
  801. continue;
  802. }
  803. if (res->start != start || res->end != end)
  804. break;
  805. *p = res->sibling;
  806. write_unlock(&resource_lock);
  807. if (res->flags & IORESOURCE_MUXED)
  808. wake_up(&muxed_resource_wait);
  809. kfree(res);
  810. return;
  811. }
  812. p = &res->sibling;
  813. }
  814. write_unlock(&resource_lock);
  815. printk(KERN_WARNING "Trying to free nonexistent resource "
  816. "<%016llx-%016llx>\n", (unsigned long long)start,
  817. (unsigned long long)end);
  818. }
  819. EXPORT_SYMBOL(__release_region);
  820. /*
  821. * Managed region resource
  822. */
  823. struct region_devres {
  824. struct resource *parent;
  825. resource_size_t start;
  826. resource_size_t n;
  827. };
  828. static void devm_region_release(struct device *dev, void *res)
  829. {
  830. struct region_devres *this = res;
  831. __release_region(this->parent, this->start, this->n);
  832. }
  833. static int devm_region_match(struct device *dev, void *res, void *match_data)
  834. {
  835. struct region_devres *this = res, *match = match_data;
  836. return this->parent == match->parent &&
  837. this->start == match->start && this->n == match->n;
  838. }
  839. struct resource * __devm_request_region(struct device *dev,
  840. struct resource *parent, resource_size_t start,
  841. resource_size_t n, const char *name)
  842. {
  843. struct region_devres *dr = NULL;
  844. struct resource *res;
  845. dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
  846. GFP_KERNEL);
  847. if (!dr)
  848. return NULL;
  849. dr->parent = parent;
  850. dr->start = start;
  851. dr->n = n;
  852. res = __request_region(parent, start, n, name, 0);
  853. if (res)
  854. devres_add(dev, dr);
  855. else
  856. devres_free(dr);
  857. return res;
  858. }
  859. EXPORT_SYMBOL(__devm_request_region);
  860. void __devm_release_region(struct device *dev, struct resource *parent,
  861. resource_size_t start, resource_size_t n)
  862. {
  863. struct region_devres match_data = { parent, start, n };
  864. __release_region(parent, start, n);
  865. WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
  866. &match_data));
  867. }
  868. EXPORT_SYMBOL(__devm_release_region);
  869. /*
  870. * Called from init/main.c to reserve IO ports.
  871. */
  872. #define MAXRESERVE 4
  873. static int __init reserve_setup(char *str)
  874. {
  875. static int reserved;
  876. static struct resource reserve[MAXRESERVE];
  877. for (;;) {
  878. unsigned int io_start, io_num;
  879. int x = reserved;
  880. if (get_option (&str, &io_start) != 2)
  881. break;
  882. if (get_option (&str, &io_num) == 0)
  883. break;
  884. if (x < MAXRESERVE) {
  885. struct resource *res = reserve + x;
  886. res->name = "reserved";
  887. res->start = io_start;
  888. res->end = io_start + io_num - 1;
  889. res->flags = IORESOURCE_BUSY;
  890. res->child = NULL;
  891. if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
  892. reserved = x+1;
  893. }
  894. }
  895. return 1;
  896. }
  897. __setup("reserve=", reserve_setup);
  898. /*
  899. * Check if the requested addr and size spans more than any slot in the
  900. * iomem resource tree.
  901. */
  902. int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
  903. {
  904. struct resource *p = &iomem_resource;
  905. int err = 0;
  906. loff_t l;
  907. read_lock(&resource_lock);
  908. for (p = p->child; p ; p = r_next(NULL, p, &l)) {
  909. /*
  910. * We can probably skip the resources without
  911. * IORESOURCE_IO attribute?
  912. */
  913. if (p->start >= addr + size)
  914. continue;
  915. if (p->end < addr)
  916. continue;
  917. if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
  918. PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
  919. continue;
  920. /*
  921. * if a resource is "BUSY", it's not a hardware resource
  922. * but a driver mapping of such a resource; we don't want
  923. * to warn for those; some drivers legitimately map only
  924. * partial hardware resources. (example: vesafb)
  925. */
  926. if (p->flags & IORESOURCE_BUSY)
  927. continue;
  928. printk(KERN_WARNING "resource map sanity check conflict: "
  929. "0x%llx 0x%llx 0x%llx 0x%llx %s\n",
  930. (unsigned long long)addr,
  931. (unsigned long long)(addr + size - 1),
  932. (unsigned long long)p->start,
  933. (unsigned long long)p->end,
  934. p->name);
  935. err = -1;
  936. break;
  937. }
  938. read_unlock(&resource_lock);
  939. return err;
  940. }
  941. #ifdef CONFIG_STRICT_DEVMEM
  942. static int strict_iomem_checks = 1;
  943. #else
  944. static int strict_iomem_checks;
  945. #endif
  946. /*
  947. * check if an address is reserved in the iomem resource tree
  948. * returns 1 if reserved, 0 if not reserved.
  949. */
  950. int iomem_is_exclusive(u64 addr)
  951. {
  952. struct resource *p = &iomem_resource;
  953. int err = 0;
  954. loff_t l;
  955. int size = PAGE_SIZE;
  956. if (!strict_iomem_checks)
  957. return 0;
  958. addr = addr & PAGE_MASK;
  959. read_lock(&resource_lock);
  960. for (p = p->child; p ; p = r_next(NULL, p, &l)) {
  961. /*
  962. * We can probably skip the resources without
  963. * IORESOURCE_IO attribute?
  964. */
  965. if (p->start >= addr + size)
  966. break;
  967. if (p->end < addr)
  968. continue;
  969. if (p->flags & IORESOURCE_BUSY &&
  970. p->flags & IORESOURCE_EXCLUSIVE) {
  971. err = 1;
  972. break;
  973. }
  974. }
  975. read_unlock(&resource_lock);
  976. return err;
  977. }
  978. static int __init strict_iomem(char *str)
  979. {
  980. if (strstr(str, "relaxed"))
  981. strict_iomem_checks = 0;
  982. if (strstr(str, "strict"))
  983. strict_iomem_checks = 1;
  984. return 1;
  985. }
  986. __setup("iomem=", strict_iomem);