amdgpu_bios.c 10 KB

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