resource.c 32 KB

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