amdgpu_bios.c 10 KB

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