resource.c 31 KB

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