amdgpu_bios.c 10 KB

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