amdgpu_bios.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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/vga_switcheroo.h>
  32. #include <linux/slab.h>
  33. #include <linux/acpi.h>
  34. /*
  35. * BIOS.
  36. */
  37. /* If you boot an IGP board with a discrete card as the primary,
  38. * the IGP rom is not accessible via the rom bar as the IGP rom is
  39. * part of the system bios. On boot, the system bios puts a
  40. * copy of the igp rom at the start of vram if a discrete card is
  41. * present.
  42. */
  43. static bool igp_read_bios_from_vram(struct amdgpu_device *adev)
  44. {
  45. uint8_t __iomem *bios;
  46. resource_size_t vram_base;
  47. resource_size_t size = 256 * 1024; /* ??? */
  48. if (!(adev->flags & AMDGPU_IS_APU))
  49. if (!amdgpu_card_posted(adev))
  50. return false;
  51. adev->bios = NULL;
  52. vram_base = pci_resource_start(adev->pdev, 0);
  53. bios = ioremap(vram_base, size);
  54. if (!bios) {
  55. return false;
  56. }
  57. if (size == 0 || bios[0] != 0x55 || bios[1] != 0xaa) {
  58. iounmap(bios);
  59. return false;
  60. }
  61. adev->bios = kmalloc(size, GFP_KERNEL);
  62. if (adev->bios == NULL) {
  63. iounmap(bios);
  64. return false;
  65. }
  66. memcpy_fromio(adev->bios, bios, size);
  67. iounmap(bios);
  68. return true;
  69. }
  70. bool amdgpu_read_bios(struct amdgpu_device *adev)
  71. {
  72. uint8_t __iomem *bios;
  73. size_t size;
  74. adev->bios = NULL;
  75. /* XXX: some cards may return 0 for rom size? ddx has a workaround */
  76. bios = pci_map_rom(adev->pdev, &size);
  77. if (!bios) {
  78. return false;
  79. }
  80. if (size == 0 || bios[0] != 0x55 || bios[1] != 0xaa) {
  81. pci_unmap_rom(adev->pdev, bios);
  82. return false;
  83. }
  84. adev->bios = kmemdup(bios, size, GFP_KERNEL);
  85. if (adev->bios == NULL) {
  86. pci_unmap_rom(adev->pdev, bios);
  87. return false;
  88. }
  89. pci_unmap_rom(adev->pdev, bios);
  90. return true;
  91. }
  92. static bool amdgpu_read_platform_bios(struct amdgpu_device *adev)
  93. {
  94. uint8_t __iomem *bios;
  95. size_t size;
  96. adev->bios = NULL;
  97. bios = pci_platform_rom(adev->pdev, &size);
  98. if (!bios) {
  99. return false;
  100. }
  101. if (size == 0 || bios[0] != 0x55 || bios[1] != 0xaa) {
  102. return false;
  103. }
  104. adev->bios = kmemdup(bios, size, GFP_KERNEL);
  105. if (adev->bios == NULL) {
  106. return false;
  107. }
  108. return true;
  109. }
  110. #ifdef CONFIG_ACPI
  111. /* ATRM is used to get the BIOS on the discrete cards in
  112. * dual-gpu systems.
  113. */
  114. /* retrieve the ROM in 4k blocks */
  115. #define ATRM_BIOS_PAGE 4096
  116. /**
  117. * amdgpu_atrm_call - fetch a chunk of the vbios
  118. *
  119. * @atrm_handle: acpi ATRM handle
  120. * @bios: vbios image pointer
  121. * @offset: offset of vbios image data to fetch
  122. * @len: length of vbios image data to fetch
  123. *
  124. * Executes ATRM to fetch a chunk of the discrete
  125. * vbios image on PX systems (all asics).
  126. * Returns the length of the buffer fetched.
  127. */
  128. static int amdgpu_atrm_call(acpi_handle atrm_handle, uint8_t *bios,
  129. int offset, int len)
  130. {
  131. acpi_status status;
  132. union acpi_object atrm_arg_elements[2], *obj;
  133. struct acpi_object_list atrm_arg;
  134. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
  135. atrm_arg.count = 2;
  136. atrm_arg.pointer = &atrm_arg_elements[0];
  137. atrm_arg_elements[0].type = ACPI_TYPE_INTEGER;
  138. atrm_arg_elements[0].integer.value = offset;
  139. atrm_arg_elements[1].type = ACPI_TYPE_INTEGER;
  140. atrm_arg_elements[1].integer.value = len;
  141. status = acpi_evaluate_object(atrm_handle, NULL, &atrm_arg, &buffer);
  142. if (ACPI_FAILURE(status)) {
  143. printk("failed to evaluate ATRM got %s\n", acpi_format_exception(status));
  144. return -ENODEV;
  145. }
  146. obj = (union acpi_object *)buffer.pointer;
  147. memcpy(bios+offset, obj->buffer.pointer, obj->buffer.length);
  148. len = obj->buffer.length;
  149. kfree(buffer.pointer);
  150. return len;
  151. }
  152. static bool amdgpu_atrm_get_bios(struct amdgpu_device *adev)
  153. {
  154. int ret;
  155. int size = 256 * 1024;
  156. int i;
  157. struct pci_dev *pdev = NULL;
  158. acpi_handle dhandle, atrm_handle;
  159. acpi_status status;
  160. bool found = false;
  161. /* ATRM is for the discrete card only */
  162. if (adev->flags & AMDGPU_IS_APU)
  163. return false;
  164. while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
  165. dhandle = ACPI_HANDLE(&pdev->dev);
  166. if (!dhandle)
  167. continue;
  168. status = acpi_get_handle(dhandle, "ATRM", &atrm_handle);
  169. if (!ACPI_FAILURE(status)) {
  170. found = true;
  171. break;
  172. }
  173. }
  174. if (!found) {
  175. while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) {
  176. dhandle = ACPI_HANDLE(&pdev->dev);
  177. if (!dhandle)
  178. continue;
  179. status = acpi_get_handle(dhandle, "ATRM", &atrm_handle);
  180. if (!ACPI_FAILURE(status)) {
  181. found = true;
  182. break;
  183. }
  184. }
  185. }
  186. if (!found)
  187. return false;
  188. adev->bios = kmalloc(size, GFP_KERNEL);
  189. if (!adev->bios) {
  190. DRM_ERROR("Unable to allocate bios\n");
  191. return false;
  192. }
  193. for (i = 0; i < size / ATRM_BIOS_PAGE; i++) {
  194. ret = amdgpu_atrm_call(atrm_handle,
  195. adev->bios,
  196. (i * ATRM_BIOS_PAGE),
  197. ATRM_BIOS_PAGE);
  198. if (ret < ATRM_BIOS_PAGE)
  199. break;
  200. }
  201. if (i == 0 || adev->bios[0] != 0x55 || adev->bios[1] != 0xaa) {
  202. kfree(adev->bios);
  203. return false;
  204. }
  205. return true;
  206. }
  207. #else
  208. static inline bool amdgpu_atrm_get_bios(struct amdgpu_device *adev)
  209. {
  210. return false;
  211. }
  212. #endif
  213. static bool amdgpu_read_disabled_bios(struct amdgpu_device *adev)
  214. {
  215. if (adev->flags & AMDGPU_IS_APU)
  216. return igp_read_bios_from_vram(adev);
  217. else
  218. return amdgpu_asic_read_disabled_bios(adev);
  219. }
  220. #ifdef CONFIG_ACPI
  221. static bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev)
  222. {
  223. bool ret = false;
  224. struct acpi_table_header *hdr;
  225. acpi_size tbl_size;
  226. UEFI_ACPI_VFCT *vfct;
  227. GOP_VBIOS_CONTENT *vbios;
  228. VFCT_IMAGE_HEADER *vhdr;
  229. if (!ACPI_SUCCESS(acpi_get_table_with_size("VFCT", 1, &hdr, &tbl_size)))
  230. return false;
  231. if (tbl_size < sizeof(UEFI_ACPI_VFCT)) {
  232. DRM_ERROR("ACPI VFCT table present but broken (too short #1)\n");
  233. goto out_unmap;
  234. }
  235. vfct = (UEFI_ACPI_VFCT *)hdr;
  236. if (vfct->VBIOSImageOffset + sizeof(VFCT_IMAGE_HEADER) > tbl_size) {
  237. DRM_ERROR("ACPI VFCT table present but broken (too short #2)\n");
  238. goto out_unmap;
  239. }
  240. vbios = (GOP_VBIOS_CONTENT *)((char *)hdr + vfct->VBIOSImageOffset);
  241. vhdr = &vbios->VbiosHeader;
  242. DRM_INFO("ACPI VFCT contains a BIOS for %02x:%02x.%d %04x:%04x, size %d\n",
  243. vhdr->PCIBus, vhdr->PCIDevice, vhdr->PCIFunction,
  244. vhdr->VendorID, vhdr->DeviceID, vhdr->ImageLength);
  245. if (vhdr->PCIBus != adev->pdev->bus->number ||
  246. vhdr->PCIDevice != PCI_SLOT(adev->pdev->devfn) ||
  247. vhdr->PCIFunction != PCI_FUNC(adev->pdev->devfn) ||
  248. vhdr->VendorID != adev->pdev->vendor ||
  249. vhdr->DeviceID != adev->pdev->device) {
  250. DRM_INFO("ACPI VFCT table is not for this card\n");
  251. goto out_unmap;
  252. }
  253. if (vfct->VBIOSImageOffset + sizeof(VFCT_IMAGE_HEADER) + vhdr->ImageLength > tbl_size) {
  254. DRM_ERROR("ACPI VFCT image truncated\n");
  255. goto out_unmap;
  256. }
  257. adev->bios = kmemdup(&vbios->VbiosContent, vhdr->ImageLength, GFP_KERNEL);
  258. ret = !!adev->bios;
  259. out_unmap:
  260. return ret;
  261. }
  262. #else
  263. static inline bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev)
  264. {
  265. return false;
  266. }
  267. #endif
  268. bool amdgpu_get_bios(struct amdgpu_device *adev)
  269. {
  270. bool r;
  271. uint16_t tmp;
  272. r = amdgpu_atrm_get_bios(adev);
  273. if (r == false)
  274. r = amdgpu_acpi_vfct_bios(adev);
  275. if (r == false)
  276. r = igp_read_bios_from_vram(adev);
  277. if (r == false)
  278. r = amdgpu_read_bios(adev);
  279. if (r == false) {
  280. r = amdgpu_read_disabled_bios(adev);
  281. }
  282. if (r == false) {
  283. r = amdgpu_read_platform_bios(adev);
  284. }
  285. if (r == false || adev->bios == NULL) {
  286. DRM_ERROR("Unable to locate a BIOS ROM\n");
  287. adev->bios = NULL;
  288. return false;
  289. }
  290. if (adev->bios[0] != 0x55 || adev->bios[1] != 0xaa) {
  291. printk("BIOS signature incorrect %x %x\n", adev->bios[0], adev->bios[1]);
  292. goto free_bios;
  293. }
  294. tmp = RBIOS16(0x18);
  295. if (RBIOS8(tmp + 0x14) != 0x0) {
  296. DRM_INFO("Not an x86 BIOS ROM, not using.\n");
  297. goto free_bios;
  298. }
  299. adev->bios_header_start = RBIOS16(0x48);
  300. if (!adev->bios_header_start) {
  301. goto free_bios;
  302. }
  303. tmp = adev->bios_header_start + 4;
  304. if (!memcmp(adev->bios + tmp, "ATOM", 4) ||
  305. !memcmp(adev->bios + tmp, "MOTA", 4)) {
  306. adev->is_atom_bios = true;
  307. } else {
  308. adev->is_atom_bios = false;
  309. }
  310. DRM_DEBUG("%sBIOS detected\n", adev->is_atom_bios ? "ATOM" : "COM");
  311. return true;
  312. free_bios:
  313. kfree(adev->bios);
  314. adev->bios = NULL;
  315. return false;
  316. }