amdgpu_bios.c 9.1 KB

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