nouveau_bios.c 67 KB

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