base.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright 2012 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include "priv.h"
  25. #include <subdev/bios.h>
  26. #include <subdev/bios/bmp.h>
  27. #include <subdev/bios/bit.h>
  28. #include <subdev/bios/image.h>
  29. static bool
  30. nvbios_addr(struct nvkm_bios *bios, u32 *addr, u8 size)
  31. {
  32. u32 p = *addr;
  33. if (*addr > bios->image0_size && bios->imaged_addr) {
  34. *addr -= bios->image0_size;
  35. *addr += bios->imaged_addr;
  36. }
  37. if (unlikely(*addr + size >= bios->size)) {
  38. nvkm_error(&bios->subdev, "OOB %d %08x %08x\n", size, p, *addr);
  39. return false;
  40. }
  41. return true;
  42. }
  43. u8
  44. nvbios_rd08(struct nvkm_bios *bios, u32 addr)
  45. {
  46. if (likely(nvbios_addr(bios, &addr, 1)))
  47. return bios->data[addr];
  48. return 0x00;
  49. }
  50. u16
  51. nvbios_rd16(struct nvkm_bios *bios, u32 addr)
  52. {
  53. if (likely(nvbios_addr(bios, &addr, 2)))
  54. return get_unaligned_le16(&bios->data[addr]);
  55. return 0x0000;
  56. }
  57. u32
  58. nvbios_rd32(struct nvkm_bios *bios, u32 addr)
  59. {
  60. if (likely(nvbios_addr(bios, &addr, 4)))
  61. return get_unaligned_le32(&bios->data[addr]);
  62. return 0x00000000;
  63. }
  64. u8
  65. nvbios_checksum(const u8 *data, int size)
  66. {
  67. u8 sum = 0;
  68. while (size--)
  69. sum += *data++;
  70. return sum;
  71. }
  72. u16
  73. nvbios_findstr(const u8 *data, int size, const char *str, int len)
  74. {
  75. int i, j;
  76. for (i = 0; i <= (size - len); i++) {
  77. for (j = 0; j < len; j++)
  78. if ((char)data[i + j] != str[j])
  79. break;
  80. if (j == len)
  81. return i;
  82. }
  83. return 0;
  84. }
  85. int
  86. nvbios_memcmp(struct nvkm_bios *bios, u32 addr, const char *str, u32 len)
  87. {
  88. unsigned char c1, c2;
  89. while (len--) {
  90. c1 = nvbios_rd08(bios, addr++);
  91. c2 = *(str++);
  92. if (c1 != c2)
  93. return c1 - c2;
  94. }
  95. return 0;
  96. }
  97. int
  98. nvbios_extend(struct nvkm_bios *bios, u32 length)
  99. {
  100. if (bios->size < length) {
  101. u8 *prev = bios->data;
  102. if (!(bios->data = kmalloc(length, GFP_KERNEL))) {
  103. bios->data = prev;
  104. return -ENOMEM;
  105. }
  106. memcpy(bios->data, prev, bios->size);
  107. bios->size = length;
  108. kfree(prev);
  109. return 1;
  110. }
  111. return 0;
  112. }
  113. static void *
  114. nvkm_bios_dtor(struct nvkm_subdev *subdev)
  115. {
  116. struct nvkm_bios *bios = nvkm_bios(subdev);
  117. kfree(bios->data);
  118. return bios;
  119. }
  120. static const struct nvkm_subdev_func
  121. nvkm_bios = {
  122. .dtor = nvkm_bios_dtor,
  123. };
  124. int
  125. nvkm_bios_new(struct nvkm_device *device, int index, struct nvkm_bios **pbios)
  126. {
  127. struct nvkm_bios *bios;
  128. struct nvbios_image image;
  129. struct bit_entry bit_i;
  130. int ret, idx = 0;
  131. if (!(bios = *pbios = kzalloc(sizeof(*bios), GFP_KERNEL)))
  132. return -ENOMEM;
  133. nvkm_subdev_ctor(&nvkm_bios, device, index, &bios->subdev);
  134. ret = nvbios_shadow(bios);
  135. if (ret)
  136. return ret;
  137. /* Some tables have weird pointers that need adjustment before
  138. * they're dereferenced. I'm not entirely sure why...
  139. */
  140. if (nvbios_image(bios, idx++, &image)) {
  141. bios->image0_size = image.size;
  142. while (nvbios_image(bios, idx++, &image)) {
  143. if (image.type == 0xe0) {
  144. bios->imaged_addr = image.base;
  145. break;
  146. }
  147. }
  148. }
  149. /* detect type of vbios we're dealing with */
  150. bios->bmp_offset = nvbios_findstr(bios->data, bios->size,
  151. "\xff\x7f""NV\0", 5);
  152. if (bios->bmp_offset) {
  153. nvkm_debug(&bios->subdev, "BMP version %x.%x\n",
  154. bmp_version(bios) >> 8,
  155. bmp_version(bios) & 0xff);
  156. }
  157. bios->bit_offset = nvbios_findstr(bios->data, bios->size,
  158. "\xff\xb8""BIT", 5);
  159. if (bios->bit_offset)
  160. nvkm_debug(&bios->subdev, "BIT signature found\n");
  161. /* determine the vbios version number */
  162. if (!bit_entry(bios, 'i', &bit_i) && bit_i.length >= 4) {
  163. bios->version.major = nvbios_rd08(bios, bit_i.offset + 3);
  164. bios->version.chip = nvbios_rd08(bios, bit_i.offset + 2);
  165. bios->version.minor = nvbios_rd08(bios, bit_i.offset + 1);
  166. bios->version.micro = nvbios_rd08(bios, bit_i.offset + 0);
  167. bios->version.patch = nvbios_rd08(bios, bit_i.offset + 4);
  168. } else
  169. if (bmp_version(bios)) {
  170. bios->version.major = nvbios_rd08(bios, bios->bmp_offset + 13);
  171. bios->version.chip = nvbios_rd08(bios, bios->bmp_offset + 12);
  172. bios->version.minor = nvbios_rd08(bios, bios->bmp_offset + 11);
  173. bios->version.micro = nvbios_rd08(bios, bios->bmp_offset + 10);
  174. }
  175. nvkm_info(&bios->subdev, "version %02x.%02x.%02x.%02x.%02x\n",
  176. bios->version.major, bios->version.chip,
  177. bios->version.minor, bios->version.micro, bios->version.patch);
  178. return 0;
  179. }