nouveau_bios.c 61 KB

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