amdgpu_bios.c 11 KB

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