devres.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/device.h>
  3. #include <linux/pci.h>
  4. #include "pci.h"
  5. /*
  6. * On the state of PCI's devres implementation:
  7. *
  8. * The older PCI devres API has one significant problem:
  9. *
  10. * It is very strongly tied to the statically allocated mapping table in struct
  11. * pcim_iomap_devres below. This is mostly solved in the sense of the pcim_
  12. * functions in this file providing things like ranged mapping by bypassing
  13. * this table, whereas the functions that were present in the old API still
  14. * enter the mapping addresses into the table for users of the old API.
  15. *
  16. * TODO:
  17. * Remove the legacy table entirely once all calls to pcim_iomap_table() in
  18. * the kernel have been removed.
  19. */
  20. /*
  21. * Legacy struct storing addresses to whole mapped BARs.
  22. */
  23. struct pcim_iomap_devres {
  24. void __iomem *table[PCI_NUM_RESOURCES];
  25. };
  26. /* Used to restore the old INTx state on driver detach. */
  27. struct pcim_intx_devres {
  28. int orig_intx;
  29. };
  30. enum pcim_addr_devres_type {
  31. /* Default initializer. */
  32. PCIM_ADDR_DEVRES_TYPE_INVALID,
  33. /* A requested region spanning an entire BAR. */
  34. PCIM_ADDR_DEVRES_TYPE_REGION,
  35. /*
  36. * A requested region spanning an entire BAR, and a mapping for
  37. * the entire BAR.
  38. */
  39. PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING,
  40. /*
  41. * A mapping within a BAR, either spanning the whole BAR or just a
  42. * range. Without a requested region.
  43. */
  44. PCIM_ADDR_DEVRES_TYPE_MAPPING,
  45. };
  46. /*
  47. * This struct envelops IO or MEM addresses, i.e., mappings and region
  48. * requests, because those are very frequently requested and released
  49. * together.
  50. */
  51. struct pcim_addr_devres {
  52. enum pcim_addr_devres_type type;
  53. void __iomem *baseaddr;
  54. unsigned long offset;
  55. unsigned long len;
  56. int bar;
  57. };
  58. static inline void pcim_addr_devres_clear(struct pcim_addr_devres *res)
  59. {
  60. memset(res, 0, sizeof(*res));
  61. res->bar = -1;
  62. }
  63. static void pcim_addr_resource_release(struct device *dev, void *resource_raw)
  64. {
  65. struct pci_dev *pdev = to_pci_dev(dev);
  66. struct pcim_addr_devres *res = resource_raw;
  67. switch (res->type) {
  68. case PCIM_ADDR_DEVRES_TYPE_REGION:
  69. pci_release_region(pdev, res->bar);
  70. break;
  71. case PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING:
  72. pci_iounmap(pdev, res->baseaddr);
  73. pci_release_region(pdev, res->bar);
  74. break;
  75. case PCIM_ADDR_DEVRES_TYPE_MAPPING:
  76. pci_iounmap(pdev, res->baseaddr);
  77. break;
  78. default:
  79. break;
  80. }
  81. }
  82. static struct pcim_addr_devres *pcim_addr_devres_alloc(struct pci_dev *pdev)
  83. {
  84. struct pcim_addr_devres *res;
  85. res = devres_alloc_node(pcim_addr_resource_release, sizeof(*res),
  86. GFP_KERNEL, dev_to_node(&pdev->dev));
  87. if (res)
  88. pcim_addr_devres_clear(res);
  89. return res;
  90. }
  91. /* Just for consistency and readability. */
  92. static inline void pcim_addr_devres_free(struct pcim_addr_devres *res)
  93. {
  94. devres_free(res);
  95. }
  96. /*
  97. * Used by devres to identify a pcim_addr_devres.
  98. */
  99. static int pcim_addr_resources_match(struct device *dev,
  100. void *a_raw, void *b_raw)
  101. {
  102. struct pcim_addr_devres *a, *b;
  103. a = a_raw;
  104. b = b_raw;
  105. if (a->type != b->type)
  106. return 0;
  107. switch (a->type) {
  108. case PCIM_ADDR_DEVRES_TYPE_REGION:
  109. case PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING:
  110. return a->bar == b->bar;
  111. case PCIM_ADDR_DEVRES_TYPE_MAPPING:
  112. return a->baseaddr == b->baseaddr;
  113. default:
  114. return 0;
  115. }
  116. }
  117. static void devm_pci_unmap_iospace(struct device *dev, void *ptr)
  118. {
  119. struct resource **res = ptr;
  120. pci_unmap_iospace(*res);
  121. }
  122. /**
  123. * devm_pci_remap_iospace - Managed pci_remap_iospace()
  124. * @dev: Generic device to remap IO address for
  125. * @res: Resource describing the I/O space
  126. * @phys_addr: physical address of range to be mapped
  127. *
  128. * Managed pci_remap_iospace(). Map is automatically unmapped on driver
  129. * detach.
  130. */
  131. int devm_pci_remap_iospace(struct device *dev, const struct resource *res,
  132. phys_addr_t phys_addr)
  133. {
  134. const struct resource **ptr;
  135. int error;
  136. ptr = devres_alloc(devm_pci_unmap_iospace, sizeof(*ptr), GFP_KERNEL);
  137. if (!ptr)
  138. return -ENOMEM;
  139. error = pci_remap_iospace(res, phys_addr);
  140. if (error) {
  141. devres_free(ptr);
  142. } else {
  143. *ptr = res;
  144. devres_add(dev, ptr);
  145. }
  146. return error;
  147. }
  148. EXPORT_SYMBOL(devm_pci_remap_iospace);
  149. /**
  150. * devm_pci_remap_cfgspace - Managed pci_remap_cfgspace()
  151. * @dev: Generic device to remap IO address for
  152. * @offset: Resource address to map
  153. * @size: Size of map
  154. *
  155. * Managed pci_remap_cfgspace(). Map is automatically unmapped on driver
  156. * detach.
  157. */
  158. void __iomem *devm_pci_remap_cfgspace(struct device *dev,
  159. resource_size_t offset,
  160. resource_size_t size)
  161. {
  162. void __iomem **ptr, *addr;
  163. ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
  164. if (!ptr)
  165. return NULL;
  166. addr = pci_remap_cfgspace(offset, size);
  167. if (addr) {
  168. *ptr = addr;
  169. devres_add(dev, ptr);
  170. } else
  171. devres_free(ptr);
  172. return addr;
  173. }
  174. EXPORT_SYMBOL(devm_pci_remap_cfgspace);
  175. /**
  176. * devm_pci_remap_cfg_resource - check, request region and ioremap cfg resource
  177. * @dev: generic device to handle the resource for
  178. * @res: configuration space resource to be handled
  179. *
  180. * Checks that a resource is a valid memory region, requests the memory
  181. * region and ioremaps with pci_remap_cfgspace() API that ensures the
  182. * proper PCI configuration space memory attributes are guaranteed.
  183. *
  184. * All operations are managed and will be undone on driver detach.
  185. *
  186. * Returns a pointer to the remapped memory or an IOMEM_ERR_PTR() encoded error
  187. * code on failure. Usage example::
  188. *
  189. * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  190. * base = devm_pci_remap_cfg_resource(&pdev->dev, res);
  191. * if (IS_ERR(base))
  192. * return PTR_ERR(base);
  193. */
  194. void __iomem *devm_pci_remap_cfg_resource(struct device *dev,
  195. struct resource *res)
  196. {
  197. resource_size_t size;
  198. const char *name;
  199. void __iomem *dest_ptr;
  200. BUG_ON(!dev);
  201. if (!res || resource_type(res) != IORESOURCE_MEM) {
  202. dev_err(dev, "invalid resource\n");
  203. return IOMEM_ERR_PTR(-EINVAL);
  204. }
  205. size = resource_size(res);
  206. if (res->name)
  207. name = devm_kasprintf(dev, GFP_KERNEL, "%s %s", dev_name(dev),
  208. res->name);
  209. else
  210. name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
  211. if (!name)
  212. return IOMEM_ERR_PTR(-ENOMEM);
  213. if (!devm_request_mem_region(dev, res->start, size, name)) {
  214. dev_err(dev, "can't request region for resource %pR\n", res);
  215. return IOMEM_ERR_PTR(-EBUSY);
  216. }
  217. dest_ptr = devm_pci_remap_cfgspace(dev, res->start, size);
  218. if (!dest_ptr) {
  219. dev_err(dev, "ioremap failed for resource %pR\n", res);
  220. devm_release_mem_region(dev, res->start, size);
  221. dest_ptr = IOMEM_ERR_PTR(-ENOMEM);
  222. }
  223. return dest_ptr;
  224. }
  225. EXPORT_SYMBOL(devm_pci_remap_cfg_resource);
  226. static void __pcim_clear_mwi(void *pdev_raw)
  227. {
  228. struct pci_dev *pdev = pdev_raw;
  229. pci_clear_mwi(pdev);
  230. }
  231. /**
  232. * pcim_set_mwi - a device-managed pci_set_mwi()
  233. * @pdev: the PCI device for which MWI is enabled
  234. *
  235. * Managed pci_set_mwi().
  236. *
  237. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  238. */
  239. int pcim_set_mwi(struct pci_dev *pdev)
  240. {
  241. int ret;
  242. ret = devm_add_action(&pdev->dev, __pcim_clear_mwi, pdev);
  243. if (ret != 0)
  244. return ret;
  245. ret = pci_set_mwi(pdev);
  246. if (ret != 0)
  247. devm_remove_action(&pdev->dev, __pcim_clear_mwi, pdev);
  248. return ret;
  249. }
  250. EXPORT_SYMBOL(pcim_set_mwi);
  251. static inline bool mask_contains_bar(int mask, int bar)
  252. {
  253. return mask & BIT(bar);
  254. }
  255. static void pcim_intx_restore(struct device *dev, void *data)
  256. {
  257. struct pci_dev *pdev = to_pci_dev(dev);
  258. struct pcim_intx_devres *res = data;
  259. pci_intx(pdev, res->orig_intx);
  260. }
  261. static void save_orig_intx(struct pci_dev *pdev, struct pcim_intx_devres *res)
  262. {
  263. u16 pci_command;
  264. pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
  265. res->orig_intx = !(pci_command & PCI_COMMAND_INTX_DISABLE);
  266. }
  267. /**
  268. * pcim_intx - managed pci_intx()
  269. * @pdev: the PCI device to operate on
  270. * @enable: boolean: whether to enable or disable PCI INTx
  271. *
  272. * Returns: 0 on success, -ENOMEM on error.
  273. *
  274. * Enable/disable PCI INTx for device @pdev.
  275. * Restore the original state on driver detach.
  276. */
  277. int pcim_intx(struct pci_dev *pdev, int enable)
  278. {
  279. struct pcim_intx_devres *res;
  280. struct device *dev = &pdev->dev;
  281. /*
  282. * pcim_intx() must only restore the INTx value that existed before the
  283. * driver was loaded, i.e., before it called pcim_intx() for the
  284. * first time.
  285. */
  286. res = devres_find(dev, pcim_intx_restore, NULL, NULL);
  287. if (!res) {
  288. res = devres_alloc(pcim_intx_restore, sizeof(*res), GFP_KERNEL);
  289. if (!res)
  290. return -ENOMEM;
  291. save_orig_intx(pdev, res);
  292. devres_add(dev, res);
  293. }
  294. pci_intx(pdev, enable);
  295. return 0;
  296. }
  297. EXPORT_SYMBOL_GPL(pcim_intx);
  298. static void pcim_disable_device(void *pdev_raw)
  299. {
  300. struct pci_dev *pdev = pdev_raw;
  301. if (!pdev->pinned)
  302. pci_disable_device(pdev);
  303. pdev->is_managed = false;
  304. }
  305. /**
  306. * pcim_enable_device - Managed pci_enable_device()
  307. * @pdev: PCI device to be initialized
  308. *
  309. * Returns: 0 on success, negative error code on failure.
  310. *
  311. * Managed pci_enable_device(). Device will automatically be disabled on
  312. * driver detach.
  313. */
  314. int pcim_enable_device(struct pci_dev *pdev)
  315. {
  316. int ret;
  317. ret = devm_add_action(&pdev->dev, pcim_disable_device, pdev);
  318. if (ret != 0)
  319. return ret;
  320. /*
  321. * We prefer removing the action in case of an error over
  322. * devm_add_action_or_reset() because the latter could theoretically be
  323. * disturbed by users having pinned the device too soon.
  324. */
  325. ret = pci_enable_device(pdev);
  326. if (ret != 0) {
  327. devm_remove_action(&pdev->dev, pcim_disable_device, pdev);
  328. return ret;
  329. }
  330. pdev->is_managed = true;
  331. return ret;
  332. }
  333. EXPORT_SYMBOL(pcim_enable_device);
  334. /**
  335. * pcim_pin_device - Pin managed PCI device
  336. * @pdev: PCI device to pin
  337. *
  338. * Pin managed PCI device @pdev. Pinned device won't be disabled on driver
  339. * detach. @pdev must have been enabled with pcim_enable_device().
  340. */
  341. void pcim_pin_device(struct pci_dev *pdev)
  342. {
  343. pdev->pinned = true;
  344. }
  345. EXPORT_SYMBOL(pcim_pin_device);
  346. static void pcim_iomap_release(struct device *gendev, void *res)
  347. {
  348. /*
  349. * Do nothing. This is legacy code.
  350. *
  351. * Cleanup of the mappings is now done directly through the callbacks
  352. * registered when creating them.
  353. */
  354. }
  355. /**
  356. * pcim_iomap_table - access iomap allocation table (DEPRECATED)
  357. * @pdev: PCI device to access iomap table for
  358. *
  359. * Returns:
  360. * Const pointer to array of __iomem pointers on success, NULL on failure.
  361. *
  362. * Access iomap allocation table for @dev. If iomap table doesn't
  363. * exist and @pdev is managed, it will be allocated. All iomaps
  364. * recorded in the iomap table are automatically unmapped on driver
  365. * detach.
  366. *
  367. * This function might sleep when the table is first allocated but can
  368. * be safely called without context and guaranteed to succeed once
  369. * allocated.
  370. *
  371. * This function is DEPRECATED. Do not use it in new code. Instead, obtain a
  372. * mapping's address directly from one of the pcim_* mapping functions. For
  373. * example:
  374. * void __iomem \*mappy = pcim_iomap(pdev, bar, length);
  375. */
  376. void __iomem * const *pcim_iomap_table(struct pci_dev *pdev)
  377. {
  378. struct pcim_iomap_devres *dr, *new_dr;
  379. dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
  380. if (dr)
  381. return dr->table;
  382. new_dr = devres_alloc_node(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL,
  383. dev_to_node(&pdev->dev));
  384. if (!new_dr)
  385. return NULL;
  386. dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
  387. return dr->table;
  388. }
  389. EXPORT_SYMBOL(pcim_iomap_table);
  390. /*
  391. * Fill the legacy mapping-table, so that drivers using the old API can
  392. * still get a BAR's mapping address through pcim_iomap_table().
  393. */
  394. static int pcim_add_mapping_to_legacy_table(struct pci_dev *pdev,
  395. void __iomem *mapping, int bar)
  396. {
  397. void __iomem **legacy_iomap_table;
  398. if (!pci_bar_index_is_valid(bar))
  399. return -EINVAL;
  400. legacy_iomap_table = (void __iomem **)pcim_iomap_table(pdev);
  401. if (!legacy_iomap_table)
  402. return -ENOMEM;
  403. legacy_iomap_table[bar] = mapping;
  404. return 0;
  405. }
  406. /*
  407. * Remove a mapping. The table only contains whole-BAR mappings, so this will
  408. * never interfere with ranged mappings.
  409. */
  410. static void pcim_remove_mapping_from_legacy_table(struct pci_dev *pdev,
  411. void __iomem *addr)
  412. {
  413. int bar;
  414. void __iomem **legacy_iomap_table;
  415. legacy_iomap_table = (void __iomem **)pcim_iomap_table(pdev);
  416. if (!legacy_iomap_table)
  417. return;
  418. for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
  419. if (legacy_iomap_table[bar] == addr) {
  420. legacy_iomap_table[bar] = NULL;
  421. return;
  422. }
  423. }
  424. }
  425. /*
  426. * The same as pcim_remove_mapping_from_legacy_table(), but identifies the
  427. * mapping by its BAR index.
  428. */
  429. static void pcim_remove_bar_from_legacy_table(struct pci_dev *pdev, int bar)
  430. {
  431. void __iomem **legacy_iomap_table;
  432. if (!pci_bar_index_is_valid(bar))
  433. return;
  434. legacy_iomap_table = (void __iomem **)pcim_iomap_table(pdev);
  435. if (!legacy_iomap_table)
  436. return;
  437. legacy_iomap_table[bar] = NULL;
  438. }
  439. /**
  440. * pcim_iomap - Managed pcim_iomap()
  441. * @pdev: PCI device to iomap for
  442. * @bar: BAR to iomap
  443. * @maxlen: Maximum length of iomap
  444. *
  445. * Returns: __iomem pointer on success, NULL on failure.
  446. *
  447. * Managed pci_iomap(). Map is automatically unmapped on driver detach. If
  448. * desired, unmap manually only with pcim_iounmap().
  449. *
  450. * This SHOULD only be used once per BAR.
  451. *
  452. * NOTE:
  453. * Contrary to the other pcim_* functions, this function does not return an
  454. * IOMEM_ERR_PTR() on failure, but a simple NULL. This is done for backwards
  455. * compatibility.
  456. */
  457. void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
  458. {
  459. void __iomem *mapping;
  460. struct pcim_addr_devres *res;
  461. if (!pci_bar_index_is_valid(bar))
  462. return NULL;
  463. res = pcim_addr_devres_alloc(pdev);
  464. if (!res)
  465. return NULL;
  466. res->type = PCIM_ADDR_DEVRES_TYPE_MAPPING;
  467. mapping = pci_iomap(pdev, bar, maxlen);
  468. if (!mapping)
  469. goto err_iomap;
  470. res->baseaddr = mapping;
  471. if (pcim_add_mapping_to_legacy_table(pdev, mapping, bar) != 0)
  472. goto err_table;
  473. devres_add(&pdev->dev, res);
  474. return mapping;
  475. err_table:
  476. pci_iounmap(pdev, mapping);
  477. err_iomap:
  478. pcim_addr_devres_free(res);
  479. return NULL;
  480. }
  481. EXPORT_SYMBOL(pcim_iomap);
  482. /**
  483. * pcim_iounmap - Managed pci_iounmap()
  484. * @pdev: PCI device to iounmap for
  485. * @addr: Address to unmap
  486. *
  487. * Managed pci_iounmap(). @addr must have been mapped using a pcim_* mapping
  488. * function.
  489. */
  490. void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
  491. {
  492. struct pcim_addr_devres res_searched;
  493. pcim_addr_devres_clear(&res_searched);
  494. res_searched.type = PCIM_ADDR_DEVRES_TYPE_MAPPING;
  495. res_searched.baseaddr = addr;
  496. if (devres_release(&pdev->dev, pcim_addr_resource_release,
  497. pcim_addr_resources_match, &res_searched) != 0) {
  498. /* Doesn't exist. User passed nonsense. */
  499. return;
  500. }
  501. pcim_remove_mapping_from_legacy_table(pdev, addr);
  502. }
  503. EXPORT_SYMBOL(pcim_iounmap);
  504. /**
  505. * pcim_iomap_region - Request and iomap a PCI BAR
  506. * @pdev: PCI device to map IO resources for
  507. * @bar: Index of a BAR to map
  508. * @name: Name of the driver requesting the resource
  509. *
  510. * Returns: __iomem pointer on success, an IOMEM_ERR_PTR on failure.
  511. *
  512. * Mapping and region will get automatically released on driver detach. If
  513. * desired, release manually only with pcim_iounmap_region().
  514. */
  515. void __iomem *pcim_iomap_region(struct pci_dev *pdev, int bar,
  516. const char *name)
  517. {
  518. int ret;
  519. struct pcim_addr_devres *res;
  520. if (!pci_bar_index_is_valid(bar))
  521. return IOMEM_ERR_PTR(-EINVAL);
  522. res = pcim_addr_devres_alloc(pdev);
  523. if (!res)
  524. return IOMEM_ERR_PTR(-ENOMEM);
  525. res->type = PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING;
  526. res->bar = bar;
  527. ret = pci_request_region(pdev, bar, name);
  528. if (ret != 0)
  529. goto err_region;
  530. res->baseaddr = pci_iomap(pdev, bar, 0);
  531. if (!res->baseaddr) {
  532. ret = -EINVAL;
  533. goto err_iomap;
  534. }
  535. devres_add(&pdev->dev, res);
  536. return res->baseaddr;
  537. err_iomap:
  538. pci_release_region(pdev, bar);
  539. err_region:
  540. pcim_addr_devres_free(res);
  541. return IOMEM_ERR_PTR(ret);
  542. }
  543. EXPORT_SYMBOL(pcim_iomap_region);
  544. /**
  545. * pcim_iounmap_region - Unmap and release a PCI BAR
  546. * @pdev: PCI device to operate on
  547. * @bar: Index of BAR to unmap and release
  548. *
  549. * Unmap a BAR and release its region manually. Only pass BARs that were
  550. * previously mapped by pcim_iomap_region().
  551. */
  552. void pcim_iounmap_region(struct pci_dev *pdev, int bar)
  553. {
  554. struct pcim_addr_devres res_searched;
  555. pcim_addr_devres_clear(&res_searched);
  556. res_searched.type = PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING;
  557. res_searched.bar = bar;
  558. devres_release(&pdev->dev, pcim_addr_resource_release,
  559. pcim_addr_resources_match, &res_searched);
  560. }
  561. EXPORT_SYMBOL(pcim_iounmap_region);
  562. /**
  563. * pcim_iomap_regions - Request and iomap PCI BARs (DEPRECATED)
  564. * @pdev: PCI device to map IO resources for
  565. * @mask: Mask of BARs to request and iomap
  566. * @name: Name of the driver requesting the resources
  567. *
  568. * Returns: 0 on success, negative error code on failure.
  569. *
  570. * Request and iomap regions specified by @mask.
  571. *
  572. * This function is DEPRECATED. Do not use it in new code.
  573. * Use pcim_iomap_region() instead.
  574. */
  575. int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)
  576. {
  577. int ret;
  578. int bar;
  579. void __iomem *mapping;
  580. for (bar = 0; bar < DEVICE_COUNT_RESOURCE; bar++) {
  581. if (!mask_contains_bar(mask, bar))
  582. continue;
  583. mapping = pcim_iomap_region(pdev, bar, name);
  584. if (IS_ERR(mapping)) {
  585. ret = PTR_ERR(mapping);
  586. goto err;
  587. }
  588. ret = pcim_add_mapping_to_legacy_table(pdev, mapping, bar);
  589. if (ret != 0)
  590. goto err;
  591. }
  592. return 0;
  593. err:
  594. while (--bar >= 0) {
  595. pcim_iounmap_region(pdev, bar);
  596. pcim_remove_bar_from_legacy_table(pdev, bar);
  597. }
  598. return ret;
  599. }
  600. EXPORT_SYMBOL(pcim_iomap_regions);
  601. /**
  602. * pcim_request_region - Request a PCI BAR
  603. * @pdev: PCI device to request region for
  604. * @bar: Index of BAR to request
  605. * @name: Name of the driver requesting the resource
  606. *
  607. * Returns: 0 on success, a negative error code on failure.
  608. *
  609. * Request region specified by @bar.
  610. *
  611. * The region will automatically be released on driver detach. If desired,
  612. * release manually only with pcim_release_region().
  613. */
  614. int pcim_request_region(struct pci_dev *pdev, int bar, const char *name)
  615. {
  616. int ret;
  617. struct pcim_addr_devres *res;
  618. if (!pci_bar_index_is_valid(bar))
  619. return -EINVAL;
  620. res = pcim_addr_devres_alloc(pdev);
  621. if (!res)
  622. return -ENOMEM;
  623. res->type = PCIM_ADDR_DEVRES_TYPE_REGION;
  624. res->bar = bar;
  625. ret = pci_request_region(pdev, bar, name);
  626. if (ret != 0) {
  627. pcim_addr_devres_free(res);
  628. return ret;
  629. }
  630. devres_add(&pdev->dev, res);
  631. return 0;
  632. }
  633. EXPORT_SYMBOL(pcim_request_region);
  634. /**
  635. * pcim_release_region - Release a PCI BAR
  636. * @pdev: PCI device to operate on
  637. * @bar: Index of BAR to release
  638. *
  639. * Release a region manually that was previously requested by
  640. * pcim_request_region().
  641. */
  642. static void pcim_release_region(struct pci_dev *pdev, int bar)
  643. {
  644. struct pcim_addr_devres res_searched;
  645. pcim_addr_devres_clear(&res_searched);
  646. res_searched.type = PCIM_ADDR_DEVRES_TYPE_REGION;
  647. res_searched.bar = bar;
  648. devres_release(&pdev->dev, pcim_addr_resource_release,
  649. pcim_addr_resources_match, &res_searched);
  650. }
  651. /**
  652. * pcim_release_all_regions - Release all regions of a PCI-device
  653. * @pdev: the PCI device
  654. *
  655. * Release all regions previously requested through pcim_request_region()
  656. * or pcim_request_all_regions().
  657. *
  658. * Can be called from any context, i.e., not necessarily as a counterpart to
  659. * pcim_request_all_regions().
  660. */
  661. static void pcim_release_all_regions(struct pci_dev *pdev)
  662. {
  663. int bar;
  664. for (bar = 0; bar < PCI_STD_NUM_BARS; bar++)
  665. pcim_release_region(pdev, bar);
  666. }
  667. /**
  668. * pcim_request_all_regions - Request all regions
  669. * @pdev: PCI device to map IO resources for
  670. * @name: name of the driver requesting the resources
  671. *
  672. * Returns: 0 on success, negative error code on failure.
  673. *
  674. * Requested regions will automatically be released at driver detach. If
  675. * desired, release individual regions with pcim_release_region() or all of
  676. * them at once with pcim_release_all_regions().
  677. */
  678. int pcim_request_all_regions(struct pci_dev *pdev, const char *name)
  679. {
  680. int ret;
  681. int bar;
  682. for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
  683. ret = pcim_request_region(pdev, bar, name);
  684. if (ret != 0)
  685. goto err;
  686. }
  687. return 0;
  688. err:
  689. pcim_release_all_regions(pdev);
  690. return ret;
  691. }
  692. EXPORT_SYMBOL(pcim_request_all_regions);
  693. /**
  694. * pcim_iomap_range - Create a ranged __iomap mapping within a PCI BAR
  695. * @pdev: PCI device to map IO resources for
  696. * @bar: Index of the BAR
  697. * @offset: Offset from the begin of the BAR
  698. * @len: Length in bytes for the mapping
  699. *
  700. * Returns: __iomem pointer on success, an IOMEM_ERR_PTR on failure.
  701. *
  702. * Creates a new IO-Mapping within the specified @bar, ranging from @offset to
  703. * @offset + @len.
  704. *
  705. * The mapping will automatically get unmapped on driver detach. If desired,
  706. * release manually only with pcim_iounmap().
  707. */
  708. void __iomem *pcim_iomap_range(struct pci_dev *pdev, int bar,
  709. unsigned long offset, unsigned long len)
  710. {
  711. void __iomem *mapping;
  712. struct pcim_addr_devres *res;
  713. if (!pci_bar_index_is_valid(bar))
  714. return IOMEM_ERR_PTR(-EINVAL);
  715. res = pcim_addr_devres_alloc(pdev);
  716. if (!res)
  717. return IOMEM_ERR_PTR(-ENOMEM);
  718. mapping = pci_iomap_range(pdev, bar, offset, len);
  719. if (!mapping) {
  720. pcim_addr_devres_free(res);
  721. return IOMEM_ERR_PTR(-EINVAL);
  722. }
  723. res->type = PCIM_ADDR_DEVRES_TYPE_MAPPING;
  724. res->baseaddr = mapping;
  725. /*
  726. * Ranged mappings don't get added to the legacy-table, since the table
  727. * only ever keeps track of whole BARs.
  728. */
  729. devres_add(&pdev->dev, res);
  730. return mapping;
  731. }
  732. EXPORT_SYMBOL(pcim_iomap_range);