shadowpci.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright 2012 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. */
  23. #include "priv.h"
  24. #include <core/pci.h>
  25. struct priv {
  26. struct pci_dev *pdev;
  27. void __iomem *rom;
  28. size_t size;
  29. };
  30. static u32
  31. pcirom_read(void *data, u32 offset, u32 length, struct nvkm_bios *bios)
  32. {
  33. struct priv *priv = data;
  34. if (offset + length <= priv->size) {
  35. memcpy_fromio(bios->data + offset, priv->rom + offset, length);
  36. return length;
  37. }
  38. return 0;
  39. }
  40. static void
  41. pcirom_fini(void *data)
  42. {
  43. struct priv *priv = data;
  44. pci_unmap_rom(priv->pdev, priv->rom);
  45. pci_disable_rom(priv->pdev);
  46. kfree(priv);
  47. }
  48. static void *
  49. pcirom_init(struct nvkm_bios *bios, const char *name)
  50. {
  51. struct nvkm_device *device = bios->subdev.device;
  52. struct priv *priv = NULL;
  53. struct pci_dev *pdev;
  54. int ret;
  55. if (device->func->pci)
  56. pdev = device->func->pci(device)->pdev;
  57. else
  58. return ERR_PTR(-ENODEV);
  59. if (!(ret = pci_enable_rom(pdev))) {
  60. if (ret = -ENOMEM,
  61. (priv = kmalloc(sizeof(*priv), GFP_KERNEL))) {
  62. if (ret = -EFAULT,
  63. (priv->rom = pci_map_rom(pdev, &priv->size))) {
  64. priv->pdev = pdev;
  65. return priv;
  66. }
  67. kfree(priv);
  68. }
  69. pci_disable_rom(pdev);
  70. }
  71. return ERR_PTR(ret);
  72. }
  73. const struct nvbios_source
  74. nvbios_pcirom = {
  75. .name = "PCIROM",
  76. .init = pcirom_init,
  77. .fini = pcirom_fini,
  78. .read = pcirom_read,
  79. .rw = true,
  80. };
  81. static void *
  82. platform_init(struct nvkm_bios *bios, const char *name)
  83. {
  84. struct nvkm_device *device = bios->subdev.device;
  85. struct pci_dev *pdev;
  86. struct priv *priv;
  87. int ret = -ENOMEM;
  88. if (device->func->pci)
  89. pdev = device->func->pci(device)->pdev;
  90. else
  91. return ERR_PTR(-ENODEV);
  92. if ((priv = kmalloc(sizeof(*priv), GFP_KERNEL))) {
  93. if (ret = -ENODEV,
  94. (priv->rom = pci_platform_rom(pdev, &priv->size)))
  95. return priv;
  96. kfree(priv);
  97. }
  98. return ERR_PTR(ret);
  99. }
  100. const struct nvbios_source
  101. nvbios_platform = {
  102. .name = "PLATFORM",
  103. .init = platform_init,
  104. .fini = (void(*)(void *))kfree,
  105. .read = pcirom_read,
  106. .rw = true,
  107. };