nouveau_bios.c 66 KB

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