nouveau_bios.c 61 KB

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