resource.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/kernel/resource.c
  4. *
  5. * Copyright (C) 1999 Linus Torvalds
  6. * Copyright (C) 1999 Martin Mares <mj@ucw.cz>
  7. *
  8. * Arbitrary resource management.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/export.h>
  12. #include <linux/errno.h>
  13. #include <linux/ioport.h>
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/fs.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/pseudo_fs.h>
  20. #include <linux/sched.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/device.h>
  23. #include <linux/pfn.h>
  24. #include <linux/mm.h>
  25. #include <linux/mount.h>
  26. #include <linux/resource_ext.h>
  27. #include <uapi/linux/magic.h>
  28. #include <linux/string.h>
  29. #include <linux/vmalloc.h>
  30. #include <asm/io.h>
  31. struct resource ioport_resource = {
  32. .name = "PCI IO",
  33. .start = 0,
  34. .end = IO_SPACE_LIMIT,
  35. .flags = IORESOURCE_IO,
  36. };
  37. EXPORT_SYMBOL(ioport_resource);
  38. struct resource iomem_resource = {
  39. .name = "PCI mem",
  40. .start = 0,
  41. .end = -1,
  42. .flags = IORESOURCE_MEM,
  43. };
  44. EXPORT_SYMBOL(iomem_resource);
  45. static DEFINE_RWLOCK(resource_lock);
  46. static struct resource *next_resource(struct resource *p, bool skip_children)
  47. {
  48. if (!skip_children && p->child)
  49. return p->child;
  50. while (!p->sibling && p->parent)
  51. p = p->parent;
  52. return p->sibling;
  53. }
  54. #define for_each_resource(_root, _p, _skip_children) \
  55. for ((_p) = (_root)->child; (_p); (_p) = next_resource(_p, _skip_children))
  56. #ifdef CONFIG_PROC_FS
  57. enum { MAX_IORES_LEVEL = 5 };
  58. static void *r_start(struct seq_file *m, loff_t *pos)
  59. __acquires(resource_lock)
  60. {
  61. struct resource *root = pde_data(file_inode(m->file));
  62. struct resource *p;
  63. loff_t l = *pos;
  64. read_lock(&resource_lock);
  65. for_each_resource(root, p, false) {
  66. if (l-- == 0)
  67. break;
  68. }
  69. return p;
  70. }
  71. static void *r_next(struct seq_file *m, void *v, loff_t *pos)
  72. {
  73. struct resource *p = v;
  74. (*pos)++;
  75. return (void *)next_resource(p, false);
  76. }
  77. static void r_stop(struct seq_file *m, void *v)
  78. __releases(resource_lock)
  79. {
  80. read_unlock(&resource_lock);
  81. }
  82. static int r_show(struct seq_file *m, void *v)
  83. {
  84. struct resource *root = pde_data(file_inode(m->file));
  85. struct resource *r = v, *p;
  86. unsigned long long start, end;
  87. int width = root->end < 0x10000 ? 4 : 8;
  88. int depth;
  89. for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
  90. if (p->parent == root)
  91. break;
  92. if (file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN)) {
  93. start = r->start;
  94. end = r->end;
  95. } else {
  96. start = end = 0;
  97. }
  98. seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
  99. depth * 2, "",
  100. width, start,
  101. width, end,
  102. r->name ? r->name : "<BAD>");
  103. return 0;
  104. }
  105. static const struct seq_operations resource_op = {
  106. .start = r_start,
  107. .next = r_next,
  108. .stop = r_stop,
  109. .show = r_show,
  110. };
  111. static int __init ioresources_init(void)
  112. {
  113. proc_create_seq_data("ioports", 0, NULL, &resource_op,
  114. &ioport_resource);
  115. proc_create_seq_data("iomem", 0, NULL, &resource_op, &iomem_resource);
  116. return 0;
  117. }
  118. __initcall(ioresources_init);
  119. #endif /* CONFIG_PROC_FS */
  120. static void free_resource(struct resource *res)
  121. {
  122. /**
  123. * If the resource was allocated using memblock early during boot
  124. * we'll leak it here: we can only return full pages back to the
  125. * buddy and trying to be smart and reusing them eventually in
  126. * alloc_resource() overcomplicates resource handling.
  127. */
  128. if (res && PageSlab(virt_to_head_page(res)))
  129. kfree(res);
  130. }
  131. static struct resource *alloc_resource(gfp_t flags)
  132. {
  133. return kzalloc(sizeof(struct resource), flags);
  134. }
  135. /* Return the conflict entry if you can't request it */
  136. static struct resource * __request_resource(struct resource *root, struct resource *new)
  137. {
  138. resource_size_t start = new->start;
  139. resource_size_t end = new->end;
  140. struct resource *tmp, **p;
  141. if (end < start)
  142. return root;
  143. if (start < root->start)
  144. return root;
  145. if (end > root->end)
  146. return root;
  147. p = &root->child;
  148. for (;;) {
  149. tmp = *p;
  150. if (!tmp || tmp->start > end) {
  151. new->sibling = tmp;
  152. *p = new;
  153. new->parent = root;
  154. return NULL;
  155. }
  156. p = &tmp->sibling;
  157. if (tmp->end < start)
  158. continue;
  159. return tmp;
  160. }
  161. }
  162. static int __release_resource(struct resource *old, bool release_child)
  163. {
  164. struct resource *tmp, **p, *chd;
  165. p = &old->parent->child;
  166. for (;;) {
  167. tmp = *p;
  168. if (!tmp)
  169. break;
  170. if (tmp == old) {
  171. if (release_child || !(tmp->child)) {
  172. *p = tmp->sibling;
  173. } else {
  174. for (chd = tmp->child;; chd = chd->sibling) {
  175. chd->parent = tmp->parent;
  176. if (!(chd->sibling))
  177. break;
  178. }
  179. *p = tmp->child;
  180. chd->sibling = tmp->sibling;
  181. }
  182. old->parent = NULL;
  183. return 0;
  184. }
  185. p = &tmp->sibling;
  186. }
  187. return -EINVAL;
  188. }
  189. static void __release_child_resources(struct resource *r)
  190. {
  191. struct resource *tmp, *p;
  192. resource_size_t size;
  193. p = r->child;
  194. r->child = NULL;
  195. while (p) {
  196. tmp = p;
  197. p = p->sibling;
  198. tmp->parent = NULL;
  199. tmp->sibling = NULL;
  200. __release_child_resources(tmp);
  201. printk(KERN_DEBUG "release child resource %pR\n", tmp);
  202. /* need to restore size, and keep flags */
  203. size = resource_size(tmp);
  204. tmp->start = 0;
  205. tmp->end = size - 1;
  206. }
  207. }
  208. void release_child_resources(struct resource *r)
  209. {
  210. write_lock(&resource_lock);
  211. __release_child_resources(r);
  212. write_unlock(&resource_lock);
  213. }
  214. /**
  215. * request_resource_conflict - request and reserve an I/O or memory resource
  216. * @root: root resource descriptor
  217. * @new: resource descriptor desired by caller
  218. *
  219. * Returns 0 for success, conflict resource on error.
  220. */
  221. struct resource *request_resource_conflict(struct resource *root, struct resource *new)
  222. {
  223. struct resource *conflict;
  224. write_lock(&resource_lock);
  225. conflict = __request_resource(root, new);
  226. write_unlock(&resource_lock);
  227. return conflict;
  228. }
  229. /**
  230. * request_resource - request and reserve an I/O or memory resource
  231. * @root: root resource descriptor
  232. * @new: resource descriptor desired by caller
  233. *
  234. * Returns 0 for success, negative error code on error.
  235. */
  236. int request_resource(struct resource *root, struct resource *new)
  237. {
  238. struct resource *conflict;
  239. conflict = request_resource_conflict(root, new);
  240. return conflict ? -EBUSY : 0;
  241. }
  242. EXPORT_SYMBOL(request_resource);
  243. /**
  244. * release_resource - release a previously reserved resource
  245. * @old: resource pointer
  246. */
  247. int release_resource(struct resource *old)
  248. {
  249. int retval;
  250. write_lock(&resource_lock);
  251. retval = __release_resource(old, true);
  252. write_unlock(&resource_lock);
  253. return retval;
  254. }
  255. EXPORT_SYMBOL(release_resource);
  256. /**
  257. * find_next_iomem_res - Finds the lowest iomem resource that covers part of
  258. * [@start..@end].
  259. *
  260. * If a resource is found, returns 0 and @*res is overwritten with the part
  261. * of the resource that's within [@start..@end]; if none is found, returns
  262. * -ENODEV. Returns -EINVAL for invalid parameters.
  263. *
  264. * @start: start address of the resource searched for
  265. * @end: end address of same resource
  266. * @flags: flags which the resource must have
  267. * @desc: descriptor the resource must have
  268. * @res: return ptr, if resource found
  269. *
  270. * The caller must specify @start, @end, @flags, and @desc
  271. * (which may be IORES_DESC_NONE).
  272. */
  273. static int find_next_iomem_res(resource_size_t start, resource_size_t end,
  274. unsigned long flags, unsigned long desc,
  275. struct resource *res)
  276. {
  277. struct resource *p;
  278. if (!res)
  279. return -EINVAL;
  280. if (start >= end)
  281. return -EINVAL;
  282. read_lock(&resource_lock);
  283. for_each_resource(&iomem_resource, p, false) {
  284. /* If we passed the resource we are looking for, stop */
  285. if (p->start > end) {
  286. p = NULL;
  287. break;
  288. }
  289. /* Skip until we find a range that matches what we look for */
  290. if (p->end < start)
  291. continue;
  292. if ((p->flags & flags) != flags)
  293. continue;
  294. if ((desc != IORES_DESC_NONE) && (desc != p->desc))
  295. continue;
  296. /* Found a match, break */
  297. break;
  298. }
  299. if (p) {
  300. /* copy data */
  301. *res = (struct resource) {
  302. .start = max(start, p->start),
  303. .end = min(end, p->end),
  304. .flags = p->flags,
  305. .desc = p->desc,
  306. .parent = p->parent,
  307. };
  308. }
  309. read_unlock(&resource_lock);
  310. return p ? 0 : -ENODEV;
  311. }
  312. static int __walk_iomem_res_desc(resource_size_t start, resource_size_t end,
  313. unsigned long flags, unsigned long desc,
  314. void *arg,
  315. int (*func)(struct resource *, void *))
  316. {
  317. struct resource res;
  318. int ret = -EINVAL;
  319. while (start < end &&
  320. !find_next_iomem_res(start, end, flags, desc, &res)) {
  321. ret = (*func)(&res, arg);
  322. if (ret)
  323. break;
  324. start = res.end + 1;
  325. }
  326. return ret;
  327. }
  328. /**
  329. * walk_iomem_res_desc - Walks through iomem resources and calls func()
  330. * with matching resource ranges.
  331. * *
  332. * @desc: I/O resource descriptor. Use IORES_DESC_NONE to skip @desc check.
  333. * @flags: I/O resource flags
  334. * @start: start addr
  335. * @end: end addr
  336. * @arg: function argument for the callback @func
  337. * @func: callback function that is called for each qualifying resource area
  338. *
  339. * All the memory ranges which overlap start,end and also match flags and
  340. * desc are valid candidates.
  341. *
  342. * NOTE: For a new descriptor search, define a new IORES_DESC in
  343. * <linux/ioport.h> and set it in 'desc' of a target resource entry.
  344. */
  345. int walk_iomem_res_desc(unsigned long desc, unsigned long flags, u64 start,
  346. u64 end, void *arg, int (*func)(struct resource *, void *))
  347. {
  348. return __walk_iomem_res_desc(start, end, flags, desc, arg, func);
  349. }
  350. EXPORT_SYMBOL_GPL(walk_iomem_res_desc);
  351. /*
  352. * This function calls the @func callback against all memory ranges of type
  353. * System RAM which are marked as IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY.
  354. * Now, this function is only for System RAM, it deals with full ranges and
  355. * not PFNs. If resources are not PFN-aligned, dealing with PFNs can truncate
  356. * ranges.
  357. */
  358. int walk_system_ram_res(u64 start, u64 end, void *arg,
  359. int (*func)(struct resource *, void *))
  360. {
  361. unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  362. return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, arg,
  363. func);
  364. }
  365. /*
  366. * This function, being a variant of walk_system_ram_res(), calls the @func
  367. * callback against all memory ranges of type System RAM which are marked as
  368. * IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY in reversed order, i.e., from
  369. * higher to lower.
  370. */
  371. int walk_system_ram_res_rev(u64 start, u64 end, void *arg,
  372. int (*func)(struct resource *, void *))
  373. {
  374. struct resource res, *rams;
  375. int rams_size = 16, i;
  376. unsigned long flags;
  377. int ret = -1;
  378. /* create a list */
  379. rams = kvcalloc(rams_size, sizeof(struct resource), GFP_KERNEL);
  380. if (!rams)
  381. return ret;
  382. flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  383. i = 0;
  384. while ((start < end) &&
  385. (!find_next_iomem_res(start, end, flags, IORES_DESC_NONE, &res))) {
  386. if (i >= rams_size) {
  387. /* re-alloc */
  388. struct resource *rams_new;
  389. rams_new = kvrealloc(rams, (rams_size + 16) * sizeof(struct resource),
  390. GFP_KERNEL);
  391. if (!rams_new)
  392. goto out;
  393. rams = rams_new;
  394. rams_size += 16;
  395. }
  396. rams[i++] = res;
  397. start = res.end + 1;
  398. }
  399. /* go reverse */
  400. for (i--; i >= 0; i--) {
  401. ret = (*func)(&rams[i], arg);
  402. if (ret)
  403. break;
  404. }
  405. out:
  406. kvfree(rams);
  407. return ret;
  408. }
  409. /*
  410. * This function calls the @func callback against all memory ranges, which
  411. * are ranges marked as IORESOURCE_MEM and IORESOUCE_BUSY.
  412. */
  413. int walk_mem_res(u64 start, u64 end, void *arg,
  414. int (*func)(struct resource *, void *))
  415. {
  416. unsigned long flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  417. return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, arg,
  418. func);
  419. }
  420. /*
  421. * This function calls the @func callback against all memory ranges of type
  422. * System RAM which are marked as IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY.
  423. * It is to be used only for System RAM.
  424. */
  425. int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
  426. void *arg, int (*func)(unsigned long, unsigned long, void *))
  427. {
  428. resource_size_t start, end;
  429. unsigned long flags;
  430. struct resource res;
  431. unsigned long pfn, end_pfn;
  432. int ret = -EINVAL;
  433. start = (u64) start_pfn << PAGE_SHIFT;
  434. end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
  435. flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  436. while (start < end &&
  437. !find_next_iomem_res(start, end, flags, IORES_DESC_NONE, &res)) {
  438. pfn = PFN_UP(res.start);
  439. end_pfn = PFN_DOWN(res.end + 1);
  440. if (end_pfn > pfn)
  441. ret = (*func)(pfn, end_pfn - pfn, arg);
  442. if (ret)
  443. break;
  444. start = res.end + 1;
  445. }
  446. return ret;
  447. }
  448. static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
  449. {
  450. return 1;
  451. }
  452. /*
  453. * This generic page_is_ram() returns true if specified address is
  454. * registered as System RAM in iomem_resource list.
  455. */
  456. int __weak page_is_ram(unsigned long pfn)
  457. {
  458. return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
  459. }
  460. EXPORT_SYMBOL_GPL(page_is_ram);
  461. static int __region_intersects(struct resource *parent, resource_size_t start,
  462. size_t size, unsigned long flags,
  463. unsigned long desc)
  464. {
  465. resource_size_t ostart, oend;
  466. int type = 0; int other = 0;
  467. struct resource *p, *dp;
  468. bool is_type, covered;
  469. struct resource res;
  470. res.start = start;
  471. res.end = start + size - 1;
  472. for (p = parent->child; p ; p = p->sibling) {
  473. if (!resource_overlaps(p, &res))
  474. continue;
  475. is_type = (p->flags & flags) == flags &&
  476. (desc == IORES_DESC_NONE || desc == p->desc);
  477. if (is_type) {
  478. type++;
  479. continue;
  480. }
  481. /*
  482. * Continue to search in descendant resources as if the
  483. * matched descendant resources cover some ranges of 'p'.
  484. *
  485. * |------------- "CXL Window 0" ------------|
  486. * |-- "System RAM" --|
  487. *
  488. * will behave similar as the following fake resource
  489. * tree when searching "System RAM".
  490. *
  491. * |-- "System RAM" --||-- "CXL Window 0a" --|
  492. */
  493. covered = false;
  494. ostart = max(res.start, p->start);
  495. oend = min(res.end, p->end);
  496. for_each_resource(p, dp, false) {
  497. if (!resource_overlaps(dp, &res))
  498. continue;
  499. is_type = (dp->flags & flags) == flags &&
  500. (desc == IORES_DESC_NONE || desc == dp->desc);
  501. if (is_type) {
  502. type++;
  503. /*
  504. * Range from 'ostart' to 'dp->start'
  505. * isn't covered by matched resource.
  506. */
  507. if (dp->start > ostart)
  508. break;
  509. if (dp->end >= oend) {
  510. covered = true;
  511. break;
  512. }
  513. /* Remove covered range */
  514. ostart = max(ostart, dp->end + 1);
  515. }
  516. }
  517. if (!covered)
  518. other++;
  519. }
  520. if (type == 0)
  521. return REGION_DISJOINT;
  522. if (other == 0)
  523. return REGION_INTERSECTS;
  524. return REGION_MIXED;
  525. }
  526. /**
  527. * region_intersects() - determine intersection of region with known resources
  528. * @start: region start address
  529. * @size: size of region
  530. * @flags: flags of resource (in iomem_resource)
  531. * @desc: descriptor of resource (in iomem_resource) or IORES_DESC_NONE
  532. *
  533. * Check if the specified region partially overlaps or fully eclipses a
  534. * resource identified by @flags and @desc (optional with IORES_DESC_NONE).
  535. * Return REGION_DISJOINT if the region does not overlap @flags/@desc,
  536. * return REGION_MIXED if the region overlaps @flags/@desc and another
  537. * resource, and return REGION_INTERSECTS if the region overlaps @flags/@desc
  538. * and no other defined resource. Note that REGION_INTERSECTS is also
  539. * returned in the case when the specified region overlaps RAM and undefined
  540. * memory holes.
  541. *
  542. * region_intersect() is used by memory remapping functions to ensure
  543. * the user is not remapping RAM and is a vast speed up over walking
  544. * through the resource table page by page.
  545. */
  546. int region_intersects(resource_size_t start, size_t size, unsigned long flags,
  547. unsigned long desc)
  548. {
  549. int ret;
  550. read_lock(&resource_lock);
  551. ret = __region_intersects(&iomem_resource, start, size, flags, desc);
  552. read_unlock(&resource_lock);
  553. return ret;
  554. }
  555. EXPORT_SYMBOL_GPL(region_intersects);
  556. void __weak arch_remove_reservations(struct resource *avail)
  557. {
  558. }
  559. static void resource_clip(struct resource *res, resource_size_t min,
  560. resource_size_t max)
  561. {
  562. if (res->start < min)
  563. res->start = min;
  564. if (res->end > max)
  565. res->end = max;
  566. }
  567. /*
  568. * Find empty space in the resource tree with the given range and
  569. * alignment constraints
  570. */
  571. static int __find_resource_space(struct resource *root, struct resource *old,
  572. struct resource *new, resource_size_t size,
  573. struct resource_constraint *constraint)
  574. {
  575. struct resource *this = root->child;
  576. struct resource tmp = *new, avail, alloc;
  577. resource_alignf alignf = constraint->alignf;
  578. tmp.start = root->start;
  579. /*
  580. * Skip past an allocated resource that starts at 0, since the assignment
  581. * of this->start - 1 to tmp->end below would cause an underflow.
  582. */
  583. if (this && this->start == root->start) {
  584. tmp.start = (this == old) ? old->start : this->end + 1;
  585. this = this->sibling;
  586. }
  587. for(;;) {
  588. if (this)
  589. tmp.end = (this == old) ? this->end : this->start - 1;
  590. else
  591. tmp.end = root->end;
  592. if (tmp.end < tmp.start)
  593. goto next;
  594. resource_clip(&tmp, constraint->min, constraint->max);
  595. arch_remove_reservations(&tmp);
  596. /* Check for overflow after ALIGN() */
  597. avail.start = ALIGN(tmp.start, constraint->align);
  598. avail.end = tmp.end;
  599. avail.flags = new->flags & ~IORESOURCE_UNSET;
  600. if (avail.start >= tmp.start) {
  601. alloc.flags = avail.flags;
  602. if (alignf) {
  603. alloc.start = alignf(constraint->alignf_data,
  604. &avail, size, constraint->align);
  605. } else {
  606. alloc.start = avail.start;
  607. }
  608. alloc.end = alloc.start + size - 1;
  609. if (alloc.start <= alloc.end &&
  610. resource_contains(&avail, &alloc)) {
  611. new->start = alloc.start;
  612. new->end = alloc.end;
  613. return 0;
  614. }
  615. }
  616. next: if (!this || this->end == root->end)
  617. break;
  618. if (this != old)
  619. tmp.start = this->end + 1;
  620. this = this->sibling;
  621. }
  622. return -EBUSY;
  623. }
  624. /**
  625. * find_resource_space - Find empty space in the resource tree
  626. * @root: Root resource descriptor
  627. * @new: Resource descriptor awaiting an empty resource space
  628. * @size: The minimum size of the empty space
  629. * @constraint: The range and alignment constraints to be met
  630. *
  631. * Finds an empty space under @root in the resource tree satisfying range and
  632. * alignment @constraints.
  633. *
  634. * Return:
  635. * * %0 - if successful, @new members start, end, and flags are altered.
  636. * * %-EBUSY - if no empty space was found.
  637. */
  638. int find_resource_space(struct resource *root, struct resource *new,
  639. resource_size_t size,
  640. struct resource_constraint *constraint)
  641. {
  642. return __find_resource_space(root, NULL, new, size, constraint);
  643. }
  644. EXPORT_SYMBOL_GPL(find_resource_space);
  645. /**
  646. * reallocate_resource - allocate a slot in the resource tree given range & alignment.
  647. * The resource will be relocated if the new size cannot be reallocated in the
  648. * current location.
  649. *
  650. * @root: root resource descriptor
  651. * @old: resource descriptor desired by caller
  652. * @newsize: new size of the resource descriptor
  653. * @constraint: the size and alignment constraints to be met.
  654. */
  655. static int reallocate_resource(struct resource *root, struct resource *old,
  656. resource_size_t newsize,
  657. struct resource_constraint *constraint)
  658. {
  659. int err=0;
  660. struct resource new = *old;
  661. struct resource *conflict;
  662. write_lock(&resource_lock);
  663. if ((err = __find_resource_space(root, old, &new, newsize, constraint)))
  664. goto out;
  665. if (resource_contains(&new, old)) {
  666. old->start = new.start;
  667. old->end = new.end;
  668. goto out;
  669. }
  670. if (old->child) {
  671. err = -EBUSY;
  672. goto out;
  673. }
  674. if (resource_contains(old, &new)) {
  675. old->start = new.start;
  676. old->end = new.end;
  677. } else {
  678. __release_resource(old, true);
  679. *old = new;
  680. conflict = __request_resource(root, old);
  681. BUG_ON(conflict);
  682. }
  683. out:
  684. write_unlock(&resource_lock);
  685. return err;
  686. }
  687. /**
  688. * allocate_resource - allocate empty slot in the resource tree given range & alignment.
  689. * The resource will be reallocated with a new size if it was already allocated
  690. * @root: root resource descriptor
  691. * @new: resource descriptor desired by caller
  692. * @size: requested resource region size
  693. * @min: minimum boundary to allocate
  694. * @max: maximum boundary to allocate
  695. * @align: alignment requested, in bytes
  696. * @alignf: alignment function, optional, called if not NULL
  697. * @alignf_data: arbitrary data to pass to the @alignf function
  698. */
  699. int allocate_resource(struct resource *root, struct resource *new,
  700. resource_size_t size, resource_size_t min,
  701. resource_size_t max, resource_size_t align,
  702. resource_alignf alignf,
  703. void *alignf_data)
  704. {
  705. int err;
  706. struct resource_constraint constraint;
  707. constraint.min = min;
  708. constraint.max = max;
  709. constraint.align = align;
  710. constraint.alignf = alignf;
  711. constraint.alignf_data = alignf_data;
  712. if ( new->parent ) {
  713. /* resource is already allocated, try reallocating with
  714. the new constraints */
  715. return reallocate_resource(root, new, size, &constraint);
  716. }
  717. write_lock(&resource_lock);
  718. err = find_resource_space(root, new, size, &constraint);
  719. if (err >= 0 && __request_resource(root, new))
  720. err = -EBUSY;
  721. write_unlock(&resource_lock);
  722. return err;
  723. }
  724. EXPORT_SYMBOL(allocate_resource);
  725. /**
  726. * lookup_resource - find an existing resource by a resource start address
  727. * @root: root resource descriptor
  728. * @start: resource start address
  729. *
  730. * Returns a pointer to the resource if found, NULL otherwise
  731. */
  732. struct resource *lookup_resource(struct resource *root, resource_size_t start)
  733. {
  734. struct resource *res;
  735. read_lock(&resource_lock);
  736. for (res = root->child; res; res = res->sibling) {
  737. if (res->start == start)
  738. break;
  739. }
  740. read_unlock(&resource_lock);
  741. return res;
  742. }
  743. /*
  744. * Insert a resource into the resource tree. If successful, return NULL,
  745. * otherwise return the conflicting resource (compare to __request_resource())
  746. */
  747. static struct resource * __insert_resource(struct resource *parent, struct resource *new)
  748. {
  749. struct resource *first, *next;
  750. for (;; parent = first) {
  751. first = __request_resource(parent, new);
  752. if (!first)
  753. return first;
  754. if (first == parent)
  755. return first;
  756. if (WARN_ON(first == new)) /* duplicated insertion */
  757. return first;
  758. if ((first->start > new->start) || (first->end < new->end))
  759. break;
  760. if ((first->start == new->start) && (first->end == new->end))
  761. break;
  762. }
  763. for (next = first; ; next = next->sibling) {
  764. /* Partial overlap? Bad, and unfixable */
  765. if (next->start < new->start || next->end > new->end)
  766. return next;
  767. if (!next->sibling)
  768. break;
  769. if (next->sibling->start > new->end)
  770. break;
  771. }
  772. new->parent = parent;
  773. new->sibling = next->sibling;
  774. new->child = first;
  775. next->sibling = NULL;
  776. for (next = first; next; next = next->sibling)
  777. next->parent = new;
  778. if (parent->child == first) {
  779. parent->child = new;
  780. } else {
  781. next = parent->child;
  782. while (next->sibling != first)
  783. next = next->sibling;
  784. next->sibling = new;
  785. }
  786. return NULL;
  787. }
  788. /**
  789. * insert_resource_conflict - Inserts resource in the resource tree
  790. * @parent: parent of the new resource
  791. * @new: new resource to insert
  792. *
  793. * Returns 0 on success, conflict resource if the resource can't be inserted.
  794. *
  795. * This function is equivalent to request_resource_conflict when no conflict
  796. * happens. If a conflict happens, and the conflicting resources
  797. * entirely fit within the range of the new resource, then the new
  798. * resource is inserted and the conflicting resources become children of
  799. * the new resource.
  800. *
  801. * This function is intended for producers of resources, such as FW modules
  802. * and bus drivers.
  803. */
  804. struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
  805. {
  806. struct resource *conflict;
  807. write_lock(&resource_lock);
  808. conflict = __insert_resource(parent, new);
  809. write_unlock(&resource_lock);
  810. return conflict;
  811. }
  812. /**
  813. * insert_resource - Inserts a resource in the resource tree
  814. * @parent: parent of the new resource
  815. * @new: new resource to insert
  816. *
  817. * Returns 0 on success, -EBUSY if the resource can't be inserted.
  818. *
  819. * This function is intended for producers of resources, such as FW modules
  820. * and bus drivers.
  821. */
  822. int insert_resource(struct resource *parent, struct resource *new)
  823. {
  824. struct resource *conflict;
  825. conflict = insert_resource_conflict(parent, new);
  826. return conflict ? -EBUSY : 0;
  827. }
  828. EXPORT_SYMBOL_GPL(insert_resource);
  829. /**
  830. * insert_resource_expand_to_fit - Insert a resource into the resource tree
  831. * @root: root resource descriptor
  832. * @new: new resource to insert
  833. *
  834. * Insert a resource into the resource tree, possibly expanding it in order
  835. * to make it encompass any conflicting resources.
  836. */
  837. void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
  838. {
  839. if (new->parent)
  840. return;
  841. write_lock(&resource_lock);
  842. for (;;) {
  843. struct resource *conflict;
  844. conflict = __insert_resource(root, new);
  845. if (!conflict)
  846. break;
  847. if (conflict == root)
  848. break;
  849. /* Ok, expand resource to cover the conflict, then try again .. */
  850. if (conflict->start < new->start)
  851. new->start = conflict->start;
  852. if (conflict->end > new->end)
  853. new->end = conflict->end;
  854. pr_info("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
  855. }
  856. write_unlock(&resource_lock);
  857. }
  858. /*
  859. * Not for general consumption, only early boot memory map parsing, PCI
  860. * resource discovery, and late discovery of CXL resources are expected
  861. * to use this interface. The former are built-in and only the latter,
  862. * CXL, is a module.
  863. */
  864. EXPORT_SYMBOL_NS_GPL(insert_resource_expand_to_fit, CXL);
  865. /**
  866. * remove_resource - Remove a resource in the resource tree
  867. * @old: resource to remove
  868. *
  869. * Returns 0 on success, -EINVAL if the resource is not valid.
  870. *
  871. * This function removes a resource previously inserted by insert_resource()
  872. * or insert_resource_conflict(), and moves the children (if any) up to
  873. * where they were before. insert_resource() and insert_resource_conflict()
  874. * insert a new resource, and move any conflicting resources down to the
  875. * children of the new resource.
  876. *
  877. * insert_resource(), insert_resource_conflict() and remove_resource() are
  878. * intended for producers of resources, such as FW modules and bus drivers.
  879. */
  880. int remove_resource(struct resource *old)
  881. {
  882. int retval;
  883. write_lock(&resource_lock);
  884. retval = __release_resource(old, false);
  885. write_unlock(&resource_lock);
  886. return retval;
  887. }
  888. EXPORT_SYMBOL_GPL(remove_resource);
  889. static int __adjust_resource(struct resource *res, resource_size_t start,
  890. resource_size_t size)
  891. {
  892. struct resource *tmp, *parent = res->parent;
  893. resource_size_t end = start + size - 1;
  894. int result = -EBUSY;
  895. if (!parent)
  896. goto skip;
  897. if ((start < parent->start) || (end > parent->end))
  898. goto out;
  899. if (res->sibling && (res->sibling->start <= end))
  900. goto out;
  901. tmp = parent->child;
  902. if (tmp != res) {
  903. while (tmp->sibling != res)
  904. tmp = tmp->sibling;
  905. if (start <= tmp->end)
  906. goto out;
  907. }
  908. skip:
  909. for (tmp = res->child; tmp; tmp = tmp->sibling)
  910. if ((tmp->start < start) || (tmp->end > end))
  911. goto out;
  912. res->start = start;
  913. res->end = end;
  914. result = 0;
  915. out:
  916. return result;
  917. }
  918. /**
  919. * adjust_resource - modify a resource's start and size
  920. * @res: resource to modify
  921. * @start: new start value
  922. * @size: new size
  923. *
  924. * Given an existing resource, change its start and size to match the
  925. * arguments. Returns 0 on success, -EBUSY if it can't fit.
  926. * Existing children of the resource are assumed to be immutable.
  927. */
  928. int adjust_resource(struct resource *res, resource_size_t start,
  929. resource_size_t size)
  930. {
  931. int result;
  932. write_lock(&resource_lock);
  933. result = __adjust_resource(res, start, size);
  934. write_unlock(&resource_lock);
  935. return result;
  936. }
  937. EXPORT_SYMBOL(adjust_resource);
  938. static void __init
  939. __reserve_region_with_split(struct resource *root, resource_size_t start,
  940. resource_size_t end, const char *name)
  941. {
  942. struct resource *parent = root;
  943. struct resource *conflict;
  944. struct resource *res = alloc_resource(GFP_ATOMIC);
  945. struct resource *next_res = NULL;
  946. int type = resource_type(root);
  947. if (!res)
  948. return;
  949. res->name = name;
  950. res->start = start;
  951. res->end = end;
  952. res->flags = type | IORESOURCE_BUSY;
  953. res->desc = IORES_DESC_NONE;
  954. while (1) {
  955. conflict = __request_resource(parent, res);
  956. if (!conflict) {
  957. if (!next_res)
  958. break;
  959. res = next_res;
  960. next_res = NULL;
  961. continue;
  962. }
  963. /* conflict covered whole area */
  964. if (conflict->start <= res->start &&
  965. conflict->end >= res->end) {
  966. free_resource(res);
  967. WARN_ON(next_res);
  968. break;
  969. }
  970. /* failed, split and try again */
  971. if (conflict->start > res->start) {
  972. end = res->end;
  973. res->end = conflict->start - 1;
  974. if (conflict->end < end) {
  975. next_res = alloc_resource(GFP_ATOMIC);
  976. if (!next_res) {
  977. free_resource(res);
  978. break;
  979. }
  980. next_res->name = name;
  981. next_res->start = conflict->end + 1;
  982. next_res->end = end;
  983. next_res->flags = type | IORESOURCE_BUSY;
  984. next_res->desc = IORES_DESC_NONE;
  985. }
  986. } else {
  987. res->start = conflict->end + 1;
  988. }
  989. }
  990. }
  991. void __init
  992. reserve_region_with_split(struct resource *root, resource_size_t start,
  993. resource_size_t end, const char *name)
  994. {
  995. int abort = 0;
  996. write_lock(&resource_lock);
  997. if (root->start > start || root->end < end) {
  998. pr_err("requested range [0x%llx-0x%llx] not in root %pr\n",
  999. (unsigned long long)start, (unsigned long long)end,
  1000. root);
  1001. if (start > root->end || end < root->start)
  1002. abort = 1;
  1003. else {
  1004. if (end > root->end)
  1005. end = root->end;
  1006. if (start < root->start)
  1007. start = root->start;
  1008. pr_err("fixing request to [0x%llx-0x%llx]\n",
  1009. (unsigned long long)start,
  1010. (unsigned long long)end);
  1011. }
  1012. dump_stack();
  1013. }
  1014. if (!abort)
  1015. __reserve_region_with_split(root, start, end, name);
  1016. write_unlock(&resource_lock);
  1017. }
  1018. /**
  1019. * resource_alignment - calculate resource's alignment
  1020. * @res: resource pointer
  1021. *
  1022. * Returns alignment on success, 0 (invalid alignment) on failure.
  1023. */
  1024. resource_size_t resource_alignment(struct resource *res)
  1025. {
  1026. switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
  1027. case IORESOURCE_SIZEALIGN:
  1028. return resource_size(res);
  1029. case IORESOURCE_STARTALIGN:
  1030. return res->start;
  1031. default:
  1032. return 0;
  1033. }
  1034. }
  1035. /*
  1036. * This is compatibility stuff for IO resources.
  1037. *
  1038. * Note how this, unlike the above, knows about
  1039. * the IO flag meanings (busy etc).
  1040. *
  1041. * request_region creates a new busy region.
  1042. *
  1043. * release_region releases a matching busy region.
  1044. */
  1045. static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
  1046. static struct inode *iomem_inode;
  1047. #ifdef CONFIG_IO_STRICT_DEVMEM
  1048. static void revoke_iomem(struct resource *res)
  1049. {
  1050. /* pairs with smp_store_release() in iomem_init_inode() */
  1051. struct inode *inode = smp_load_acquire(&iomem_inode);
  1052. /*
  1053. * Check that the initialization has completed. Losing the race
  1054. * is ok because it means drivers are claiming resources before
  1055. * the fs_initcall level of init and prevent iomem_get_mapping users
  1056. * from establishing mappings.
  1057. */
  1058. if (!inode)
  1059. return;
  1060. /*
  1061. * The expectation is that the driver has successfully marked
  1062. * the resource busy by this point, so devmem_is_allowed()
  1063. * should start returning false, however for performance this
  1064. * does not iterate the entire resource range.
  1065. */
  1066. if (devmem_is_allowed(PHYS_PFN(res->start)) &&
  1067. devmem_is_allowed(PHYS_PFN(res->end))) {
  1068. /*
  1069. * *cringe* iomem=relaxed says "go ahead, what's the
  1070. * worst that can happen?"
  1071. */
  1072. return;
  1073. }
  1074. unmap_mapping_range(inode->i_mapping, res->start, resource_size(res), 1);
  1075. }
  1076. #else
  1077. static void revoke_iomem(struct resource *res) {}
  1078. #endif
  1079. struct address_space *iomem_get_mapping(void)
  1080. {
  1081. /*
  1082. * This function is only called from file open paths, hence guaranteed
  1083. * that fs_initcalls have completed and no need to check for NULL. But
  1084. * since revoke_iomem can be called before the initcall we still need
  1085. * the barrier to appease checkers.
  1086. */
  1087. return smp_load_acquire(&iomem_inode)->i_mapping;
  1088. }
  1089. static int __request_region_locked(struct resource *res, struct resource *parent,
  1090. resource_size_t start, resource_size_t n,
  1091. const char *name, int flags)
  1092. {
  1093. DECLARE_WAITQUEUE(wait, current);
  1094. res->name = name;
  1095. res->start = start;
  1096. res->end = start + n - 1;
  1097. for (;;) {
  1098. struct resource *conflict;
  1099. res->flags = resource_type(parent) | resource_ext_type(parent);
  1100. res->flags |= IORESOURCE_BUSY | flags;
  1101. res->desc = parent->desc;
  1102. conflict = __request_resource(parent, res);
  1103. if (!conflict)
  1104. break;
  1105. /*
  1106. * mm/hmm.c reserves physical addresses which then
  1107. * become unavailable to other users. Conflicts are
  1108. * not expected. Warn to aid debugging if encountered.
  1109. */
  1110. if (conflict->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) {
  1111. pr_warn("Unaddressable device %s %pR conflicts with %pR",
  1112. conflict->name, conflict, res);
  1113. }
  1114. if (conflict != parent) {
  1115. if (!(conflict->flags & IORESOURCE_BUSY)) {
  1116. parent = conflict;
  1117. continue;
  1118. }
  1119. }
  1120. if (conflict->flags & flags & IORESOURCE_MUXED) {
  1121. add_wait_queue(&muxed_resource_wait, &wait);
  1122. write_unlock(&resource_lock);
  1123. set_current_state(TASK_UNINTERRUPTIBLE);
  1124. schedule();
  1125. remove_wait_queue(&muxed_resource_wait, &wait);
  1126. write_lock(&resource_lock);
  1127. continue;
  1128. }
  1129. /* Uhhuh, that didn't work out.. */
  1130. return -EBUSY;
  1131. }
  1132. return 0;
  1133. }
  1134. /**
  1135. * __request_region - create a new busy resource region
  1136. * @parent: parent resource descriptor
  1137. * @start: resource start address
  1138. * @n: resource region size
  1139. * @name: reserving caller's ID string
  1140. * @flags: IO resource flags
  1141. */
  1142. struct resource *__request_region(struct resource *parent,
  1143. resource_size_t start, resource_size_t n,
  1144. const char *name, int flags)
  1145. {
  1146. struct resource *res = alloc_resource(GFP_KERNEL);
  1147. int ret;
  1148. if (!res)
  1149. return NULL;
  1150. write_lock(&resource_lock);
  1151. ret = __request_region_locked(res, parent, start, n, name, flags);
  1152. write_unlock(&resource_lock);
  1153. if (ret) {
  1154. free_resource(res);
  1155. return NULL;
  1156. }
  1157. if (parent == &iomem_resource)
  1158. revoke_iomem(res);
  1159. return res;
  1160. }
  1161. EXPORT_SYMBOL(__request_region);
  1162. /**
  1163. * __release_region - release a previously reserved resource region
  1164. * @parent: parent resource descriptor
  1165. * @start: resource start address
  1166. * @n: resource region size
  1167. *
  1168. * The described resource region must match a currently busy region.
  1169. */
  1170. void __release_region(struct resource *parent, resource_size_t start,
  1171. resource_size_t n)
  1172. {
  1173. struct resource **p;
  1174. resource_size_t end;
  1175. p = &parent->child;
  1176. end = start + n - 1;
  1177. write_lock(&resource_lock);
  1178. for (;;) {
  1179. struct resource *res = *p;
  1180. if (!res)
  1181. break;
  1182. if (res->start <= start && res->end >= end) {
  1183. if (!(res->flags & IORESOURCE_BUSY)) {
  1184. p = &res->child;
  1185. continue;
  1186. }
  1187. if (res->start != start || res->end != end)
  1188. break;
  1189. *p = res->sibling;
  1190. write_unlock(&resource_lock);
  1191. if (res->flags & IORESOURCE_MUXED)
  1192. wake_up(&muxed_resource_wait);
  1193. free_resource(res);
  1194. return;
  1195. }
  1196. p = &res->sibling;
  1197. }
  1198. write_unlock(&resource_lock);
  1199. pr_warn("Trying to free nonexistent resource <%pa-%pa>\n", &start, &end);
  1200. }
  1201. EXPORT_SYMBOL(__release_region);
  1202. #ifdef CONFIG_MEMORY_HOTREMOVE
  1203. /**
  1204. * release_mem_region_adjustable - release a previously reserved memory region
  1205. * @start: resource start address
  1206. * @size: resource region size
  1207. *
  1208. * This interface is intended for memory hot-delete. The requested region
  1209. * is released from a currently busy memory resource. The requested region
  1210. * must either match exactly or fit into a single busy resource entry. In
  1211. * the latter case, the remaining resource is adjusted accordingly.
  1212. * Existing children of the busy memory resource must be immutable in the
  1213. * request.
  1214. *
  1215. * Note:
  1216. * - Additional release conditions, such as overlapping region, can be
  1217. * supported after they are confirmed as valid cases.
  1218. * - When a busy memory resource gets split into two entries, the code
  1219. * assumes that all children remain in the lower address entry for
  1220. * simplicity. Enhance this logic when necessary.
  1221. */
  1222. void release_mem_region_adjustable(resource_size_t start, resource_size_t size)
  1223. {
  1224. struct resource *parent = &iomem_resource;
  1225. struct resource *new_res = NULL;
  1226. bool alloc_nofail = false;
  1227. struct resource **p;
  1228. struct resource *res;
  1229. resource_size_t end;
  1230. end = start + size - 1;
  1231. if (WARN_ON_ONCE((start < parent->start) || (end > parent->end)))
  1232. return;
  1233. /*
  1234. * We free up quite a lot of memory on memory hotunplug (esp., memap),
  1235. * just before releasing the region. This is highly unlikely to
  1236. * fail - let's play save and make it never fail as the caller cannot
  1237. * perform any error handling (e.g., trying to re-add memory will fail
  1238. * similarly).
  1239. */
  1240. retry:
  1241. new_res = alloc_resource(GFP_KERNEL | (alloc_nofail ? __GFP_NOFAIL : 0));
  1242. p = &parent->child;
  1243. write_lock(&resource_lock);
  1244. while ((res = *p)) {
  1245. if (res->start >= end)
  1246. break;
  1247. /* look for the next resource if it does not fit into */
  1248. if (res->start > start || res->end < end) {
  1249. p = &res->sibling;
  1250. continue;
  1251. }
  1252. if (!(res->flags & IORESOURCE_MEM))
  1253. break;
  1254. if (!(res->flags & IORESOURCE_BUSY)) {
  1255. p = &res->child;
  1256. continue;
  1257. }
  1258. /* found the target resource; let's adjust accordingly */
  1259. if (res->start == start && res->end == end) {
  1260. /* free the whole entry */
  1261. *p = res->sibling;
  1262. free_resource(res);
  1263. } else if (res->start == start && res->end != end) {
  1264. /* adjust the start */
  1265. WARN_ON_ONCE(__adjust_resource(res, end + 1,
  1266. res->end - end));
  1267. } else if (res->start != start && res->end == end) {
  1268. /* adjust the end */
  1269. WARN_ON_ONCE(__adjust_resource(res, res->start,
  1270. start - res->start));
  1271. } else {
  1272. /* split into two entries - we need a new resource */
  1273. if (!new_res) {
  1274. new_res = alloc_resource(GFP_ATOMIC);
  1275. if (!new_res) {
  1276. alloc_nofail = true;
  1277. write_unlock(&resource_lock);
  1278. goto retry;
  1279. }
  1280. }
  1281. new_res->name = res->name;
  1282. new_res->start = end + 1;
  1283. new_res->end = res->end;
  1284. new_res->flags = res->flags;
  1285. new_res->desc = res->desc;
  1286. new_res->parent = res->parent;
  1287. new_res->sibling = res->sibling;
  1288. new_res->child = NULL;
  1289. if (WARN_ON_ONCE(__adjust_resource(res, res->start,
  1290. start - res->start)))
  1291. break;
  1292. res->sibling = new_res;
  1293. new_res = NULL;
  1294. }
  1295. break;
  1296. }
  1297. write_unlock(&resource_lock);
  1298. free_resource(new_res);
  1299. }
  1300. #endif /* CONFIG_MEMORY_HOTREMOVE */
  1301. #ifdef CONFIG_MEMORY_HOTPLUG
  1302. static bool system_ram_resources_mergeable(struct resource *r1,
  1303. struct resource *r2)
  1304. {
  1305. /* We assume either r1 or r2 is IORESOURCE_SYSRAM_MERGEABLE. */
  1306. return r1->flags == r2->flags && r1->end + 1 == r2->start &&
  1307. r1->name == r2->name && r1->desc == r2->desc &&
  1308. !r1->child && !r2->child;
  1309. }
  1310. /**
  1311. * merge_system_ram_resource - mark the System RAM resource mergeable and try to
  1312. * merge it with adjacent, mergeable resources
  1313. * @res: resource descriptor
  1314. *
  1315. * This interface is intended for memory hotplug, whereby lots of contiguous
  1316. * system ram resources are added (e.g., via add_memory*()) by a driver, and
  1317. * the actual resource boundaries are not of interest (e.g., it might be
  1318. * relevant for DIMMs). Only resources that are marked mergeable, that have the
  1319. * same parent, and that don't have any children are considered. All mergeable
  1320. * resources must be immutable during the request.
  1321. *
  1322. * Note:
  1323. * - The caller has to make sure that no pointers to resources that are
  1324. * marked mergeable are used anymore after this call - the resource might
  1325. * be freed and the pointer might be stale!
  1326. * - release_mem_region_adjustable() will split on demand on memory hotunplug
  1327. */
  1328. void merge_system_ram_resource(struct resource *res)
  1329. {
  1330. const unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  1331. struct resource *cur;
  1332. if (WARN_ON_ONCE((res->flags & flags) != flags))
  1333. return;
  1334. write_lock(&resource_lock);
  1335. res->flags |= IORESOURCE_SYSRAM_MERGEABLE;
  1336. /* Try to merge with next item in the list. */
  1337. cur = res->sibling;
  1338. if (cur && system_ram_resources_mergeable(res, cur)) {
  1339. res->end = cur->end;
  1340. res->sibling = cur->sibling;
  1341. free_resource(cur);
  1342. }
  1343. /* Try to merge with previous item in the list. */
  1344. cur = res->parent->child;
  1345. while (cur && cur->sibling != res)
  1346. cur = cur->sibling;
  1347. if (cur && system_ram_resources_mergeable(cur, res)) {
  1348. cur->end = res->end;
  1349. cur->sibling = res->sibling;
  1350. free_resource(res);
  1351. }
  1352. write_unlock(&resource_lock);
  1353. }
  1354. #endif /* CONFIG_MEMORY_HOTPLUG */
  1355. /*
  1356. * Managed region resource
  1357. */
  1358. static void devm_resource_release(struct device *dev, void *ptr)
  1359. {
  1360. struct resource **r = ptr;
  1361. release_resource(*r);
  1362. }
  1363. /**
  1364. * devm_request_resource() - request and reserve an I/O or memory resource
  1365. * @dev: device for which to request the resource
  1366. * @root: root of the resource tree from which to request the resource
  1367. * @new: descriptor of the resource to request
  1368. *
  1369. * This is a device-managed version of request_resource(). There is usually
  1370. * no need to release resources requested by this function explicitly since
  1371. * that will be taken care of when the device is unbound from its driver.
  1372. * If for some reason the resource needs to be released explicitly, because
  1373. * of ordering issues for example, drivers must call devm_release_resource()
  1374. * rather than the regular release_resource().
  1375. *
  1376. * When a conflict is detected between any existing resources and the newly
  1377. * requested resource, an error message will be printed.
  1378. *
  1379. * Returns 0 on success or a negative error code on failure.
  1380. */
  1381. int devm_request_resource(struct device *dev, struct resource *root,
  1382. struct resource *new)
  1383. {
  1384. struct resource *conflict, **ptr;
  1385. ptr = devres_alloc(devm_resource_release, sizeof(*ptr), GFP_KERNEL);
  1386. if (!ptr)
  1387. return -ENOMEM;
  1388. *ptr = new;
  1389. conflict = request_resource_conflict(root, new);
  1390. if (conflict) {
  1391. dev_err(dev, "resource collision: %pR conflicts with %s %pR\n",
  1392. new, conflict->name, conflict);
  1393. devres_free(ptr);
  1394. return -EBUSY;
  1395. }
  1396. devres_add(dev, ptr);
  1397. return 0;
  1398. }
  1399. EXPORT_SYMBOL(devm_request_resource);
  1400. static int devm_resource_match(struct device *dev, void *res, void *data)
  1401. {
  1402. struct resource **ptr = res;
  1403. return *ptr == data;
  1404. }
  1405. /**
  1406. * devm_release_resource() - release a previously requested resource
  1407. * @dev: device for which to release the resource
  1408. * @new: descriptor of the resource to release
  1409. *
  1410. * Releases a resource previously requested using devm_request_resource().
  1411. */
  1412. void devm_release_resource(struct device *dev, struct resource *new)
  1413. {
  1414. WARN_ON(devres_release(dev, devm_resource_release, devm_resource_match,
  1415. new));
  1416. }
  1417. EXPORT_SYMBOL(devm_release_resource);
  1418. struct region_devres {
  1419. struct resource *parent;
  1420. resource_size_t start;
  1421. resource_size_t n;
  1422. };
  1423. static void devm_region_release(struct device *dev, void *res)
  1424. {
  1425. struct region_devres *this = res;
  1426. __release_region(this->parent, this->start, this->n);
  1427. }
  1428. static int devm_region_match(struct device *dev, void *res, void *match_data)
  1429. {
  1430. struct region_devres *this = res, *match = match_data;
  1431. return this->parent == match->parent &&
  1432. this->start == match->start && this->n == match->n;
  1433. }
  1434. struct resource *
  1435. __devm_request_region(struct device *dev, struct resource *parent,
  1436. resource_size_t start, resource_size_t n, const char *name)
  1437. {
  1438. struct region_devres *dr = NULL;
  1439. struct resource *res;
  1440. dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
  1441. GFP_KERNEL);
  1442. if (!dr)
  1443. return NULL;
  1444. dr->parent = parent;
  1445. dr->start = start;
  1446. dr->n = n;
  1447. res = __request_region(parent, start, n, name, 0);
  1448. if (res)
  1449. devres_add(dev, dr);
  1450. else
  1451. devres_free(dr);
  1452. return res;
  1453. }
  1454. EXPORT_SYMBOL(__devm_request_region);
  1455. void __devm_release_region(struct device *dev, struct resource *parent,
  1456. resource_size_t start, resource_size_t n)
  1457. {
  1458. struct region_devres match_data = { parent, start, n };
  1459. __release_region(parent, start, n);
  1460. WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
  1461. &match_data));
  1462. }
  1463. EXPORT_SYMBOL(__devm_release_region);
  1464. /*
  1465. * Reserve I/O ports or memory based on "reserve=" kernel parameter.
  1466. */
  1467. #define MAXRESERVE 4
  1468. static int __init reserve_setup(char *str)
  1469. {
  1470. static int reserved;
  1471. static struct resource reserve[MAXRESERVE];
  1472. for (;;) {
  1473. unsigned int io_start, io_num;
  1474. int x = reserved;
  1475. struct resource *parent;
  1476. if (get_option(&str, &io_start) != 2)
  1477. break;
  1478. if (get_option(&str, &io_num) == 0)
  1479. break;
  1480. if (x < MAXRESERVE) {
  1481. struct resource *res = reserve + x;
  1482. /*
  1483. * If the region starts below 0x10000, we assume it's
  1484. * I/O port space; otherwise assume it's memory.
  1485. */
  1486. if (io_start < 0x10000) {
  1487. res->flags = IORESOURCE_IO;
  1488. parent = &ioport_resource;
  1489. } else {
  1490. res->flags = IORESOURCE_MEM;
  1491. parent = &iomem_resource;
  1492. }
  1493. res->name = "reserved";
  1494. res->start = io_start;
  1495. res->end = io_start + io_num - 1;
  1496. res->flags |= IORESOURCE_BUSY;
  1497. res->desc = IORES_DESC_NONE;
  1498. res->child = NULL;
  1499. if (request_resource(parent, res) == 0)
  1500. reserved = x+1;
  1501. }
  1502. }
  1503. return 1;
  1504. }
  1505. __setup("reserve=", reserve_setup);
  1506. /*
  1507. * Check if the requested addr and size spans more than any slot in the
  1508. * iomem resource tree.
  1509. */
  1510. int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
  1511. {
  1512. resource_size_t end = addr + size - 1;
  1513. struct resource *p;
  1514. int err = 0;
  1515. read_lock(&resource_lock);
  1516. for_each_resource(&iomem_resource, p, false) {
  1517. /*
  1518. * We can probably skip the resources without
  1519. * IORESOURCE_IO attribute?
  1520. */
  1521. if (p->start > end)
  1522. continue;
  1523. if (p->end < addr)
  1524. continue;
  1525. if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
  1526. PFN_DOWN(p->end) >= PFN_DOWN(end))
  1527. continue;
  1528. /*
  1529. * if a resource is "BUSY", it's not a hardware resource
  1530. * but a driver mapping of such a resource; we don't want
  1531. * to warn for those; some drivers legitimately map only
  1532. * partial hardware resources. (example: vesafb)
  1533. */
  1534. if (p->flags & IORESOURCE_BUSY)
  1535. continue;
  1536. pr_warn("resource sanity check: requesting [mem %pa-%pa], which spans more than %s %pR\n",
  1537. &addr, &end, p->name, p);
  1538. err = -1;
  1539. break;
  1540. }
  1541. read_unlock(&resource_lock);
  1542. return err;
  1543. }
  1544. #ifdef CONFIG_STRICT_DEVMEM
  1545. static int strict_iomem_checks = 1;
  1546. #else
  1547. static int strict_iomem_checks;
  1548. #endif
  1549. /*
  1550. * Check if an address is exclusive to the kernel and must not be mapped to
  1551. * user space, for example, via /dev/mem.
  1552. *
  1553. * Returns true if exclusive to the kernel, otherwise returns false.
  1554. */
  1555. bool resource_is_exclusive(struct resource *root, u64 addr, resource_size_t size)
  1556. {
  1557. const unsigned int exclusive_system_ram = IORESOURCE_SYSTEM_RAM |
  1558. IORESOURCE_EXCLUSIVE;
  1559. bool skip_children = false, err = false;
  1560. struct resource *p;
  1561. read_lock(&resource_lock);
  1562. for_each_resource(root, p, skip_children) {
  1563. if (p->start >= addr + size)
  1564. break;
  1565. if (p->end < addr) {
  1566. skip_children = true;
  1567. continue;
  1568. }
  1569. skip_children = false;
  1570. /*
  1571. * IORESOURCE_SYSTEM_RAM resources are exclusive if
  1572. * IORESOURCE_EXCLUSIVE is set, even if they
  1573. * are not busy and even if "iomem=relaxed" is set. The
  1574. * responsible driver dynamically adds/removes system RAM within
  1575. * such an area and uncontrolled access is dangerous.
  1576. */
  1577. if ((p->flags & exclusive_system_ram) == exclusive_system_ram) {
  1578. err = true;
  1579. break;
  1580. }
  1581. /*
  1582. * A resource is exclusive if IORESOURCE_EXCLUSIVE is set
  1583. * or CONFIG_IO_STRICT_DEVMEM is enabled and the
  1584. * resource is busy.
  1585. */
  1586. if (!strict_iomem_checks || !(p->flags & IORESOURCE_BUSY))
  1587. continue;
  1588. if (IS_ENABLED(CONFIG_IO_STRICT_DEVMEM)
  1589. || p->flags & IORESOURCE_EXCLUSIVE) {
  1590. err = true;
  1591. break;
  1592. }
  1593. }
  1594. read_unlock(&resource_lock);
  1595. return err;
  1596. }
  1597. bool iomem_is_exclusive(u64 addr)
  1598. {
  1599. return resource_is_exclusive(&iomem_resource, addr & PAGE_MASK,
  1600. PAGE_SIZE);
  1601. }
  1602. struct resource_entry *resource_list_create_entry(struct resource *res,
  1603. size_t extra_size)
  1604. {
  1605. struct resource_entry *entry;
  1606. entry = kzalloc(sizeof(*entry) + extra_size, GFP_KERNEL);
  1607. if (entry) {
  1608. INIT_LIST_HEAD(&entry->node);
  1609. entry->res = res ? res : &entry->__res;
  1610. }
  1611. return entry;
  1612. }
  1613. EXPORT_SYMBOL(resource_list_create_entry);
  1614. void resource_list_free(struct list_head *head)
  1615. {
  1616. struct resource_entry *entry, *tmp;
  1617. list_for_each_entry_safe(entry, tmp, head, node)
  1618. resource_list_destroy_entry(entry);
  1619. }
  1620. EXPORT_SYMBOL(resource_list_free);
  1621. #ifdef CONFIG_GET_FREE_REGION
  1622. #define GFR_DESCENDING (1UL << 0)
  1623. #define GFR_REQUEST_REGION (1UL << 1)
  1624. #ifdef PA_SECTION_SHIFT
  1625. #define GFR_DEFAULT_ALIGN (1UL << PA_SECTION_SHIFT)
  1626. #else
  1627. #define GFR_DEFAULT_ALIGN PAGE_SIZE
  1628. #endif
  1629. static resource_size_t gfr_start(struct resource *base, resource_size_t size,
  1630. resource_size_t align, unsigned long flags)
  1631. {
  1632. if (flags & GFR_DESCENDING) {
  1633. resource_size_t end;
  1634. end = min_t(resource_size_t, base->end, PHYSMEM_END);
  1635. return end - size + 1;
  1636. }
  1637. return ALIGN(max(base->start, align), align);
  1638. }
  1639. static bool gfr_continue(struct resource *base, resource_size_t addr,
  1640. resource_size_t size, unsigned long flags)
  1641. {
  1642. if (flags & GFR_DESCENDING)
  1643. return addr > size && addr >= base->start;
  1644. /*
  1645. * In the ascend case be careful that the last increment by
  1646. * @size did not wrap 0.
  1647. */
  1648. return addr > addr - size &&
  1649. addr <= min_t(resource_size_t, base->end, PHYSMEM_END);
  1650. }
  1651. static resource_size_t gfr_next(resource_size_t addr, resource_size_t size,
  1652. unsigned long flags)
  1653. {
  1654. if (flags & GFR_DESCENDING)
  1655. return addr - size;
  1656. return addr + size;
  1657. }
  1658. static void remove_free_mem_region(void *_res)
  1659. {
  1660. struct resource *res = _res;
  1661. if (res->parent)
  1662. remove_resource(res);
  1663. free_resource(res);
  1664. }
  1665. static struct resource *
  1666. get_free_mem_region(struct device *dev, struct resource *base,
  1667. resource_size_t size, const unsigned long align,
  1668. const char *name, const unsigned long desc,
  1669. const unsigned long flags)
  1670. {
  1671. resource_size_t addr;
  1672. struct resource *res;
  1673. struct region_devres *dr = NULL;
  1674. size = ALIGN(size, align);
  1675. res = alloc_resource(GFP_KERNEL);
  1676. if (!res)
  1677. return ERR_PTR(-ENOMEM);
  1678. if (dev && (flags & GFR_REQUEST_REGION)) {
  1679. dr = devres_alloc(devm_region_release,
  1680. sizeof(struct region_devres), GFP_KERNEL);
  1681. if (!dr) {
  1682. free_resource(res);
  1683. return ERR_PTR(-ENOMEM);
  1684. }
  1685. } else if (dev) {
  1686. if (devm_add_action_or_reset(dev, remove_free_mem_region, res))
  1687. return ERR_PTR(-ENOMEM);
  1688. }
  1689. write_lock(&resource_lock);
  1690. for (addr = gfr_start(base, size, align, flags);
  1691. gfr_continue(base, addr, align, flags);
  1692. addr = gfr_next(addr, align, flags)) {
  1693. if (__region_intersects(base, addr, size, 0, IORES_DESC_NONE) !=
  1694. REGION_DISJOINT)
  1695. continue;
  1696. if (flags & GFR_REQUEST_REGION) {
  1697. if (__request_region_locked(res, &iomem_resource, addr,
  1698. size, name, 0))
  1699. break;
  1700. if (dev) {
  1701. dr->parent = &iomem_resource;
  1702. dr->start = addr;
  1703. dr->n = size;
  1704. devres_add(dev, dr);
  1705. }
  1706. res->desc = desc;
  1707. write_unlock(&resource_lock);
  1708. /*
  1709. * A driver is claiming this region so revoke any
  1710. * mappings.
  1711. */
  1712. revoke_iomem(res);
  1713. } else {
  1714. res->start = addr;
  1715. res->end = addr + size - 1;
  1716. res->name = name;
  1717. res->desc = desc;
  1718. res->flags = IORESOURCE_MEM;
  1719. /*
  1720. * Only succeed if the resource hosts an exclusive
  1721. * range after the insert
  1722. */
  1723. if (__insert_resource(base, res) || res->child)
  1724. break;
  1725. write_unlock(&resource_lock);
  1726. }
  1727. return res;
  1728. }
  1729. write_unlock(&resource_lock);
  1730. if (flags & GFR_REQUEST_REGION) {
  1731. free_resource(res);
  1732. devres_free(dr);
  1733. } else if (dev)
  1734. devm_release_action(dev, remove_free_mem_region, res);
  1735. return ERR_PTR(-ERANGE);
  1736. }
  1737. /**
  1738. * devm_request_free_mem_region - find free region for device private memory
  1739. *
  1740. * @dev: device struct to bind the resource to
  1741. * @size: size in bytes of the device memory to add
  1742. * @base: resource tree to look in
  1743. *
  1744. * This function tries to find an empty range of physical address big enough to
  1745. * contain the new resource, so that it can later be hotplugged as ZONE_DEVICE
  1746. * memory, which in turn allocates struct pages.
  1747. */
  1748. struct resource *devm_request_free_mem_region(struct device *dev,
  1749. struct resource *base, unsigned long size)
  1750. {
  1751. unsigned long flags = GFR_DESCENDING | GFR_REQUEST_REGION;
  1752. return get_free_mem_region(dev, base, size, GFR_DEFAULT_ALIGN,
  1753. dev_name(dev),
  1754. IORES_DESC_DEVICE_PRIVATE_MEMORY, flags);
  1755. }
  1756. EXPORT_SYMBOL_GPL(devm_request_free_mem_region);
  1757. struct resource *request_free_mem_region(struct resource *base,
  1758. unsigned long size, const char *name)
  1759. {
  1760. unsigned long flags = GFR_DESCENDING | GFR_REQUEST_REGION;
  1761. return get_free_mem_region(NULL, base, size, GFR_DEFAULT_ALIGN, name,
  1762. IORES_DESC_DEVICE_PRIVATE_MEMORY, flags);
  1763. }
  1764. EXPORT_SYMBOL_GPL(request_free_mem_region);
  1765. /**
  1766. * alloc_free_mem_region - find a free region relative to @base
  1767. * @base: resource that will parent the new resource
  1768. * @size: size in bytes of memory to allocate from @base
  1769. * @align: alignment requirements for the allocation
  1770. * @name: resource name
  1771. *
  1772. * Buses like CXL, that can dynamically instantiate new memory regions,
  1773. * need a method to allocate physical address space for those regions.
  1774. * Allocate and insert a new resource to cover a free, unclaimed by a
  1775. * descendant of @base, range in the span of @base.
  1776. */
  1777. struct resource *alloc_free_mem_region(struct resource *base,
  1778. unsigned long size, unsigned long align,
  1779. const char *name)
  1780. {
  1781. /* Default of ascending direction and insert resource */
  1782. unsigned long flags = 0;
  1783. return get_free_mem_region(NULL, base, size, align, name,
  1784. IORES_DESC_NONE, flags);
  1785. }
  1786. EXPORT_SYMBOL_GPL(alloc_free_mem_region);
  1787. #endif /* CONFIG_GET_FREE_REGION */
  1788. static int __init strict_iomem(char *str)
  1789. {
  1790. if (strstr(str, "relaxed"))
  1791. strict_iomem_checks = 0;
  1792. if (strstr(str, "strict"))
  1793. strict_iomem_checks = 1;
  1794. return 1;
  1795. }
  1796. static int iomem_fs_init_fs_context(struct fs_context *fc)
  1797. {
  1798. return init_pseudo(fc, DEVMEM_MAGIC) ? 0 : -ENOMEM;
  1799. }
  1800. static struct file_system_type iomem_fs_type = {
  1801. .name = "iomem",
  1802. .owner = THIS_MODULE,
  1803. .init_fs_context = iomem_fs_init_fs_context,
  1804. .kill_sb = kill_anon_super,
  1805. };
  1806. static int __init iomem_init_inode(void)
  1807. {
  1808. static struct vfsmount *iomem_vfs_mount;
  1809. static int iomem_fs_cnt;
  1810. struct inode *inode;
  1811. int rc;
  1812. rc = simple_pin_fs(&iomem_fs_type, &iomem_vfs_mount, &iomem_fs_cnt);
  1813. if (rc < 0) {
  1814. pr_err("Cannot mount iomem pseudo filesystem: %d\n", rc);
  1815. return rc;
  1816. }
  1817. inode = alloc_anon_inode(iomem_vfs_mount->mnt_sb);
  1818. if (IS_ERR(inode)) {
  1819. rc = PTR_ERR(inode);
  1820. pr_err("Cannot allocate inode for iomem: %d\n", rc);
  1821. simple_release_fs(&iomem_vfs_mount, &iomem_fs_cnt);
  1822. return rc;
  1823. }
  1824. /*
  1825. * Publish iomem revocation inode initialized.
  1826. * Pairs with smp_load_acquire() in revoke_iomem().
  1827. */
  1828. smp_store_release(&iomem_inode, inode);
  1829. return 0;
  1830. }
  1831. fs_initcall(iomem_init_inode);
  1832. __setup("iomem=", strict_iomem);