amdgpu_bios.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Copyright 2008 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. * Copyright 2009 Jerome Glisse.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors: Dave Airlie
  25. * Alex Deucher
  26. * Jerome Glisse
  27. */
  28. #include <drm/drmP.h>
  29. #include "amdgpu.h"
  30. #include "atom.h"
  31. #include <linux/slab.h>
  32. #include <linux/acpi.h>
  33. /*
  34. * BIOS.
  35. */
  36. /* If you boot an IGP board with a discrete card as the primary,
  37. * the IGP rom is not accessible via the rom bar as the IGP rom is
  38. * part of the system bios. On boot, the system bios puts a
  39. * copy of the igp rom at the start of vram if a discrete card is
  40. * present.
  41. */
  42. static bool igp_read_bios_from_vram(struct amdgpu_device *adev)
  43. {
  44. uint8_t __iomem *bios;
  45. resource_size_t vram_base;
  46. resource_size_t size = 256 * 1024; /* ??? */
  47. if (!(adev->flags & AMD_IS_APU))
  48. if (!amdgpu_card_posted(adev))
  49. return false;
  50. adev->bios = NULL;
  51. vram_base = pci_resource_start(adev->pdev, 0);
  52. bios = ioremap(vram_base, size);
  53. if (!bios) {
  54. return false;
  55. }
  56. if (size == 0 || bios[0] != 0x55 || bios[1] != 0xaa) {
  57. iounmap(bios);
  58. return false;
  59. }
  60. adev->bios = kmalloc(size, GFP_KERNEL);
  61. if (adev->bios == NULL) {
  62. iounmap(bios);
  63. return false;
  64. }
  65. memcpy_fromio(adev->bios, bios, size);
  66. iounmap(bios);
  67. return true;
  68. }
  69. bool amdgpu_read_bios(struct amdgpu_device *adev)
  70. {
  71. uint8_t __iomem *bios, val1, val2;
  72. size_t size;
  73. adev->bios = NULL;
  74. /* XXX: some cards may return 0 for rom size? ddx has a workaround */
  75. bios = pci_map_rom(adev->pdev, &size);
  76. if (!bios) {
  77. return false;
  78. }
  79. val1 = readb(&bios[0]);
  80. val2 = readb(&bios[1]);
  81. if (size == 0 || val1 != 0x55 || val2 != 0xaa) {
  82. pci_unmap_rom(adev->pdev, bios);
  83. return false;
  84. }
  85. adev->bios = kzalloc(size, GFP_KERNEL);
  86. if (adev->bios == NULL) {
  87. pci_unmap_rom(adev->pdev, bios);
  88. return false;
  89. }
  90. memcpy_fromio(adev->bios, bios, size);
  91. pci_unmap_rom(adev->pdev, bios);
  92. return true;
  93. }
  94. static bool amdgpu_read_platform_bios(struct amdgpu_device *adev)
  95. {
  96. uint8_t __iomem *bios;
  97. size_t size;
  98. adev->bios = NULL;
  99. bios = pci_platform_rom(adev->pdev, &size);
  100. if (!bios) {
  101. return false;
  102. }
  103. if (size == 0 || bios[0] != 0x55 || bios[1] != 0xaa) {
  104. return false;
  105. }
  106. adev->bios = kmemdup(bios, size, GFP_KERNEL);
  107. if (adev->bios == NULL) {
  108. return false;
  109. }
  110. return true;
  111. }
  112. #ifdef CONFIG_ACPI
  113. /* ATRM is used to get the BIOS on the discrete cards in
  114. * dual-gpu systems.
  115. */
  116. /* retrieve the ROM in 4k blocks */
  117. #define ATRM_BIOS_PAGE 4096
  118. /**
  119. * amdgpu_atrm_call - fetch a chunk of the vbios
  120. *
  121. * @atrm_handle: acpi ATRM handle
  122. * @bios: vbios image pointer
  123. * @offset: offset of vbios image data to fetch
  124. * @len: length of vbios image data to fetch
  125. *
  126. * Executes ATRM to fetch a chunk of the discrete
  127. * vbios image on PX systems (all asics).
  128. * Returns the length of the buffer fetched.
  129. */
  130. static int amdgpu_atrm_call(acpi_handle atrm_handle, uint8_t *bios,
  131. int offset, int len)
  132. {
  133. acpi_status status;
  134. union acpi_object atrm_arg_elements[2], *obj;
  135. struct acpi_object_list atrm_arg;
  136. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
  137. atrm_arg.count = 2;
  138. atrm_arg.pointer = &atrm_arg_elements[0];
  139. atrm_arg_elements[0].type = ACPI_TYPE_INTEGER;
  140. atrm_arg_elements[0].integer.value = offset;
  141. atrm_arg_elements[1].type = ACPI_TYPE_INTEGER;
  142. atrm_arg_elements[1].integer.value = len;
  143. status = acpi_evaluate_object(atrm_handle, NULL, &atrm_arg, &buffer);
  144. if (ACPI_FAILURE(status)) {
  145. printk("failed to evaluate ATRM got %s\n", acpi_format_exception(status));
  146. return -ENODEV;
  147. }
  148. obj = (union acpi_object *)buffer.pointer;
  149. memcpy(bios+offset, obj->buffer.pointer, obj->buffer.length);
  150. len = obj->buffer.length;
  151. kfree(buffer.pointer);
  152. return len;
  153. }
  154. static bool amdgpu_atrm_get_bios(struct amdgpu_device *adev)
  155. {
  156. int ret;
  157. int size = 256 * 1024;
  158. int i;
  159. struct pci_dev *pdev = NULL;
  160. acpi_handle dhandle, atrm_handle;
  161. acpi_status status;
  162. bool found = false;
  163. /* ATRM is for the discrete card only */
  164. if (adev->flags & AMD_IS_APU)
  165. return false;
  166. while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
  167. dhandle = ACPI_HANDLE(&pdev->dev);
  168. if (!dhandle)
  169. continue;
  170. status = acpi_get_handle(dhandle, "ATRM", &atrm_handle);
  171. if (!ACPI_FAILURE(status)) {
  172. found = true;
  173. break;
  174. }
  175. }
  176. if (!found) {
  177. while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) {
  178. dhandle = ACPI_HANDLE(&pdev->dev);
  179. if (!dhandle)
  180. continue;
  181. status = acpi_get_handle(dhandle, "ATRM", &atrm_handle);
  182. if (!ACPI_FAILURE(status)) {
  183. found = true;
  184. break;
  185. }
  186. }
  187. }
  188. if (!found)
  189. return false;
  190. adev->bios = kmalloc(size, GFP_KERNEL);
  191. if (!adev->bios) {
  192. DRM_ERROR("Unable to allocate bios\n");
  193. return false;
  194. }
  195. for (i = 0; i < size / ATRM_BIOS_PAGE; i++) {
  196. ret = amdgpu_atrm_call(atrm_handle,
  197. adev->bios,
  198. (i * ATRM_BIOS_PAGE),
  199. ATRM_BIOS_PAGE);
  200. if (ret < ATRM_BIOS_PAGE)
  201. break;
  202. }
  203. if (i == 0 || adev->bios[0] != 0x55 || adev->bios[1] != 0xaa) {
  204. kfree(adev->bios);
  205. return false;
  206. }
  207. return true;
  208. }
  209. #else
  210. static inline bool amdgpu_atrm_get_bios(struct amdgpu_device *adev)
  211. {
  212. return false;
  213. }
  214. #endif
  215. static bool amdgpu_read_disabled_bios(struct amdgpu_device *adev)
  216. {
  217. if (adev->flags & AMD_IS_APU)
  218. return igp_read_bios_from_vram(adev);
  219. else
  220. return amdgpu_asic_read_disabled_bios(adev);
  221. }
  222. #ifdef CONFIG_ACPI
  223. static bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev)
  224. {
  225. bool ret = false;
  226. struct acpi_table_header *hdr;
  227. acpi_size tbl_size;
  228. UEFI_ACPI_VFCT *vfct;
  229. GOP_VBIOS_CONTENT *vbios;
  230. VFCT_IMAGE_HEADER *vhdr;
  231. if (!ACPI_SUCCESS(acpi_get_table_with_size("VFCT", 1, &hdr, &tbl_size)))
  232. return false;
  233. if (tbl_size < sizeof(UEFI_ACPI_VFCT)) {
  234. DRM_ERROR("ACPI VFCT table present but broken (too short #1)\n");
  235. goto out_unmap;
  236. }
  237. vfct = (UEFI_ACPI_VFCT *)hdr;
  238. if (vfct->VBIOSImageOffset + sizeof(VFCT_IMAGE_HEADER) > tbl_size) {
  239. DRM_ERROR("ACPI VFCT table present but broken (too short #2)\n");
  240. goto out_unmap;
  241. }
  242. vbios = (GOP_VBIOS_CONTENT *)((char *)hdr + vfct->VBIOSImageOffset);
  243. vhdr = &vbios->VbiosHeader;
  244. DRM_INFO("ACPI VFCT contains a BIOS for %02x:%02x.%d %04x:%04x, size %d\n",
  245. vhdr->PCIBus, vhdr->PCIDevice, vhdr->PCIFunction,
  246. vhdr->VendorID, vhdr->DeviceID, vhdr->ImageLength);
  247. if (vhdr->PCIBus != adev->pdev->bus->number ||
  248. vhdr->PCIDevice != PCI_SLOT(adev->pdev->devfn) ||
  249. vhdr->PCIFunction != PCI_FUNC(adev->pdev->devfn) ||
  250. vhdr->VendorID != adev->pdev->vendor ||
  251. vhdr->DeviceID != adev->pdev->device) {
  252. DRM_INFO("ACPI VFCT table is not for this card\n");
  253. goto out_unmap;
  254. }
  255. if (vfct->VBIOSImageOffset + sizeof(VFCT_IMAGE_HEADER) + vhdr->ImageLength > tbl_size) {
  256. DRM_ERROR("ACPI VFCT image truncated\n");
  257. goto out_unmap;
  258. }
  259. adev->bios = kmemdup(&vbios->VbiosContent, vhdr->ImageLength, GFP_KERNEL);
  260. ret = !!adev->bios;
  261. out_unmap:
  262. return ret;
  263. }
  264. #else
  265. static inline bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev)
  266. {
  267. return false;
  268. }
  269. #endif
  270. bool amdgpu_get_bios(struct amdgpu_device *adev)
  271. {
  272. bool r;
  273. uint16_t tmp;
  274. r = amdgpu_atrm_get_bios(adev);
  275. if (r == false)
  276. r = amdgpu_acpi_vfct_bios(adev);
  277. if (r == false)
  278. r = igp_read_bios_from_vram(adev);
  279. if (r == false)
  280. r = amdgpu_read_bios(adev);
  281. if (r == false) {
  282. r = amdgpu_read_disabled_bios(adev);
  283. }
  284. if (r == false) {
  285. r = amdgpu_read_platform_bios(adev);
  286. }
  287. if (r == false || adev->bios == NULL) {
  288. DRM_ERROR("Unable to locate a BIOS ROM\n");
  289. adev->bios = NULL;
  290. return false;
  291. }
  292. if (adev->bios[0] != 0x55 || adev->bios[1] != 0xaa) {
  293. printk("BIOS signature incorrect %x %x\n", adev->bios[0], adev->bios[1]);
  294. goto free_bios;
  295. }
  296. tmp = RBIOS16(0x18);
  297. if (RBIOS8(tmp + 0x14) != 0x0) {
  298. DRM_INFO("Not an x86 BIOS ROM, not using.\n");
  299. goto free_bios;
  300. }
  301. adev->bios_header_start = RBIOS16(0x48);
  302. if (!adev->bios_header_start) {
  303. goto free_bios;
  304. }
  305. tmp = adev->bios_header_start + 4;
  306. if (!memcmp(adev->bios + tmp, "ATOM", 4) ||
  307. !memcmp(adev->bios + tmp, "MOTA", 4)) {
  308. adev->is_atom_bios = true;
  309. } else {
  310. adev->is_atom_bios = false;
  311. }
  312. DRM_DEBUG("%sBIOS detected\n", adev->is_atom_bios ? "ATOM" : "COM");
  313. return true;
  314. free_bios:
  315. kfree(adev->bios);
  316. adev->bios = NULL;
  317. return false;
  318. }