amdgpu_bios.c 11 KB

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