nouveau_bios.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132
  1. /*
  2. * Copyright 2005-2006 Erik Waling
  3. * Copyright 2006 Stephane Marchesin
  4. * Copyright 2007-2009 Stuart Bennett
  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 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
  21. * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #include <drm/drmP.h>
  25. #include "nouveau_drv.h"
  26. #include "nouveau_reg.h"
  27. #include "dispnv04/hw.h"
  28. #include "nouveau_encoder.h"
  29. #include <linux/io-mapping.h>
  30. #include <linux/firmware.h>
  31. /* these defines are made up */
  32. #define NV_CIO_CRE_44_HEADA 0x0
  33. #define NV_CIO_CRE_44_HEADB 0x3
  34. #define FEATURE_MOBILE 0x10 /* also FEATURE_QUADRO for BMP */
  35. #define EDID1_LEN 128
  36. #define BIOSLOG(sip, fmt, arg...) NV_DEBUG(sip->dev, fmt, ##arg)
  37. #define LOG_OLD_VALUE(x)
  38. struct init_exec {
  39. bool execute;
  40. bool repeat;
  41. };
  42. static bool nv_cksum(const uint8_t *data, unsigned int length)
  43. {
  44. /*
  45. * There's a few checksums in the BIOS, so here's a generic checking
  46. * function.
  47. */
  48. int i;
  49. uint8_t sum = 0;
  50. for (i = 0; i < length; i++)
  51. sum += data[i];
  52. if (sum)
  53. return true;
  54. return false;
  55. }
  56. static uint16_t clkcmptable(struct nvbios *bios, uint16_t clktable, int pxclk)
  57. {
  58. int compare_record_len, i = 0;
  59. uint16_t compareclk, scriptptr = 0;
  60. if (bios->major_version < 5) /* pre BIT */
  61. compare_record_len = 3;
  62. else
  63. compare_record_len = 4;
  64. do {
  65. compareclk = ROM16(bios->data[clktable + compare_record_len * i]);
  66. if (pxclk >= compareclk * 10) {
  67. if (bios->major_version < 5) {
  68. uint8_t tmdssub = bios->data[clktable + 2 + compare_record_len * i];
  69. scriptptr = ROM16(bios->data[bios->init_script_tbls_ptr + tmdssub * 2]);
  70. } else
  71. scriptptr = ROM16(bios->data[clktable + 2 + compare_record_len * i]);
  72. break;
  73. }
  74. i++;
  75. } while (compareclk);
  76. return scriptptr;
  77. }
  78. static void
  79. run_digital_op_script(struct drm_device *dev, uint16_t scriptptr,
  80. struct dcb_output *dcbent, int head, bool dl)
  81. {
  82. struct nouveau_drm *drm = nouveau_drm(dev);
  83. NV_INFO(drm, "0x%04X: Parsing digital output script table\n",
  84. scriptptr);
  85. NVWriteVgaCrtc(dev, 0, NV_CIO_CRE_44, head ? NV_CIO_CRE_44_HEADB :
  86. NV_CIO_CRE_44_HEADA);
  87. nouveau_bios_run_init_table(dev, scriptptr, dcbent, head);
  88. nv04_dfp_bind_head(dev, dcbent, head, dl);
  89. }
  90. static int call_lvds_manufacturer_script(struct drm_device *dev, struct dcb_output *dcbent, int head, enum LVDS_script script)
  91. {
  92. struct nouveau_drm *drm = nouveau_drm(dev);
  93. struct nvbios *bios = &drm->vbios;
  94. uint8_t sub = bios->data[bios->fp.xlated_entry + script] + (bios->fp.link_c_increment && dcbent->or & DCB_OUTPUT_C ? 1 : 0);
  95. uint16_t scriptofs = ROM16(bios->data[bios->init_script_tbls_ptr + sub * 2]);
  96. if (!bios->fp.xlated_entry || !sub || !scriptofs)
  97. return -EINVAL;
  98. run_digital_op_script(dev, scriptofs, dcbent, head, bios->fp.dual_link);
  99. if (script == LVDS_PANEL_OFF) {
  100. /* off-on delay in ms */
  101. mdelay(ROM16(bios->data[bios->fp.xlated_entry + 7]));
  102. }
  103. #ifdef __powerpc__
  104. /* Powerbook specific quirks */
  105. if (script == LVDS_RESET &&
  106. (dev->pdev->device == 0x0179 || dev->pdev->device == 0x0189 ||
  107. dev->pdev->device == 0x0329))
  108. nv_write_tmds(dev, dcbent->or, 0, 0x02, 0x72);
  109. #endif
  110. return 0;
  111. }
  112. static int run_lvds_table(struct drm_device *dev, struct dcb_output *dcbent, int head, enum LVDS_script script, int pxclk)
  113. {
  114. /*
  115. * The BIT LVDS table's header has the information to setup the
  116. * necessary registers. Following the standard 4 byte header are:
  117. * A bitmask byte and a dual-link transition pxclk value for use in
  118. * selecting the init script when not using straps; 4 script pointers
  119. * for panel power, selected by output and on/off; and 8 table pointers
  120. * for panel init, the needed one determined by output, and bits in the
  121. * conf byte. These tables are similar to the TMDS tables, consisting
  122. * of a list of pxclks and script pointers.
  123. */
  124. struct nouveau_drm *drm = nouveau_drm(dev);
  125. struct nvbios *bios = &drm->vbios;
  126. unsigned int outputset = (dcbent->or == 4) ? 1 : 0;
  127. uint16_t scriptptr = 0, clktable;
  128. /*
  129. * For now we assume version 3.0 table - g80 support will need some
  130. * changes
  131. */
  132. switch (script) {
  133. case LVDS_INIT:
  134. return -ENOSYS;
  135. case LVDS_BACKLIGHT_ON:
  136. case LVDS_PANEL_ON:
  137. scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 7 + outputset * 2]);
  138. break;
  139. case LVDS_BACKLIGHT_OFF:
  140. case LVDS_PANEL_OFF:
  141. scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 11 + outputset * 2]);
  142. break;
  143. case LVDS_RESET:
  144. clktable = bios->fp.lvdsmanufacturerpointer + 15;
  145. if (dcbent->or == 4)
  146. clktable += 8;
  147. if (dcbent->lvdsconf.use_straps_for_mode) {
  148. if (bios->fp.dual_link)
  149. clktable += 4;
  150. if (bios->fp.if_is_24bit)
  151. clktable += 2;
  152. } else {
  153. /* using EDID */
  154. int cmpval_24bit = (dcbent->or == 4) ? 4 : 1;
  155. if (bios->fp.dual_link) {
  156. clktable += 4;
  157. cmpval_24bit <<= 1;
  158. }
  159. if (bios->fp.strapless_is_24bit & cmpval_24bit)
  160. clktable += 2;
  161. }
  162. clktable = ROM16(bios->data[clktable]);
  163. if (!clktable) {
  164. NV_ERROR(drm, "Pixel clock comparison table not found\n");
  165. return -ENOENT;
  166. }
  167. scriptptr = clkcmptable(bios, clktable, pxclk);
  168. }
  169. if (!scriptptr) {
  170. NV_ERROR(drm, "LVDS output init script not found\n");
  171. return -ENOENT;
  172. }
  173. run_digital_op_script(dev, scriptptr, dcbent, head, bios->fp.dual_link);
  174. return 0;
  175. }
  176. int call_lvds_script(struct drm_device *dev, struct dcb_output *dcbent, int head, enum LVDS_script script, int pxclk)
  177. {
  178. /*
  179. * LVDS operations are multiplexed in an effort to present a single API
  180. * which works with two vastly differing underlying structures.
  181. * This acts as the demux
  182. */
  183. struct nouveau_drm *drm = nouveau_drm(dev);
  184. struct nvif_object *device = &drm->client.device.object;
  185. struct nvbios *bios = &drm->vbios;
  186. uint8_t lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
  187. uint32_t sel_clk_binding, sel_clk;
  188. int ret;
  189. if (bios->fp.last_script_invoc == (script << 1 | head) || !lvds_ver ||
  190. (lvds_ver >= 0x30 && script == LVDS_INIT))
  191. return 0;
  192. if (!bios->fp.lvds_init_run) {
  193. bios->fp.lvds_init_run = true;
  194. call_lvds_script(dev, dcbent, head, LVDS_INIT, pxclk);
  195. }
  196. if (script == LVDS_PANEL_ON && bios->fp.reset_after_pclk_change)
  197. call_lvds_script(dev, dcbent, head, LVDS_RESET, pxclk);
  198. if (script == LVDS_RESET && bios->fp.power_off_for_reset)
  199. call_lvds_script(dev, dcbent, head, LVDS_PANEL_OFF, pxclk);
  200. NV_INFO(drm, "Calling LVDS script %d:\n", script);
  201. /* don't let script change pll->head binding */
  202. sel_clk_binding = nvif_rd32(device, NV_PRAMDAC_SEL_CLK) & 0x50000;
  203. if (lvds_ver < 0x30)
  204. ret = call_lvds_manufacturer_script(dev, dcbent, head, script);
  205. else
  206. ret = run_lvds_table(dev, dcbent, head, script, pxclk);
  207. bios->fp.last_script_invoc = (script << 1 | head);
  208. sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
  209. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
  210. /* some scripts set a value in NV_PBUS_POWERCTRL_2 and break video overlay */
  211. nvif_wr32(device, NV_PBUS_POWERCTRL_2, 0);
  212. return ret;
  213. }
  214. struct lvdstableheader {
  215. uint8_t lvds_ver, headerlen, recordlen;
  216. };
  217. static int parse_lvds_manufacturer_table_header(struct drm_device *dev, struct nvbios *bios, struct lvdstableheader *lth)
  218. {
  219. /*
  220. * BMP version (0xa) LVDS table has a simple header of version and
  221. * record length. The BIT LVDS table has the typical BIT table header:
  222. * version byte, header length byte, record length byte, and a byte for
  223. * the maximum number of records that can be held in the table.
  224. */
  225. struct nouveau_drm *drm = nouveau_drm(dev);
  226. uint8_t lvds_ver, headerlen, recordlen;
  227. memset(lth, 0, sizeof(struct lvdstableheader));
  228. if (bios->fp.lvdsmanufacturerpointer == 0x0) {
  229. NV_ERROR(drm, "Pointer to LVDS manufacturer table invalid\n");
  230. return -EINVAL;
  231. }
  232. lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
  233. switch (lvds_ver) {
  234. case 0x0a: /* pre NV40 */
  235. headerlen = 2;
  236. recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
  237. break;
  238. case 0x30: /* NV4x */
  239. headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
  240. if (headerlen < 0x1f) {
  241. NV_ERROR(drm, "LVDS table header not understood\n");
  242. return -EINVAL;
  243. }
  244. recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
  245. break;
  246. case 0x40: /* G80/G90 */
  247. headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
  248. if (headerlen < 0x7) {
  249. NV_ERROR(drm, "LVDS table header not understood\n");
  250. return -EINVAL;
  251. }
  252. recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
  253. break;
  254. default:
  255. NV_ERROR(drm,
  256. "LVDS table revision %d.%d not currently supported\n",
  257. lvds_ver >> 4, lvds_ver & 0xf);
  258. return -ENOSYS;
  259. }
  260. lth->lvds_ver = lvds_ver;
  261. lth->headerlen = headerlen;
  262. lth->recordlen = recordlen;
  263. return 0;
  264. }
  265. static int
  266. get_fp_strap(struct drm_device *dev, struct nvbios *bios)
  267. {
  268. struct nouveau_drm *drm = nouveau_drm(dev);
  269. struct nvif_object *device = &drm->client.device.object;
  270. /*
  271. * The fp strap is normally dictated by the "User Strap" in
  272. * PEXTDEV_BOOT_0[20:16], but on BMP cards when bit 2 of the
  273. * Internal_Flags struct at 0x48 is set, the user strap gets overriden
  274. * by the PCI subsystem ID during POST, but not before the previous user
  275. * strap has been committed to CR58 for CR57=0xf on head A, which may be
  276. * read and used instead
  277. */
  278. if (bios->major_version < 5 && bios->data[0x48] & 0x4)
  279. return NVReadVgaCrtc5758(dev, 0, 0xf) & 0xf;
  280. if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_MAXWELL)
  281. return nvif_rd32(device, 0x001800) & 0x0000000f;
  282. else
  283. if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA)
  284. return (nvif_rd32(device, NV_PEXTDEV_BOOT_0) >> 24) & 0xf;
  285. else
  286. return (nvif_rd32(device, NV_PEXTDEV_BOOT_0) >> 16) & 0xf;
  287. }
  288. static int parse_fp_mode_table(struct drm_device *dev, struct nvbios *bios)
  289. {
  290. struct nouveau_drm *drm = nouveau_drm(dev);
  291. uint8_t *fptable;
  292. uint8_t fptable_ver, headerlen = 0, recordlen, fpentries = 0xf, fpindex;
  293. int ret, ofs, fpstrapping;
  294. struct lvdstableheader lth;
  295. if (bios->fp.fptablepointer == 0x0) {
  296. /* Most laptop cards lack an fp table. They use DDC. */
  297. NV_DEBUG(drm, "Pointer to flat panel table invalid\n");
  298. bios->digital_min_front_porch = 0x4b;
  299. return 0;
  300. }
  301. fptable = &bios->data[bios->fp.fptablepointer];
  302. fptable_ver = fptable[0];
  303. switch (fptable_ver) {
  304. /*
  305. * BMP version 0x5.0x11 BIOSen have version 1 like tables, but no
  306. * version field, and miss one of the spread spectrum/PWM bytes.
  307. * This could affect early GF2Go parts (not seen any appropriate ROMs
  308. * though). Here we assume that a version of 0x05 matches this case
  309. * (combining with a BMP version check would be better), as the
  310. * common case for the panel type field is 0x0005, and that is in
  311. * fact what we are reading the first byte of.
  312. */
  313. case 0x05: /* some NV10, 11, 15, 16 */
  314. recordlen = 42;
  315. ofs = -1;
  316. break;
  317. case 0x10: /* some NV15/16, and NV11+ */
  318. recordlen = 44;
  319. ofs = 0;
  320. break;
  321. case 0x20: /* NV40+ */
  322. headerlen = fptable[1];
  323. recordlen = fptable[2];
  324. fpentries = fptable[3];
  325. /*
  326. * fptable[4] is the minimum
  327. * RAMDAC_FP_HCRTC -> RAMDAC_FP_HSYNC_START gap
  328. */
  329. bios->digital_min_front_porch = fptable[4];
  330. ofs = -7;
  331. break;
  332. default:
  333. NV_ERROR(drm,
  334. "FP table revision %d.%d not currently supported\n",
  335. fptable_ver >> 4, fptable_ver & 0xf);
  336. return -ENOSYS;
  337. }
  338. if (!bios->is_mobile) /* !mobile only needs digital_min_front_porch */
  339. return 0;
  340. ret = parse_lvds_manufacturer_table_header(dev, bios, &lth);
  341. if (ret)
  342. return ret;
  343. if (lth.lvds_ver == 0x30 || lth.lvds_ver == 0x40) {
  344. bios->fp.fpxlatetableptr = bios->fp.lvdsmanufacturerpointer +
  345. lth.headerlen + 1;
  346. bios->fp.xlatwidth = lth.recordlen;
  347. }
  348. if (bios->fp.fpxlatetableptr == 0x0) {
  349. NV_ERROR(drm, "Pointer to flat panel xlat table invalid\n");
  350. return -EINVAL;
  351. }
  352. fpstrapping = get_fp_strap(dev, bios);
  353. fpindex = bios->data[bios->fp.fpxlatetableptr +
  354. fpstrapping * bios->fp.xlatwidth];
  355. if (fpindex > fpentries) {
  356. NV_ERROR(drm, "Bad flat panel table index\n");
  357. return -ENOENT;
  358. }
  359. /* nv4x cards need both a strap value and fpindex of 0xf to use DDC */
  360. if (lth.lvds_ver > 0x10)
  361. bios->fp_no_ddc = fpstrapping != 0xf || fpindex != 0xf;
  362. /*
  363. * If either the strap or xlated fpindex value are 0xf there is no
  364. * panel using a strap-derived bios mode present. this condition
  365. * includes, but is different from, the DDC panel indicator above
  366. */
  367. if (fpstrapping == 0xf || fpindex == 0xf)
  368. return 0;
  369. bios->fp.mode_ptr = bios->fp.fptablepointer + headerlen +
  370. recordlen * fpindex + ofs;
  371. NV_INFO(drm, "BIOS FP mode: %dx%d (%dkHz pixel clock)\n",
  372. ROM16(bios->data[bios->fp.mode_ptr + 11]) + 1,
  373. ROM16(bios->data[bios->fp.mode_ptr + 25]) + 1,
  374. ROM16(bios->data[bios->fp.mode_ptr + 7]) * 10);
  375. return 0;
  376. }
  377. bool nouveau_bios_fp_mode(struct drm_device *dev, struct drm_display_mode *mode)
  378. {
  379. struct nouveau_drm *drm = nouveau_drm(dev);
  380. struct nvbios *bios = &drm->vbios;
  381. uint8_t *mode_entry = &bios->data[bios->fp.mode_ptr];
  382. if (!mode) /* just checking whether we can produce a mode */
  383. return bios->fp.mode_ptr;
  384. memset(mode, 0, sizeof(struct drm_display_mode));
  385. /*
  386. * For version 1.0 (version in byte 0):
  387. * bytes 1-2 are "panel type", including bits on whether Colour/mono,
  388. * single/dual link, and type (TFT etc.)
  389. * bytes 3-6 are bits per colour in RGBX
  390. */
  391. mode->clock = ROM16(mode_entry[7]) * 10;
  392. /* bytes 9-10 is HActive */
  393. mode->hdisplay = ROM16(mode_entry[11]) + 1;
  394. /*
  395. * bytes 13-14 is HValid Start
  396. * bytes 15-16 is HValid End
  397. */
  398. mode->hsync_start = ROM16(mode_entry[17]) + 1;
  399. mode->hsync_end = ROM16(mode_entry[19]) + 1;
  400. mode->htotal = ROM16(mode_entry[21]) + 1;
  401. /* bytes 23-24, 27-30 similarly, but vertical */
  402. mode->vdisplay = ROM16(mode_entry[25]) + 1;
  403. mode->vsync_start = ROM16(mode_entry[31]) + 1;
  404. mode->vsync_end = ROM16(mode_entry[33]) + 1;
  405. mode->vtotal = ROM16(mode_entry[35]) + 1;
  406. mode->flags |= (mode_entry[37] & 0x10) ?
  407. DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
  408. mode->flags |= (mode_entry[37] & 0x1) ?
  409. DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
  410. /*
  411. * bytes 38-39 relate to spread spectrum settings
  412. * bytes 40-43 are something to do with PWM
  413. */
  414. mode->status = MODE_OK;
  415. mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  416. drm_mode_set_name(mode);
  417. return bios->fp.mode_ptr;
  418. }
  419. int nouveau_bios_parse_lvds_table(struct drm_device *dev, int pxclk, bool *dl, bool *if_is_24bit)
  420. {
  421. /*
  422. * The LVDS table header is (mostly) described in
  423. * parse_lvds_manufacturer_table_header(): the BIT header additionally
  424. * contains the dual-link transition pxclk (in 10s kHz), at byte 5 - if
  425. * straps are not being used for the panel, this specifies the frequency
  426. * at which modes should be set up in the dual link style.
  427. *
  428. * Following the header, the BMP (ver 0xa) table has several records,
  429. * indexed by a separate xlat table, indexed in turn by the fp strap in
  430. * EXTDEV_BOOT. Each record had a config byte, followed by 6 script
  431. * numbers for use by INIT_SUB which controlled panel init and power,
  432. * and finally a dword of ms to sleep between power off and on
  433. * operations.
  434. *
  435. * In the BIT versions, the table following the header serves as an
  436. * integrated config and xlat table: the records in the table are
  437. * indexed by the FP strap nibble in EXTDEV_BOOT, and each record has
  438. * two bytes - the first as a config byte, the second for indexing the
  439. * fp mode table pointed to by the BIT 'D' table
  440. *
  441. * DDC is not used until after card init, so selecting the correct table
  442. * entry and setting the dual link flag for EDID equipped panels,
  443. * requiring tests against the native-mode pixel clock, cannot be done
  444. * until later, when this function should be called with non-zero pxclk
  445. */
  446. struct nouveau_drm *drm = nouveau_drm(dev);
  447. struct nvbios *bios = &drm->vbios;
  448. int fpstrapping = get_fp_strap(dev, bios), lvdsmanufacturerindex = 0;
  449. struct lvdstableheader lth;
  450. uint16_t lvdsofs;
  451. int ret, chip_version = bios->chip_version;
  452. ret = parse_lvds_manufacturer_table_header(dev, bios, &lth);
  453. if (ret)
  454. return ret;
  455. switch (lth.lvds_ver) {
  456. case 0x0a: /* pre NV40 */
  457. lvdsmanufacturerindex = bios->data[
  458. bios->fp.fpxlatemanufacturertableptr +
  459. fpstrapping];
  460. /* we're done if this isn't the EDID panel case */
  461. if (!pxclk)
  462. break;
  463. if (chip_version < 0x25) {
  464. /* nv17 behaviour
  465. *
  466. * It seems the old style lvds script pointer is reused
  467. * to select 18/24 bit colour depth for EDID panels.
  468. */
  469. lvdsmanufacturerindex =
  470. (bios->legacy.lvds_single_a_script_ptr & 1) ?
  471. 2 : 0;
  472. if (pxclk >= bios->fp.duallink_transition_clk)
  473. lvdsmanufacturerindex++;
  474. } else if (chip_version < 0x30) {
  475. /* nv28 behaviour (off-chip encoder)
  476. *
  477. * nv28 does a complex dance of first using byte 121 of
  478. * the EDID to choose the lvdsmanufacturerindex, then
  479. * later attempting to match the EDID manufacturer and
  480. * product IDs in a table (signature 'pidt' (panel id
  481. * table?)), setting an lvdsmanufacturerindex of 0 and
  482. * an fp strap of the match index (or 0xf if none)
  483. */
  484. lvdsmanufacturerindex = 0;
  485. } else {
  486. /* nv31, nv34 behaviour */
  487. lvdsmanufacturerindex = 0;
  488. if (pxclk >= bios->fp.duallink_transition_clk)
  489. lvdsmanufacturerindex = 2;
  490. if (pxclk >= 140000)
  491. lvdsmanufacturerindex = 3;
  492. }
  493. /*
  494. * nvidia set the high nibble of (cr57=f, cr58) to
  495. * lvdsmanufacturerindex in this case; we don't
  496. */
  497. break;
  498. case 0x30: /* NV4x */
  499. case 0x40: /* G80/G90 */
  500. lvdsmanufacturerindex = fpstrapping;
  501. break;
  502. default:
  503. NV_ERROR(drm, "LVDS table revision not currently supported\n");
  504. return -ENOSYS;
  505. }
  506. lvdsofs = bios->fp.xlated_entry = bios->fp.lvdsmanufacturerpointer + lth.headerlen + lth.recordlen * lvdsmanufacturerindex;
  507. switch (lth.lvds_ver) {
  508. case 0x0a:
  509. bios->fp.power_off_for_reset = bios->data[lvdsofs] & 1;
  510. bios->fp.reset_after_pclk_change = bios->data[lvdsofs] & 2;
  511. bios->fp.dual_link = bios->data[lvdsofs] & 4;
  512. bios->fp.link_c_increment = bios->data[lvdsofs] & 8;
  513. *if_is_24bit = bios->data[lvdsofs] & 16;
  514. break;
  515. case 0x30:
  516. case 0x40:
  517. /*
  518. * No sign of the "power off for reset" or "reset for panel
  519. * on" bits, but it's safer to assume we should
  520. */
  521. bios->fp.power_off_for_reset = true;
  522. bios->fp.reset_after_pclk_change = true;
  523. /*
  524. * It's ok lvdsofs is wrong for nv4x edid case; dual_link is
  525. * over-written, and if_is_24bit isn't used
  526. */
  527. bios->fp.dual_link = bios->data[lvdsofs] & 1;
  528. bios->fp.if_is_24bit = bios->data[lvdsofs] & 2;
  529. bios->fp.strapless_is_24bit = bios->data[bios->fp.lvdsmanufacturerpointer + 4];
  530. bios->fp.duallink_transition_clk = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 5]) * 10;
  531. break;
  532. }
  533. /* set dual_link flag for EDID case */
  534. if (pxclk && (chip_version < 0x25 || chip_version > 0x28))
  535. bios->fp.dual_link = (pxclk >= bios->fp.duallink_transition_clk);
  536. *dl = bios->fp.dual_link;
  537. return 0;
  538. }
  539. int run_tmds_table(struct drm_device *dev, struct dcb_output *dcbent, int head, int pxclk)
  540. {
  541. /*
  542. * the pxclk parameter is in kHz
  543. *
  544. * This runs the TMDS regs setting code found on BIT bios cards
  545. *
  546. * For ffs(or) == 1 use the first table, for ffs(or) == 2 and
  547. * ffs(or) == 3, use the second.
  548. */
  549. struct nouveau_drm *drm = nouveau_drm(dev);
  550. struct nvif_object *device = &drm->client.device.object;
  551. struct nvbios *bios = &drm->vbios;
  552. int cv = bios->chip_version;
  553. uint16_t clktable = 0, scriptptr;
  554. uint32_t sel_clk_binding, sel_clk;
  555. /* pre-nv17 off-chip tmds uses scripts, post nv17 doesn't */
  556. if (cv >= 0x17 && cv != 0x1a && cv != 0x20 &&
  557. dcbent->location != DCB_LOC_ON_CHIP)
  558. return 0;
  559. switch (ffs(dcbent->or)) {
  560. case 1:
  561. clktable = bios->tmds.output0_script_ptr;
  562. break;
  563. case 2:
  564. case 3:
  565. clktable = bios->tmds.output1_script_ptr;
  566. break;
  567. }
  568. if (!clktable) {
  569. NV_ERROR(drm, "Pixel clock comparison table not found\n");
  570. return -EINVAL;
  571. }
  572. scriptptr = clkcmptable(bios, clktable, pxclk);
  573. if (!scriptptr) {
  574. NV_ERROR(drm, "TMDS output init script not found\n");
  575. return -ENOENT;
  576. }
  577. /* don't let script change pll->head binding */
  578. sel_clk_binding = nvif_rd32(device, NV_PRAMDAC_SEL_CLK) & 0x50000;
  579. run_digital_op_script(dev, scriptptr, dcbent, head, pxclk >= 165000);
  580. sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
  581. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
  582. return 0;
  583. }
  584. static void parse_script_table_pointers(struct nvbios *bios, uint16_t offset)
  585. {
  586. /*
  587. * Parses the init table segment for pointers used in script execution.
  588. *
  589. * offset + 0 (16 bits): init script tables pointer
  590. * offset + 2 (16 bits): macro index table pointer
  591. * offset + 4 (16 bits): macro table pointer
  592. * offset + 6 (16 bits): condition table pointer
  593. * offset + 8 (16 bits): io condition table pointer
  594. * offset + 10 (16 bits): io flag condition table pointer
  595. * offset + 12 (16 bits): init function table pointer
  596. */
  597. bios->init_script_tbls_ptr = ROM16(bios->data[offset]);
  598. }
  599. static int parse_bit_A_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  600. {
  601. /*
  602. * Parses the load detect values for g80 cards.
  603. *
  604. * offset + 0 (16 bits): loadval table pointer
  605. */
  606. struct nouveau_drm *drm = nouveau_drm(dev);
  607. uint16_t load_table_ptr;
  608. uint8_t version, headerlen, entrylen, num_entries;
  609. if (bitentry->length != 3) {
  610. NV_ERROR(drm, "Do not understand BIT A table\n");
  611. return -EINVAL;
  612. }
  613. load_table_ptr = ROM16(bios->data[bitentry->offset]);
  614. if (load_table_ptr == 0x0) {
  615. NV_DEBUG(drm, "Pointer to BIT loadval table invalid\n");
  616. return -EINVAL;
  617. }
  618. version = bios->data[load_table_ptr];
  619. if (version != 0x10) {
  620. NV_ERROR(drm, "BIT loadval table version %d.%d not supported\n",
  621. version >> 4, version & 0xF);
  622. return -ENOSYS;
  623. }
  624. headerlen = bios->data[load_table_ptr + 1];
  625. entrylen = bios->data[load_table_ptr + 2];
  626. num_entries = bios->data[load_table_ptr + 3];
  627. if (headerlen != 4 || entrylen != 4 || num_entries != 2) {
  628. NV_ERROR(drm, "Do not understand BIT loadval table\n");
  629. return -EINVAL;
  630. }
  631. /* First entry is normal dac, 2nd tv-out perhaps? */
  632. bios->dactestval = ROM32(bios->data[load_table_ptr + headerlen]) & 0x3ff;
  633. return 0;
  634. }
  635. static int parse_bit_display_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  636. {
  637. /*
  638. * Parses the flat panel table segment that the bit entry points to.
  639. * Starting at bitentry->offset:
  640. *
  641. * offset + 0 (16 bits): ??? table pointer - seems to have 18 byte
  642. * records beginning with a freq.
  643. * offset + 2 (16 bits): mode table pointer
  644. */
  645. struct nouveau_drm *drm = nouveau_drm(dev);
  646. if (bitentry->length != 4) {
  647. NV_ERROR(drm, "Do not understand BIT display table\n");
  648. return -EINVAL;
  649. }
  650. bios->fp.fptablepointer = ROM16(bios->data[bitentry->offset + 2]);
  651. return 0;
  652. }
  653. static int parse_bit_init_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  654. {
  655. /*
  656. * Parses the init table segment that the bit entry points to.
  657. *
  658. * See parse_script_table_pointers for layout
  659. */
  660. struct nouveau_drm *drm = nouveau_drm(dev);
  661. if (bitentry->length < 14) {
  662. NV_ERROR(drm, "Do not understand init table\n");
  663. return -EINVAL;
  664. }
  665. parse_script_table_pointers(bios, bitentry->offset);
  666. return 0;
  667. }
  668. static int parse_bit_i_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  669. {
  670. /*
  671. * BIT 'i' (info?) table
  672. *
  673. * offset + 0 (32 bits): BIOS version dword (as in B table)
  674. * offset + 5 (8 bits): BIOS feature byte (same as for BMP?)
  675. * offset + 13 (16 bits): pointer to table containing DAC load
  676. * detection comparison values
  677. *
  678. * There's other things in the table, purpose unknown
  679. */
  680. struct nouveau_drm *drm = nouveau_drm(dev);
  681. uint16_t daccmpoffset;
  682. uint8_t dacver, dacheaderlen;
  683. if (bitentry->length < 6) {
  684. NV_ERROR(drm, "BIT i table too short for needed information\n");
  685. return -EINVAL;
  686. }
  687. /*
  688. * bit 4 seems to indicate a mobile bios (doesn't suffer from BMP's
  689. * Quadro identity crisis), other bits possibly as for BMP feature byte
  690. */
  691. bios->feature_byte = bios->data[bitentry->offset + 5];
  692. bios->is_mobile = bios->feature_byte & FEATURE_MOBILE;
  693. if (bitentry->length < 15) {
  694. NV_WARN(drm, "BIT i table not long enough for DAC load "
  695. "detection comparison table\n");
  696. return -EINVAL;
  697. }
  698. daccmpoffset = ROM16(bios->data[bitentry->offset + 13]);
  699. /* doesn't exist on g80 */
  700. if (!daccmpoffset)
  701. return 0;
  702. /*
  703. * The first value in the table, following the header, is the
  704. * comparison value, the second entry is a comparison value for
  705. * TV load detection.
  706. */
  707. dacver = bios->data[daccmpoffset];
  708. dacheaderlen = bios->data[daccmpoffset + 1];
  709. if (dacver != 0x00 && dacver != 0x10) {
  710. NV_WARN(drm, "DAC load detection comparison table version "
  711. "%d.%d not known\n", dacver >> 4, dacver & 0xf);
  712. return -ENOSYS;
  713. }
  714. bios->dactestval = ROM32(bios->data[daccmpoffset + dacheaderlen]);
  715. bios->tvdactestval = ROM32(bios->data[daccmpoffset + dacheaderlen + 4]);
  716. return 0;
  717. }
  718. static int parse_bit_lvds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  719. {
  720. /*
  721. * Parses the LVDS table segment that the bit entry points to.
  722. * Starting at bitentry->offset:
  723. *
  724. * offset + 0 (16 bits): LVDS strap xlate table pointer
  725. */
  726. struct nouveau_drm *drm = nouveau_drm(dev);
  727. if (bitentry->length != 2) {
  728. NV_ERROR(drm, "Do not understand BIT LVDS table\n");
  729. return -EINVAL;
  730. }
  731. /*
  732. * No idea if it's still called the LVDS manufacturer table, but
  733. * the concept's close enough.
  734. */
  735. bios->fp.lvdsmanufacturerpointer = ROM16(bios->data[bitentry->offset]);
  736. return 0;
  737. }
  738. static int
  739. parse_bit_M_tbl_entry(struct drm_device *dev, struct nvbios *bios,
  740. struct bit_entry *bitentry)
  741. {
  742. /*
  743. * offset + 2 (8 bits): number of options in an
  744. * INIT_RAM_RESTRICT_ZM_REG_GROUP opcode option set
  745. * offset + 3 (16 bits): pointer to strap xlate table for RAM
  746. * restrict option selection
  747. *
  748. * There's a bunch of bits in this table other than the RAM restrict
  749. * stuff that we don't use - their use currently unknown
  750. */
  751. /*
  752. * Older bios versions don't have a sufficiently long table for
  753. * what we want
  754. */
  755. if (bitentry->length < 0x5)
  756. return 0;
  757. if (bitentry->version < 2) {
  758. bios->ram_restrict_group_count = bios->data[bitentry->offset + 2];
  759. bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 3]);
  760. } else {
  761. bios->ram_restrict_group_count = bios->data[bitentry->offset + 0];
  762. bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 1]);
  763. }
  764. return 0;
  765. }
  766. static int parse_bit_tmds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  767. {
  768. /*
  769. * Parses the pointer to the TMDS table
  770. *
  771. * Starting at bitentry->offset:
  772. *
  773. * offset + 0 (16 bits): TMDS table pointer
  774. *
  775. * The TMDS table is typically found just before the DCB table, with a
  776. * characteristic signature of 0x11,0x13 (1.1 being version, 0x13 being
  777. * length?)
  778. *
  779. * At offset +7 is a pointer to a script, which I don't know how to
  780. * run yet.
  781. * At offset +9 is a pointer to another script, likewise
  782. * Offset +11 has a pointer to a table where the first word is a pxclk
  783. * frequency and the second word a pointer to a script, which should be
  784. * run if the comparison pxclk frequency is less than the pxclk desired.
  785. * This repeats for decreasing comparison frequencies
  786. * Offset +13 has a pointer to a similar table
  787. * The selection of table (and possibly +7/+9 script) is dictated by
  788. * "or" from the DCB.
  789. */
  790. struct nouveau_drm *drm = nouveau_drm(dev);
  791. uint16_t tmdstableptr, script1, script2;
  792. if (bitentry->length != 2) {
  793. NV_ERROR(drm, "Do not understand BIT TMDS table\n");
  794. return -EINVAL;
  795. }
  796. tmdstableptr = ROM16(bios->data[bitentry->offset]);
  797. if (!tmdstableptr) {
  798. NV_ERROR(drm, "Pointer to TMDS table invalid\n");
  799. return -EINVAL;
  800. }
  801. NV_INFO(drm, "TMDS table version %d.%d\n",
  802. bios->data[tmdstableptr] >> 4, bios->data[tmdstableptr] & 0xf);
  803. /* nv50+ has v2.0, but we don't parse it atm */
  804. if (bios->data[tmdstableptr] != 0x11)
  805. return -ENOSYS;
  806. /*
  807. * These two scripts are odd: they don't seem to get run even when
  808. * they are not stubbed.
  809. */
  810. script1 = ROM16(bios->data[tmdstableptr + 7]);
  811. script2 = ROM16(bios->data[tmdstableptr + 9]);
  812. if (bios->data[script1] != 'q' || bios->data[script2] != 'q')
  813. NV_WARN(drm, "TMDS table script pointers not stubbed\n");
  814. bios->tmds.output0_script_ptr = ROM16(bios->data[tmdstableptr + 11]);
  815. bios->tmds.output1_script_ptr = ROM16(bios->data[tmdstableptr + 13]);
  816. return 0;
  817. }
  818. struct bit_table {
  819. const char id;
  820. int (* const parse_fn)(struct drm_device *, struct nvbios *, struct bit_entry *);
  821. };
  822. #define BIT_TABLE(id, funcid) ((struct bit_table){ id, parse_bit_##funcid##_tbl_entry })
  823. int
  824. bit_table(struct drm_device *dev, u8 id, struct bit_entry *bit)
  825. {
  826. struct nouveau_drm *drm = nouveau_drm(dev);
  827. struct nvbios *bios = &drm->vbios;
  828. u8 entries, *entry;
  829. if (bios->type != NVBIOS_BIT)
  830. return -ENODEV;
  831. entries = bios->data[bios->offset + 10];
  832. entry = &bios->data[bios->offset + 12];
  833. while (entries--) {
  834. if (entry[0] == id) {
  835. bit->id = entry[0];
  836. bit->version = entry[1];
  837. bit->length = ROM16(entry[2]);
  838. bit->offset = ROM16(entry[4]);
  839. bit->data = ROMPTR(dev, entry[4]);
  840. return 0;
  841. }
  842. entry += bios->data[bios->offset + 9];
  843. }
  844. return -ENOENT;
  845. }
  846. static int
  847. parse_bit_table(struct nvbios *bios, const uint16_t bitoffset,
  848. struct bit_table *table)
  849. {
  850. struct drm_device *dev = bios->dev;
  851. struct nouveau_drm *drm = nouveau_drm(dev);
  852. struct bit_entry bitentry;
  853. if (bit_table(dev, table->id, &bitentry) == 0)
  854. return table->parse_fn(dev, bios, &bitentry);
  855. NV_INFO(drm, "BIT table '%c' not found\n", table->id);
  856. return -ENOSYS;
  857. }
  858. static int
  859. parse_bit_structure(struct nvbios *bios, const uint16_t bitoffset)
  860. {
  861. int ret;
  862. /*
  863. * The only restriction on parsing order currently is having 'i' first
  864. * for use of bios->*_version or bios->feature_byte while parsing;
  865. * functions shouldn't be actually *doing* anything apart from pulling
  866. * data from the image into the bios struct, thus no interdependencies
  867. */
  868. ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('i', i));
  869. if (ret) /* info? */
  870. return ret;
  871. if (bios->major_version >= 0x60) /* g80+ */
  872. parse_bit_table(bios, bitoffset, &BIT_TABLE('A', A));
  873. parse_bit_table(bios, bitoffset, &BIT_TABLE('D', display));
  874. ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('I', init));
  875. if (ret)
  876. return ret;
  877. parse_bit_table(bios, bitoffset, &BIT_TABLE('M', M)); /* memory? */
  878. parse_bit_table(bios, bitoffset, &BIT_TABLE('L', lvds));
  879. parse_bit_table(bios, bitoffset, &BIT_TABLE('T', tmds));
  880. return 0;
  881. }
  882. static int parse_bmp_structure(struct drm_device *dev, struct nvbios *bios, unsigned int offset)
  883. {
  884. /*
  885. * Parses the BMP structure for useful things, but does not act on them
  886. *
  887. * offset + 5: BMP major version
  888. * offset + 6: BMP minor version
  889. * offset + 9: BMP feature byte
  890. * offset + 10: BCD encoded BIOS version
  891. *
  892. * offset + 18: init script table pointer (for bios versions < 5.10h)
  893. * offset + 20: extra init script table pointer (for bios
  894. * versions < 5.10h)
  895. *
  896. * offset + 24: memory init table pointer (used on early bios versions)
  897. * offset + 26: SDR memory sequencing setup data table
  898. * offset + 28: DDR memory sequencing setup data table
  899. *
  900. * offset + 54: index of I2C CRTC pair to use for CRT output
  901. * offset + 55: index of I2C CRTC pair to use for TV output
  902. * offset + 56: index of I2C CRTC pair to use for flat panel output
  903. * offset + 58: write CRTC index for I2C pair 0
  904. * offset + 59: read CRTC index for I2C pair 0
  905. * offset + 60: write CRTC index for I2C pair 1
  906. * offset + 61: read CRTC index for I2C pair 1
  907. *
  908. * offset + 67: maximum internal PLL frequency (single stage PLL)
  909. * offset + 71: minimum internal PLL frequency (single stage PLL)
  910. *
  911. * offset + 75: script table pointers, as described in
  912. * parse_script_table_pointers
  913. *
  914. * offset + 89: TMDS single link output A table pointer
  915. * offset + 91: TMDS single link output B table pointer
  916. * offset + 95: LVDS single link output A table pointer
  917. * offset + 105: flat panel timings table pointer
  918. * offset + 107: flat panel strapping translation table pointer
  919. * offset + 117: LVDS manufacturer panel config table pointer
  920. * offset + 119: LVDS manufacturer strapping translation table pointer
  921. *
  922. * offset + 142: PLL limits table pointer
  923. *
  924. * offset + 156: minimum pixel clock for LVDS dual link
  925. */
  926. struct nouveau_drm *drm = nouveau_drm(dev);
  927. uint8_t *bmp = &bios->data[offset], bmp_version_major, bmp_version_minor;
  928. uint16_t bmplength;
  929. uint16_t legacy_scripts_offset, legacy_i2c_offset;
  930. /* load needed defaults in case we can't parse this info */
  931. bios->digital_min_front_porch = 0x4b;
  932. bios->fmaxvco = 256000;
  933. bios->fminvco = 128000;
  934. bios->fp.duallink_transition_clk = 90000;
  935. bmp_version_major = bmp[5];
  936. bmp_version_minor = bmp[6];
  937. NV_INFO(drm, "BMP version %d.%d\n",
  938. bmp_version_major, bmp_version_minor);
  939. /*
  940. * Make sure that 0x36 is blank and can't be mistaken for a DCB
  941. * pointer on early versions
  942. */
  943. if (bmp_version_major < 5)
  944. *(uint16_t *)&bios->data[0x36] = 0;
  945. /*
  946. * Seems that the minor version was 1 for all major versions prior
  947. * to 5. Version 6 could theoretically exist, but I suspect BIT
  948. * happened instead.
  949. */
  950. if ((bmp_version_major < 5 && bmp_version_minor != 1) || bmp_version_major > 5) {
  951. NV_ERROR(drm, "You have an unsupported BMP version. "
  952. "Please send in your bios\n");
  953. return -ENOSYS;
  954. }
  955. if (bmp_version_major == 0)
  956. /* nothing that's currently useful in this version */
  957. return 0;
  958. else if (bmp_version_major == 1)
  959. bmplength = 44; /* exact for 1.01 */
  960. else if (bmp_version_major == 2)
  961. bmplength = 48; /* exact for 2.01 */
  962. else if (bmp_version_major == 3)
  963. bmplength = 54;
  964. /* guessed - mem init tables added in this version */
  965. else if (bmp_version_major == 4 || bmp_version_minor < 0x1)
  966. /* don't know if 5.0 exists... */
  967. bmplength = 62;
  968. /* guessed - BMP I2C indices added in version 4*/
  969. else if (bmp_version_minor < 0x6)
  970. bmplength = 67; /* exact for 5.01 */
  971. else if (bmp_version_minor < 0x10)
  972. bmplength = 75; /* exact for 5.06 */
  973. else if (bmp_version_minor == 0x10)
  974. bmplength = 89; /* exact for 5.10h */
  975. else if (bmp_version_minor < 0x14)
  976. bmplength = 118; /* exact for 5.11h */
  977. else if (bmp_version_minor < 0x24)
  978. /*
  979. * Not sure of version where pll limits came in;
  980. * certainly exist by 0x24 though.
  981. */
  982. /* length not exact: this is long enough to get lvds members */
  983. bmplength = 123;
  984. else if (bmp_version_minor < 0x27)
  985. /*
  986. * Length not exact: this is long enough to get pll limit
  987. * member
  988. */
  989. bmplength = 144;
  990. else
  991. /*
  992. * Length not exact: this is long enough to get dual link
  993. * transition clock.
  994. */
  995. bmplength = 158;
  996. /* checksum */
  997. if (nv_cksum(bmp, 8)) {
  998. NV_ERROR(drm, "Bad BMP checksum\n");
  999. return -EINVAL;
  1000. }
  1001. /*
  1002. * Bit 4 seems to indicate either a mobile bios or a quadro card --
  1003. * mobile behaviour consistent (nv11+), quadro only seen nv18gl-nv36gl
  1004. * (not nv10gl), bit 5 that the flat panel tables are present, and
  1005. * bit 6 a tv bios.
  1006. */
  1007. bios->feature_byte = bmp[9];
  1008. if (bmp_version_major < 5 || bmp_version_minor < 0x10)
  1009. bios->old_style_init = true;
  1010. legacy_scripts_offset = 18;
  1011. if (bmp_version_major < 2)
  1012. legacy_scripts_offset -= 4;
  1013. bios->init_script_tbls_ptr = ROM16(bmp[legacy_scripts_offset]);
  1014. bios->extra_init_script_tbl_ptr = ROM16(bmp[legacy_scripts_offset + 2]);
  1015. if (bmp_version_major > 2) { /* appears in BMP 3 */
  1016. bios->legacy.mem_init_tbl_ptr = ROM16(bmp[24]);
  1017. bios->legacy.sdr_seq_tbl_ptr = ROM16(bmp[26]);
  1018. bios->legacy.ddr_seq_tbl_ptr = ROM16(bmp[28]);
  1019. }
  1020. legacy_i2c_offset = 0x48; /* BMP version 2 & 3 */
  1021. if (bmplength > 61)
  1022. legacy_i2c_offset = offset + 54;
  1023. bios->legacy.i2c_indices.crt = bios->data[legacy_i2c_offset];
  1024. bios->legacy.i2c_indices.tv = bios->data[legacy_i2c_offset + 1];
  1025. bios->legacy.i2c_indices.panel = bios->data[legacy_i2c_offset + 2];
  1026. if (bmplength > 74) {
  1027. bios->fmaxvco = ROM32(bmp[67]);
  1028. bios->fminvco = ROM32(bmp[71]);
  1029. }
  1030. if (bmplength > 88)
  1031. parse_script_table_pointers(bios, offset + 75);
  1032. if (bmplength > 94) {
  1033. bios->tmds.output0_script_ptr = ROM16(bmp[89]);
  1034. bios->tmds.output1_script_ptr = ROM16(bmp[91]);
  1035. /*
  1036. * Never observed in use with lvds scripts, but is reused for
  1037. * 18/24 bit panel interface default for EDID equipped panels
  1038. * (if_is_24bit not set directly to avoid any oscillation).
  1039. */
  1040. bios->legacy.lvds_single_a_script_ptr = ROM16(bmp[95]);
  1041. }
  1042. if (bmplength > 108) {
  1043. bios->fp.fptablepointer = ROM16(bmp[105]);
  1044. bios->fp.fpxlatetableptr = ROM16(bmp[107]);
  1045. bios->fp.xlatwidth = 1;
  1046. }
  1047. if (bmplength > 120) {
  1048. bios->fp.lvdsmanufacturerpointer = ROM16(bmp[117]);
  1049. bios->fp.fpxlatemanufacturertableptr = ROM16(bmp[119]);
  1050. }
  1051. #if 0
  1052. if (bmplength > 143)
  1053. bios->pll_limit_tbl_ptr = ROM16(bmp[142]);
  1054. #endif
  1055. if (bmplength > 157)
  1056. bios->fp.duallink_transition_clk = ROM16(bmp[156]) * 10;
  1057. return 0;
  1058. }
  1059. static uint16_t findstr(uint8_t *data, int n, const uint8_t *str, int len)
  1060. {
  1061. int i, j;
  1062. for (i = 0; i <= (n - len); i++) {
  1063. for (j = 0; j < len; j++)
  1064. if (data[i + j] != str[j])
  1065. break;
  1066. if (j == len)
  1067. return i;
  1068. }
  1069. return 0;
  1070. }
  1071. void *
  1072. olddcb_table(struct drm_device *dev)
  1073. {
  1074. struct nouveau_drm *drm = nouveau_drm(dev);
  1075. u8 *dcb = NULL;
  1076. if (drm->client.device.info.family > NV_DEVICE_INFO_V0_TNT)
  1077. dcb = ROMPTR(dev, drm->vbios.data[0x36]);
  1078. if (!dcb) {
  1079. NV_WARN(drm, "No DCB data found in VBIOS\n");
  1080. return NULL;
  1081. }
  1082. if (dcb[0] >= 0x42) {
  1083. NV_WARN(drm, "DCB version 0x%02x unknown\n", dcb[0]);
  1084. return NULL;
  1085. } else
  1086. if (dcb[0] >= 0x30) {
  1087. if (ROM32(dcb[6]) == 0x4edcbdcb)
  1088. return dcb;
  1089. } else
  1090. if (dcb[0] >= 0x20) {
  1091. if (ROM32(dcb[4]) == 0x4edcbdcb)
  1092. return dcb;
  1093. } else
  1094. if (dcb[0] >= 0x15) {
  1095. if (!memcmp(&dcb[-7], "DEV_REC", 7))
  1096. return dcb;
  1097. } else {
  1098. /*
  1099. * v1.4 (some NV15/16, NV11+) seems the same as v1.5, but
  1100. * always has the same single (crt) entry, even when tv-out
  1101. * present, so the conclusion is this version cannot really
  1102. * be used.
  1103. *
  1104. * v1.2 tables (some NV6/10, and NV15+) normally have the
  1105. * same 5 entries, which are not specific to the card and so
  1106. * no use.
  1107. *
  1108. * v1.2 does have an I2C table that read_dcb_i2c_table can
  1109. * handle, but cards exist (nv11 in #14821) with a bad i2c
  1110. * table pointer, so use the indices parsed in
  1111. * parse_bmp_structure.
  1112. *
  1113. * v1.1 (NV5+, maybe some NV4) is entirely unhelpful
  1114. */
  1115. NV_WARN(drm, "No useful DCB data in VBIOS\n");
  1116. return NULL;
  1117. }
  1118. NV_WARN(drm, "DCB header validation failed\n");
  1119. return NULL;
  1120. }
  1121. void *
  1122. olddcb_outp(struct drm_device *dev, u8 idx)
  1123. {
  1124. u8 *dcb = olddcb_table(dev);
  1125. if (dcb && dcb[0] >= 0x30) {
  1126. if (idx < dcb[2])
  1127. return dcb + dcb[1] + (idx * dcb[3]);
  1128. } else
  1129. if (dcb && dcb[0] >= 0x20) {
  1130. u8 *i2c = ROMPTR(dev, dcb[2]);
  1131. u8 *ent = dcb + 8 + (idx * 8);
  1132. if (i2c && ent < i2c)
  1133. return ent;
  1134. } else
  1135. if (dcb && dcb[0] >= 0x15) {
  1136. u8 *i2c = ROMPTR(dev, dcb[2]);
  1137. u8 *ent = dcb + 4 + (idx * 10);
  1138. if (i2c && ent < i2c)
  1139. return ent;
  1140. }
  1141. return NULL;
  1142. }
  1143. int
  1144. olddcb_outp_foreach(struct drm_device *dev, void *data,
  1145. int (*exec)(struct drm_device *, void *, int idx, u8 *outp))
  1146. {
  1147. int ret, idx = -1;
  1148. u8 *outp = NULL;
  1149. while ((outp = olddcb_outp(dev, ++idx))) {
  1150. if (ROM32(outp[0]) == 0x00000000)
  1151. break; /* seen on an NV11 with DCB v1.5 */
  1152. if (ROM32(outp[0]) == 0xffffffff)
  1153. break; /* seen on an NV17 with DCB v2.0 */
  1154. if ((outp[0] & 0x0f) == DCB_OUTPUT_UNUSED)
  1155. continue;
  1156. if ((outp[0] & 0x0f) == DCB_OUTPUT_EOL)
  1157. break;
  1158. ret = exec(dev, data, idx, outp);
  1159. if (ret)
  1160. return ret;
  1161. }
  1162. return 0;
  1163. }
  1164. u8 *
  1165. olddcb_conntab(struct drm_device *dev)
  1166. {
  1167. u8 *dcb = olddcb_table(dev);
  1168. if (dcb && dcb[0] >= 0x30 && dcb[1] >= 0x16) {
  1169. u8 *conntab = ROMPTR(dev, dcb[0x14]);
  1170. if (conntab && conntab[0] >= 0x30 && conntab[0] <= 0x40)
  1171. return conntab;
  1172. }
  1173. return NULL;
  1174. }
  1175. u8 *
  1176. olddcb_conn(struct drm_device *dev, u8 idx)
  1177. {
  1178. u8 *conntab = olddcb_conntab(dev);
  1179. if (conntab && idx < conntab[2])
  1180. return conntab + conntab[1] + (idx * conntab[3]);
  1181. return NULL;
  1182. }
  1183. static struct dcb_output *new_dcb_entry(struct dcb_table *dcb)
  1184. {
  1185. struct dcb_output *entry = &dcb->entry[dcb->entries];
  1186. memset(entry, 0, sizeof(struct dcb_output));
  1187. entry->index = dcb->entries++;
  1188. return entry;
  1189. }
  1190. static void fabricate_dcb_output(struct dcb_table *dcb, int type, int i2c,
  1191. int heads, int or)
  1192. {
  1193. struct dcb_output *entry = new_dcb_entry(dcb);
  1194. entry->type = type;
  1195. entry->i2c_index = i2c;
  1196. entry->heads = heads;
  1197. if (type != DCB_OUTPUT_ANALOG)
  1198. entry->location = !DCB_LOC_ON_CHIP; /* ie OFF CHIP */
  1199. entry->or = or;
  1200. }
  1201. static bool
  1202. parse_dcb20_entry(struct drm_device *dev, struct dcb_table *dcb,
  1203. uint32_t conn, uint32_t conf, struct dcb_output *entry)
  1204. {
  1205. struct nouveau_drm *drm = nouveau_drm(dev);
  1206. int link = 0;
  1207. entry->type = conn & 0xf;
  1208. entry->i2c_index = (conn >> 4) & 0xf;
  1209. entry->heads = (conn >> 8) & 0xf;
  1210. entry->connector = (conn >> 12) & 0xf;
  1211. entry->bus = (conn >> 16) & 0xf;
  1212. entry->location = (conn >> 20) & 0x3;
  1213. entry->or = (conn >> 24) & 0xf;
  1214. switch (entry->type) {
  1215. case DCB_OUTPUT_ANALOG:
  1216. /*
  1217. * Although the rest of a CRT conf dword is usually
  1218. * zeros, mac biosen have stuff there so we must mask
  1219. */
  1220. entry->crtconf.maxfreq = (dcb->version < 0x30) ?
  1221. (conf & 0xffff) * 10 :
  1222. (conf & 0xff) * 10000;
  1223. break;
  1224. case DCB_OUTPUT_LVDS:
  1225. {
  1226. uint32_t mask;
  1227. if (conf & 0x1)
  1228. entry->lvdsconf.use_straps_for_mode = true;
  1229. if (dcb->version < 0x22) {
  1230. mask = ~0xd;
  1231. /*
  1232. * The laptop in bug 14567 lies and claims to not use
  1233. * straps when it does, so assume all DCB 2.0 laptops
  1234. * use straps, until a broken EDID using one is produced
  1235. */
  1236. entry->lvdsconf.use_straps_for_mode = true;
  1237. /*
  1238. * Both 0x4 and 0x8 show up in v2.0 tables; assume they
  1239. * mean the same thing (probably wrong, but might work)
  1240. */
  1241. if (conf & 0x4 || conf & 0x8)
  1242. entry->lvdsconf.use_power_scripts = true;
  1243. } else {
  1244. mask = ~0x7;
  1245. if (conf & 0x2)
  1246. entry->lvdsconf.use_acpi_for_edid = true;
  1247. if (conf & 0x4)
  1248. entry->lvdsconf.use_power_scripts = true;
  1249. entry->lvdsconf.sor.link = (conf & 0x00000030) >> 4;
  1250. link = entry->lvdsconf.sor.link;
  1251. }
  1252. if (conf & mask) {
  1253. /*
  1254. * Until we even try to use these on G8x, it's
  1255. * useless reporting unknown bits. They all are.
  1256. */
  1257. if (dcb->version >= 0x40)
  1258. break;
  1259. NV_ERROR(drm, "Unknown LVDS configuration bits, "
  1260. "please report\n");
  1261. }
  1262. break;
  1263. }
  1264. case DCB_OUTPUT_TV:
  1265. {
  1266. if (dcb->version >= 0x30)
  1267. entry->tvconf.has_component_output = conf & (0x8 << 4);
  1268. else
  1269. entry->tvconf.has_component_output = false;
  1270. break;
  1271. }
  1272. case DCB_OUTPUT_DP:
  1273. entry->dpconf.sor.link = (conf & 0x00000030) >> 4;
  1274. entry->extdev = (conf & 0x0000ff00) >> 8;
  1275. switch ((conf & 0x00e00000) >> 21) {
  1276. case 0:
  1277. entry->dpconf.link_bw = 162000;
  1278. break;
  1279. case 1:
  1280. entry->dpconf.link_bw = 270000;
  1281. break;
  1282. case 2:
  1283. entry->dpconf.link_bw = 540000;
  1284. break;
  1285. case 3:
  1286. default:
  1287. entry->dpconf.link_bw = 810000;
  1288. break;
  1289. }
  1290. switch ((conf & 0x0f000000) >> 24) {
  1291. case 0xf:
  1292. case 0x4:
  1293. entry->dpconf.link_nr = 4;
  1294. break;
  1295. case 0x3:
  1296. case 0x2:
  1297. entry->dpconf.link_nr = 2;
  1298. break;
  1299. default:
  1300. entry->dpconf.link_nr = 1;
  1301. break;
  1302. }
  1303. link = entry->dpconf.sor.link;
  1304. break;
  1305. case DCB_OUTPUT_TMDS:
  1306. if (dcb->version >= 0x40) {
  1307. entry->tmdsconf.sor.link = (conf & 0x00000030) >> 4;
  1308. entry->extdev = (conf & 0x0000ff00) >> 8;
  1309. link = entry->tmdsconf.sor.link;
  1310. }
  1311. else if (dcb->version >= 0x30)
  1312. entry->tmdsconf.slave_addr = (conf & 0x00000700) >> 8;
  1313. else if (dcb->version >= 0x22)
  1314. entry->tmdsconf.slave_addr = (conf & 0x00000070) >> 4;
  1315. break;
  1316. case DCB_OUTPUT_EOL:
  1317. /* weird g80 mobile type that "nv" treats as a terminator */
  1318. dcb->entries--;
  1319. return false;
  1320. default:
  1321. break;
  1322. }
  1323. if (dcb->version < 0x40) {
  1324. /* Normal entries consist of a single bit, but dual link has
  1325. * the next most significant bit set too
  1326. */
  1327. entry->duallink_possible =
  1328. ((1 << (ffs(entry->or) - 1)) * 3 == entry->or);
  1329. } else {
  1330. entry->duallink_possible = (entry->sorconf.link == 3);
  1331. }
  1332. /* unsure what DCB version introduces this, 3.0? */
  1333. if (conf & 0x100000)
  1334. entry->i2c_upper_default = true;
  1335. entry->hasht = (entry->extdev << 8) | (entry->location << 4) |
  1336. entry->type;
  1337. entry->hashm = (entry->heads << 8) | (link << 6) | entry->or;
  1338. return true;
  1339. }
  1340. static bool
  1341. parse_dcb15_entry(struct drm_device *dev, struct dcb_table *dcb,
  1342. uint32_t conn, uint32_t conf, struct dcb_output *entry)
  1343. {
  1344. struct nouveau_drm *drm = nouveau_drm(dev);
  1345. switch (conn & 0x0000000f) {
  1346. case 0:
  1347. entry->type = DCB_OUTPUT_ANALOG;
  1348. break;
  1349. case 1:
  1350. entry->type = DCB_OUTPUT_TV;
  1351. break;
  1352. case 2:
  1353. case 4:
  1354. if (conn & 0x10)
  1355. entry->type = DCB_OUTPUT_LVDS;
  1356. else
  1357. entry->type = DCB_OUTPUT_TMDS;
  1358. break;
  1359. case 3:
  1360. entry->type = DCB_OUTPUT_LVDS;
  1361. break;
  1362. default:
  1363. NV_ERROR(drm, "Unknown DCB type %d\n", conn & 0x0000000f);
  1364. return false;
  1365. }
  1366. entry->i2c_index = (conn & 0x0003c000) >> 14;
  1367. entry->heads = ((conn & 0x001c0000) >> 18) + 1;
  1368. entry->or = entry->heads; /* same as heads, hopefully safe enough */
  1369. entry->location = (conn & 0x01e00000) >> 21;
  1370. entry->bus = (conn & 0x0e000000) >> 25;
  1371. entry->duallink_possible = false;
  1372. switch (entry->type) {
  1373. case DCB_OUTPUT_ANALOG:
  1374. entry->crtconf.maxfreq = (conf & 0xffff) * 10;
  1375. break;
  1376. case DCB_OUTPUT_TV:
  1377. entry->tvconf.has_component_output = false;
  1378. break;
  1379. case DCB_OUTPUT_LVDS:
  1380. if ((conn & 0x00003f00) >> 8 != 0x10)
  1381. entry->lvdsconf.use_straps_for_mode = true;
  1382. entry->lvdsconf.use_power_scripts = true;
  1383. break;
  1384. default:
  1385. break;
  1386. }
  1387. return true;
  1388. }
  1389. static
  1390. void merge_like_dcb_entries(struct drm_device *dev, struct dcb_table *dcb)
  1391. {
  1392. /*
  1393. * DCB v2.0 lists each output combination separately.
  1394. * Here we merge compatible entries to have fewer outputs, with
  1395. * more options
  1396. */
  1397. struct nouveau_drm *drm = nouveau_drm(dev);
  1398. int i, newentries = 0;
  1399. for (i = 0; i < dcb->entries; i++) {
  1400. struct dcb_output *ient = &dcb->entry[i];
  1401. int j;
  1402. for (j = i + 1; j < dcb->entries; j++) {
  1403. struct dcb_output *jent = &dcb->entry[j];
  1404. if (jent->type == 100) /* already merged entry */
  1405. continue;
  1406. /* merge heads field when all other fields the same */
  1407. if (jent->i2c_index == ient->i2c_index &&
  1408. jent->type == ient->type &&
  1409. jent->location == ient->location &&
  1410. jent->or == ient->or) {
  1411. NV_INFO(drm, "Merging DCB entries %d and %d\n",
  1412. i, j);
  1413. ient->heads |= jent->heads;
  1414. jent->type = 100; /* dummy value */
  1415. }
  1416. }
  1417. }
  1418. /* Compact entries merged into others out of dcb */
  1419. for (i = 0; i < dcb->entries; i++) {
  1420. if (dcb->entry[i].type == 100)
  1421. continue;
  1422. if (newentries != i) {
  1423. dcb->entry[newentries] = dcb->entry[i];
  1424. dcb->entry[newentries].index = newentries;
  1425. }
  1426. newentries++;
  1427. }
  1428. dcb->entries = newentries;
  1429. }
  1430. static bool
  1431. apply_dcb_encoder_quirks(struct drm_device *dev, int idx, u32 *conn, u32 *conf)
  1432. {
  1433. struct nouveau_drm *drm = nouveau_drm(dev);
  1434. struct dcb_table *dcb = &drm->vbios.dcb;
  1435. /* Dell Precision M6300
  1436. * DCB entry 2: 02025312 00000010
  1437. * DCB entry 3: 02026312 00000020
  1438. *
  1439. * Identical, except apparently a different connector on a
  1440. * different SOR link. Not a clue how we're supposed to know
  1441. * which one is in use if it even shares an i2c line...
  1442. *
  1443. * Ignore the connector on the second SOR link to prevent
  1444. * nasty problems until this is sorted (assuming it's not a
  1445. * VBIOS bug).
  1446. */
  1447. if (nv_match_device(dev, 0x040d, 0x1028, 0x019b)) {
  1448. if (*conn == 0x02026312 && *conf == 0x00000020)
  1449. return false;
  1450. }
  1451. /* GeForce3 Ti 200
  1452. *
  1453. * DCB reports an LVDS output that should be TMDS:
  1454. * DCB entry 1: f2005014 ffffffff
  1455. */
  1456. if (nv_match_device(dev, 0x0201, 0x1462, 0x8851)) {
  1457. if (*conn == 0xf2005014 && *conf == 0xffffffff) {
  1458. fabricate_dcb_output(dcb, DCB_OUTPUT_TMDS, 1, 1, 1);
  1459. return false;
  1460. }
  1461. }
  1462. /* XFX GT-240X-YA
  1463. *
  1464. * So many things wrong here, replace the entire encoder table..
  1465. */
  1466. if (nv_match_device(dev, 0x0ca3, 0x1682, 0x3003)) {
  1467. if (idx == 0) {
  1468. *conn = 0x02001300; /* VGA, connector 1 */
  1469. *conf = 0x00000028;
  1470. } else
  1471. if (idx == 1) {
  1472. *conn = 0x01010312; /* DVI, connector 0 */
  1473. *conf = 0x00020030;
  1474. } else
  1475. if (idx == 2) {
  1476. *conn = 0x01010310; /* VGA, connector 0 */
  1477. *conf = 0x00000028;
  1478. } else
  1479. if (idx == 3) {
  1480. *conn = 0x02022362; /* HDMI, connector 2 */
  1481. *conf = 0x00020010;
  1482. } else {
  1483. *conn = 0x0000000e; /* EOL */
  1484. *conf = 0x00000000;
  1485. }
  1486. }
  1487. /* Some other twisted XFX board (rhbz#694914)
  1488. *
  1489. * The DVI/VGA encoder combo that's supposed to represent the
  1490. * DVI-I connector actually point at two different ones, and
  1491. * the HDMI connector ends up paired with the VGA instead.
  1492. *
  1493. * Connector table is missing anything for VGA at all, pointing it
  1494. * an invalid conntab entry 2 so we figure it out ourself.
  1495. */
  1496. if (nv_match_device(dev, 0x0615, 0x1682, 0x2605)) {
  1497. if (idx == 0) {
  1498. *conn = 0x02002300; /* VGA, connector 2 */
  1499. *conf = 0x00000028;
  1500. } else
  1501. if (idx == 1) {
  1502. *conn = 0x01010312; /* DVI, connector 0 */
  1503. *conf = 0x00020030;
  1504. } else
  1505. if (idx == 2) {
  1506. *conn = 0x04020310; /* VGA, connector 0 */
  1507. *conf = 0x00000028;
  1508. } else
  1509. if (idx == 3) {
  1510. *conn = 0x02021322; /* HDMI, connector 1 */
  1511. *conf = 0x00020010;
  1512. } else {
  1513. *conn = 0x0000000e; /* EOL */
  1514. *conf = 0x00000000;
  1515. }
  1516. }
  1517. /* fdo#50830: connector indices for VGA and DVI-I are backwards */
  1518. if (nv_match_device(dev, 0x0421, 0x3842, 0xc793)) {
  1519. if (idx == 0 && *conn == 0x02000300)
  1520. *conn = 0x02011300;
  1521. else
  1522. if (idx == 1 && *conn == 0x04011310)
  1523. *conn = 0x04000310;
  1524. else
  1525. if (idx == 2 && *conn == 0x02011312)
  1526. *conn = 0x02000312;
  1527. }
  1528. return true;
  1529. }
  1530. static void
  1531. fabricate_dcb_encoder_table(struct drm_device *dev, struct nvbios *bios)
  1532. {
  1533. struct dcb_table *dcb = &bios->dcb;
  1534. int all_heads = (nv_two_heads(dev) ? 3 : 1);
  1535. #ifdef __powerpc__
  1536. /* Apple iMac G4 NV17 */
  1537. if (of_machine_is_compatible("PowerMac4,5")) {
  1538. fabricate_dcb_output(dcb, DCB_OUTPUT_TMDS, 0, all_heads, 1);
  1539. fabricate_dcb_output(dcb, DCB_OUTPUT_ANALOG, 1, all_heads, 2);
  1540. return;
  1541. }
  1542. #endif
  1543. /* Make up some sane defaults */
  1544. fabricate_dcb_output(dcb, DCB_OUTPUT_ANALOG,
  1545. bios->legacy.i2c_indices.crt, 1, 1);
  1546. if (nv04_tv_identify(dev, bios->legacy.i2c_indices.tv) >= 0)
  1547. fabricate_dcb_output(dcb, DCB_OUTPUT_TV,
  1548. bios->legacy.i2c_indices.tv,
  1549. all_heads, 0);
  1550. else if (bios->tmds.output0_script_ptr ||
  1551. bios->tmds.output1_script_ptr)
  1552. fabricate_dcb_output(dcb, DCB_OUTPUT_TMDS,
  1553. bios->legacy.i2c_indices.panel,
  1554. all_heads, 1);
  1555. }
  1556. static int
  1557. parse_dcb_entry(struct drm_device *dev, void *data, int idx, u8 *outp)
  1558. {
  1559. struct nouveau_drm *drm = nouveau_drm(dev);
  1560. struct dcb_table *dcb = &drm->vbios.dcb;
  1561. u32 conf = (dcb->version >= 0x20) ? ROM32(outp[4]) : ROM32(outp[6]);
  1562. u32 conn = ROM32(outp[0]);
  1563. bool ret;
  1564. if (apply_dcb_encoder_quirks(dev, idx, &conn, &conf)) {
  1565. struct dcb_output *entry = new_dcb_entry(dcb);
  1566. NV_INFO(drm, "DCB outp %02d: %08x %08x\n", idx, conn, conf);
  1567. if (dcb->version >= 0x20)
  1568. ret = parse_dcb20_entry(dev, dcb, conn, conf, entry);
  1569. else
  1570. ret = parse_dcb15_entry(dev, dcb, conn, conf, entry);
  1571. if (!ret)
  1572. return 1; /* stop parsing */
  1573. /* Ignore the I2C index for on-chip TV-out, as there
  1574. * are cards with bogus values (nv31m in bug 23212),
  1575. * and it's otherwise useless.
  1576. */
  1577. if (entry->type == DCB_OUTPUT_TV &&
  1578. entry->location == DCB_LOC_ON_CHIP)
  1579. entry->i2c_index = 0x0f;
  1580. }
  1581. return 0;
  1582. }
  1583. static void
  1584. dcb_fake_connectors(struct nvbios *bios)
  1585. {
  1586. struct dcb_table *dcbt = &bios->dcb;
  1587. u8 map[16] = { };
  1588. int i, idx = 0;
  1589. /* heuristic: if we ever get a non-zero connector field, assume
  1590. * that all the indices are valid and we don't need fake them.
  1591. *
  1592. * and, as usual, a blacklist of boards with bad bios data..
  1593. */
  1594. if (!nv_match_device(bios->dev, 0x0392, 0x107d, 0x20a2)) {
  1595. for (i = 0; i < dcbt->entries; i++) {
  1596. if (dcbt->entry[i].connector)
  1597. return;
  1598. }
  1599. }
  1600. /* no useful connector info available, we need to make it up
  1601. * ourselves. the rule here is: anything on the same i2c bus
  1602. * is considered to be on the same connector. any output
  1603. * without an associated i2c bus is assigned its own unique
  1604. * connector index.
  1605. */
  1606. for (i = 0; i < dcbt->entries; i++) {
  1607. u8 i2c = dcbt->entry[i].i2c_index;
  1608. if (i2c == 0x0f) {
  1609. dcbt->entry[i].connector = idx++;
  1610. } else {
  1611. if (!map[i2c])
  1612. map[i2c] = ++idx;
  1613. dcbt->entry[i].connector = map[i2c] - 1;
  1614. }
  1615. }
  1616. /* if we created more than one connector, destroy the connector
  1617. * table - just in case it has random, rather than stub, entries.
  1618. */
  1619. if (i > 1) {
  1620. u8 *conntab = olddcb_conntab(bios->dev);
  1621. if (conntab)
  1622. conntab[0] = 0x00;
  1623. }
  1624. }
  1625. static int
  1626. parse_dcb_table(struct drm_device *dev, struct nvbios *bios)
  1627. {
  1628. struct nouveau_drm *drm = nouveau_drm(dev);
  1629. struct dcb_table *dcb = &bios->dcb;
  1630. u8 *dcbt, *conn;
  1631. int idx;
  1632. dcbt = olddcb_table(dev);
  1633. if (!dcbt) {
  1634. /* handle pre-DCB boards */
  1635. if (bios->type == NVBIOS_BMP) {
  1636. fabricate_dcb_encoder_table(dev, bios);
  1637. return 0;
  1638. }
  1639. return -EINVAL;
  1640. }
  1641. NV_INFO(drm, "DCB version %d.%d\n", dcbt[0] >> 4, dcbt[0] & 0xf);
  1642. dcb->version = dcbt[0];
  1643. olddcb_outp_foreach(dev, NULL, parse_dcb_entry);
  1644. /*
  1645. * apart for v2.1+ not being known for requiring merging, this
  1646. * guarantees dcbent->index is the index of the entry in the rom image
  1647. */
  1648. if (dcb->version < 0x21)
  1649. merge_like_dcb_entries(dev, dcb);
  1650. /* dump connector table entries to log, if any exist */
  1651. idx = -1;
  1652. while ((conn = olddcb_conn(dev, ++idx))) {
  1653. if (conn[0] != 0xff) {
  1654. if (olddcb_conntab(dev)[3] < 4)
  1655. NV_INFO(drm, "DCB conn %02d: %04x\n",
  1656. idx, ROM16(conn[0]));
  1657. else
  1658. NV_INFO(drm, "DCB conn %02d: %08x\n",
  1659. idx, ROM32(conn[0]));
  1660. }
  1661. }
  1662. dcb_fake_connectors(bios);
  1663. return 0;
  1664. }
  1665. static int load_nv17_hwsq_ucode_entry(struct drm_device *dev, struct nvbios *bios, uint16_t hwsq_offset, int entry)
  1666. {
  1667. /*
  1668. * The header following the "HWSQ" signature has the number of entries,
  1669. * and the entry size
  1670. *
  1671. * An entry consists of a dword to write to the sequencer control reg
  1672. * (0x00001304), followed by the ucode bytes, written sequentially,
  1673. * starting at reg 0x00001400
  1674. */
  1675. struct nouveau_drm *drm = nouveau_drm(dev);
  1676. struct nvif_object *device = &drm->client.device.object;
  1677. uint8_t bytes_to_write;
  1678. uint16_t hwsq_entry_offset;
  1679. int i;
  1680. if (bios->data[hwsq_offset] <= entry) {
  1681. NV_ERROR(drm, "Too few entries in HW sequencer table for "
  1682. "requested entry\n");
  1683. return -ENOENT;
  1684. }
  1685. bytes_to_write = bios->data[hwsq_offset + 1];
  1686. if (bytes_to_write != 36) {
  1687. NV_ERROR(drm, "Unknown HW sequencer entry size\n");
  1688. return -EINVAL;
  1689. }
  1690. NV_INFO(drm, "Loading NV17 power sequencing microcode\n");
  1691. hwsq_entry_offset = hwsq_offset + 2 + entry * bytes_to_write;
  1692. /* set sequencer control */
  1693. nvif_wr32(device, 0x00001304, ROM32(bios->data[hwsq_entry_offset]));
  1694. bytes_to_write -= 4;
  1695. /* write ucode */
  1696. for (i = 0; i < bytes_to_write; i += 4)
  1697. nvif_wr32(device, 0x00001400 + i, ROM32(bios->data[hwsq_entry_offset + i + 4]));
  1698. /* twiddle NV_PBUS_DEBUG_4 */
  1699. nvif_wr32(device, NV_PBUS_DEBUG_4, nvif_rd32(device, NV_PBUS_DEBUG_4) | 0x18);
  1700. return 0;
  1701. }
  1702. static int load_nv17_hw_sequencer_ucode(struct drm_device *dev,
  1703. struct nvbios *bios)
  1704. {
  1705. /*
  1706. * BMP based cards, from NV17, need a microcode loading to correctly
  1707. * control the GPIO etc for LVDS panels
  1708. *
  1709. * BIT based cards seem to do this directly in the init scripts
  1710. *
  1711. * The microcode entries are found by the "HWSQ" signature.
  1712. */
  1713. static const uint8_t hwsq_signature[] = { 'H', 'W', 'S', 'Q' };
  1714. const int sz = sizeof(hwsq_signature);
  1715. int hwsq_offset;
  1716. hwsq_offset = findstr(bios->data, bios->length, hwsq_signature, sz);
  1717. if (!hwsq_offset)
  1718. return 0;
  1719. /* always use entry 0? */
  1720. return load_nv17_hwsq_ucode_entry(dev, bios, hwsq_offset + sz, 0);
  1721. }
  1722. uint8_t *nouveau_bios_embedded_edid(struct drm_device *dev)
  1723. {
  1724. struct nouveau_drm *drm = nouveau_drm(dev);
  1725. struct nvbios *bios = &drm->vbios;
  1726. static const uint8_t edid_sig[] = {
  1727. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
  1728. uint16_t offset = 0;
  1729. uint16_t newoffset;
  1730. int searchlen = NV_PROM_SIZE;
  1731. if (bios->fp.edid)
  1732. return bios->fp.edid;
  1733. while (searchlen) {
  1734. newoffset = findstr(&bios->data[offset], searchlen,
  1735. edid_sig, 8);
  1736. if (!newoffset)
  1737. return NULL;
  1738. offset += newoffset;
  1739. if (!nv_cksum(&bios->data[offset], EDID1_LEN))
  1740. break;
  1741. searchlen -= offset;
  1742. offset++;
  1743. }
  1744. NV_INFO(drm, "Found EDID in BIOS\n");
  1745. return bios->fp.edid = &bios->data[offset];
  1746. }
  1747. static bool NVInitVBIOS(struct drm_device *dev)
  1748. {
  1749. struct nouveau_drm *drm = nouveau_drm(dev);
  1750. struct nvkm_bios *bios = nvxx_bios(&drm->client.device);
  1751. struct nvbios *legacy = &drm->vbios;
  1752. memset(legacy, 0, sizeof(struct nvbios));
  1753. spin_lock_init(&legacy->lock);
  1754. legacy->dev = dev;
  1755. legacy->data = bios->data;
  1756. legacy->length = bios->size;
  1757. legacy->major_version = bios->version.major;
  1758. legacy->chip_version = bios->version.chip;
  1759. if (bios->bit_offset) {
  1760. legacy->type = NVBIOS_BIT;
  1761. legacy->offset = bios->bit_offset;
  1762. return !parse_bit_structure(legacy, legacy->offset + 6);
  1763. } else
  1764. if (bios->bmp_offset) {
  1765. legacy->type = NVBIOS_BMP;
  1766. legacy->offset = bios->bmp_offset;
  1767. return !parse_bmp_structure(dev, legacy, legacy->offset);
  1768. }
  1769. return false;
  1770. }
  1771. int
  1772. nouveau_run_vbios_init(struct drm_device *dev)
  1773. {
  1774. struct nouveau_drm *drm = nouveau_drm(dev);
  1775. struct nvbios *bios = &drm->vbios;
  1776. int ret = 0;
  1777. /* Reset the BIOS head to 0. */
  1778. bios->state.crtchead = 0;
  1779. if (bios->major_version < 5) /* BMP only */
  1780. load_nv17_hw_sequencer_ucode(dev, bios);
  1781. if (bios->execute) {
  1782. bios->fp.last_script_invoc = 0;
  1783. bios->fp.lvds_init_run = false;
  1784. }
  1785. return ret;
  1786. }
  1787. static bool
  1788. nouveau_bios_posted(struct drm_device *dev)
  1789. {
  1790. struct nouveau_drm *drm = nouveau_drm(dev);
  1791. unsigned htotal;
  1792. if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA)
  1793. return true;
  1794. htotal = NVReadVgaCrtc(dev, 0, 0x06);
  1795. htotal |= (NVReadVgaCrtc(dev, 0, 0x07) & 0x01) << 8;
  1796. htotal |= (NVReadVgaCrtc(dev, 0, 0x07) & 0x20) << 4;
  1797. htotal |= (NVReadVgaCrtc(dev, 0, 0x25) & 0x01) << 10;
  1798. htotal |= (NVReadVgaCrtc(dev, 0, 0x41) & 0x01) << 11;
  1799. return (htotal != 0);
  1800. }
  1801. int
  1802. nouveau_bios_init(struct drm_device *dev)
  1803. {
  1804. struct nouveau_drm *drm = nouveau_drm(dev);
  1805. struct nvbios *bios = &drm->vbios;
  1806. int ret;
  1807. /* only relevant for PCI devices */
  1808. if (!dev->pdev)
  1809. return 0;
  1810. if (!NVInitVBIOS(dev))
  1811. return -ENODEV;
  1812. ret = parse_dcb_table(dev, bios);
  1813. if (ret)
  1814. return ret;
  1815. if (!bios->major_version) /* we don't run version 0 bios */
  1816. return 0;
  1817. /* init script execution disabled */
  1818. bios->execute = false;
  1819. /* ... unless card isn't POSTed already */
  1820. if (!nouveau_bios_posted(dev)) {
  1821. NV_INFO(drm, "Adaptor not initialised, "
  1822. "running VBIOS init tables.\n");
  1823. bios->execute = true;
  1824. }
  1825. ret = nouveau_run_vbios_init(dev);
  1826. if (ret)
  1827. return ret;
  1828. /* feature_byte on BMP is poor, but init always sets CR4B */
  1829. if (bios->major_version < 5)
  1830. bios->is_mobile = NVReadVgaCrtc(dev, 0, NV_CIO_CRE_4B) & 0x40;
  1831. /* all BIT systems need p_f_m_t for digital_min_front_porch */
  1832. if (bios->is_mobile || bios->major_version >= 5)
  1833. ret = parse_fp_mode_table(dev, bios);
  1834. /* allow subsequent scripts to execute */
  1835. bios->execute = true;
  1836. return 0;
  1837. }
  1838. void
  1839. nouveau_bios_takedown(struct drm_device *dev)
  1840. {
  1841. }