2
0

edid-generate.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. * QEMU EDID generator.
  3. *
  4. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  5. * See the COPYING file in the top-level directory.
  6. */
  7. #include "qemu/osdep.h"
  8. #include "qemu/bswap.h"
  9. #include "hw/display/edid.h"
  10. static const struct edid_mode {
  11. uint32_t xres;
  12. uint32_t yres;
  13. uint32_t byte;
  14. uint32_t xtra3;
  15. uint32_t bit;
  16. uint32_t dta;
  17. } modes[] = {
  18. /* dea/dta extension timings (all @ 50 Hz) */
  19. { .xres = 5120, .yres = 2160, .dta = 125 },
  20. { .xres = 4096, .yres = 2160, .dta = 101 },
  21. { .xres = 3840, .yres = 2160, .dta = 96 },
  22. { .xres = 2560, .yres = 1080, .dta = 89 },
  23. { .xres = 2048, .yres = 1152 },
  24. { .xres = 1920, .yres = 1080, .dta = 31 },
  25. /* dea/dta extension timings (all @ 60 Hz) */
  26. { .xres = 3840, .yres = 2160, .dta = 97 },
  27. /* additional standard timings 3 (all @ 60Hz) */
  28. { .xres = 1920, .yres = 1200, .xtra3 = 10, .bit = 0 },
  29. { .xres = 1600, .yres = 1200, .xtra3 = 9, .bit = 2 },
  30. { .xres = 1680, .yres = 1050, .xtra3 = 9, .bit = 5 },
  31. { .xres = 1440, .yres = 900, .xtra3 = 8, .bit = 5 },
  32. { .xres = 1280, .yres = 1024, .xtra3 = 7, .bit = 1 },
  33. { .xres = 1280, .yres = 960, .xtra3 = 7, .bit = 3 },
  34. { .xres = 1280, .yres = 768, .xtra3 = 7, .bit = 6 },
  35. { .xres = 1920, .yres = 1440, .xtra3 = 11, .bit = 5 },
  36. { .xres = 1856, .yres = 1392, .xtra3 = 10, .bit = 3 },
  37. { .xres = 1792, .yres = 1344, .xtra3 = 10, .bit = 5 },
  38. { .xres = 1440, .yres = 1050, .xtra3 = 8, .bit = 1 },
  39. { .xres = 1360, .yres = 768, .xtra3 = 8, .bit = 7 },
  40. /* established timings (all @ 60Hz) */
  41. { .xres = 1024, .yres = 768, .byte = 36, .bit = 3 },
  42. { .xres = 800, .yres = 600, .byte = 35, .bit = 0 },
  43. { .xres = 640, .yres = 480, .byte = 35, .bit = 5 },
  44. };
  45. typedef struct Timings {
  46. uint32_t xfront;
  47. uint32_t xsync;
  48. uint32_t xblank;
  49. uint32_t yfront;
  50. uint32_t ysync;
  51. uint32_t yblank;
  52. uint64_t clock;
  53. } Timings;
  54. static void generate_timings(Timings *timings, uint32_t refresh_rate,
  55. uint32_t xres, uint32_t yres)
  56. {
  57. /* pull some realistic looking timings out of thin air */
  58. timings->xfront = xres * 25 / 100;
  59. timings->xsync = xres * 3 / 100;
  60. timings->xblank = xres * 35 / 100;
  61. timings->yfront = yres * 5 / 1000;
  62. timings->ysync = yres * 5 / 1000;
  63. timings->yblank = yres * 35 / 1000;
  64. timings->clock = ((uint64_t)refresh_rate *
  65. (xres + timings->xblank) *
  66. (yres + timings->yblank)) / 10000000;
  67. }
  68. static void edid_ext_dta(uint8_t *dta)
  69. {
  70. dta[0] = 0x02;
  71. dta[1] = 0x03;
  72. dta[2] = 0x05;
  73. dta[3] = 0x00;
  74. /* video data block */
  75. dta[4] = 0x40;
  76. }
  77. static void edid_ext_dta_mode(uint8_t *dta, uint8_t nr)
  78. {
  79. dta[dta[2]] = nr;
  80. dta[2]++;
  81. dta[4]++;
  82. }
  83. static int edid_std_mode(uint8_t *mode, uint32_t xres, uint32_t yres)
  84. {
  85. uint32_t aspect;
  86. if (xres == 0 || yres == 0) {
  87. mode[0] = 0x01;
  88. mode[1] = 0x01;
  89. return 0;
  90. } else if (xres * 10 == yres * 16) {
  91. aspect = 0;
  92. } else if (xres * 3 == yres * 4) {
  93. aspect = 1;
  94. } else if (xres * 4 == yres * 5) {
  95. aspect = 2;
  96. } else if (xres * 9 == yres * 16) {
  97. aspect = 3;
  98. } else {
  99. return -1;
  100. }
  101. if ((xres / 8) - 31 > 255) {
  102. return -1;
  103. }
  104. mode[0] = (xres / 8) - 31;
  105. mode[1] = ((aspect << 6) | (60 - 60));
  106. return 0;
  107. }
  108. static void edid_fill_modes(uint8_t *edid, uint8_t *xtra3, uint8_t *dta,
  109. uint32_t maxx, uint32_t maxy)
  110. {
  111. const struct edid_mode *mode;
  112. int std = 38;
  113. int rc, i;
  114. for (i = 0; i < ARRAY_SIZE(modes); i++) {
  115. mode = modes + i;
  116. if ((maxx && mode->xres > maxx) ||
  117. (maxy && mode->yres > maxy)) {
  118. continue;
  119. }
  120. if (mode->byte) {
  121. edid[mode->byte] |= (1 << mode->bit);
  122. } else if (std < 54) {
  123. rc = edid_std_mode(edid + std, mode->xres, mode->yres);
  124. if (rc == 0) {
  125. std += 2;
  126. }
  127. } else if (mode->xtra3 && xtra3) {
  128. xtra3[mode->xtra3] |= (1 << mode->bit);
  129. }
  130. if (dta && mode->dta) {
  131. edid_ext_dta_mode(dta, mode->dta);
  132. }
  133. }
  134. while (std < 54) {
  135. edid_std_mode(edid + std, 0, 0);
  136. std += 2;
  137. }
  138. }
  139. static void edid_checksum(uint8_t *edid, size_t len)
  140. {
  141. uint32_t sum = 0;
  142. int i;
  143. for (i = 0; i < len; i++) {
  144. sum += edid[i];
  145. }
  146. sum &= 0xff;
  147. if (sum) {
  148. edid[len] = 0x100 - sum;
  149. }
  150. }
  151. static uint8_t *edid_desc_next(uint8_t *edid, uint8_t *dta, uint8_t *desc)
  152. {
  153. if (desc == NULL) {
  154. return NULL;
  155. }
  156. if (desc + 18 + 18 < edid + 127) {
  157. return desc + 18;
  158. }
  159. if (dta) {
  160. if (desc < edid + 127) {
  161. return dta + dta[2];
  162. }
  163. if (desc + 18 + 18 < dta + 127) {
  164. return desc + 18;
  165. }
  166. }
  167. return NULL;
  168. }
  169. static void edid_desc_type(uint8_t *desc, uint8_t type)
  170. {
  171. desc[0] = 0;
  172. desc[1] = 0;
  173. desc[2] = 0;
  174. desc[3] = type;
  175. desc[4] = 0;
  176. }
  177. static void edid_desc_text(uint8_t *desc, uint8_t type,
  178. const char *text)
  179. {
  180. size_t len;
  181. edid_desc_type(desc, type);
  182. memset(desc + 5, ' ', 13);
  183. len = strlen(text);
  184. if (len > 12) {
  185. len = 12;
  186. }
  187. memcpy(desc + 5, text, len);
  188. desc[5 + len] = '\n';
  189. }
  190. static void edid_desc_ranges(uint8_t *desc)
  191. {
  192. edid_desc_type(desc, 0xfd);
  193. /* vertical (50 -> 125 Hz) */
  194. desc[5] = 50;
  195. desc[6] = 125;
  196. /* horizontal (30 -> 160 kHz) */
  197. desc[7] = 30;
  198. desc[8] = 160;
  199. /* max dot clock (2550 MHz) */
  200. desc[9] = 2550 / 10;
  201. /* no extended timing information */
  202. desc[10] = 0x01;
  203. /* padding */
  204. desc[11] = '\n';
  205. memset(desc + 12, ' ', 6);
  206. }
  207. /* additional standard timings 3 */
  208. static void edid_desc_xtra3_std(uint8_t *desc)
  209. {
  210. edid_desc_type(desc, 0xf7);
  211. desc[5] = 10;
  212. }
  213. static void edid_desc_dummy(uint8_t *desc)
  214. {
  215. edid_desc_type(desc, 0x10);
  216. }
  217. static void edid_desc_timing(uint8_t *desc, const Timings *timings,
  218. uint32_t xres, uint32_t yres,
  219. uint32_t xmm, uint32_t ymm)
  220. {
  221. stw_le_p(desc, timings->clock);
  222. desc[2] = xres & 0xff;
  223. desc[3] = timings->xblank & 0xff;
  224. desc[4] = (((xres & 0xf00) >> 4) |
  225. ((timings->xblank & 0xf00) >> 8));
  226. desc[5] = yres & 0xff;
  227. desc[6] = timings->yblank & 0xff;
  228. desc[7] = (((yres & 0xf00) >> 4) |
  229. ((timings->yblank & 0xf00) >> 8));
  230. desc[8] = timings->xfront & 0xff;
  231. desc[9] = timings->xsync & 0xff;
  232. desc[10] = (((timings->yfront & 0x00f) << 4) |
  233. ((timings->ysync & 0x00f) << 0));
  234. desc[11] = (((timings->xfront & 0x300) >> 2) |
  235. ((timings->xsync & 0x300) >> 4) |
  236. ((timings->yfront & 0x030) >> 2) |
  237. ((timings->ysync & 0x030) >> 4));
  238. desc[12] = xmm & 0xff;
  239. desc[13] = ymm & 0xff;
  240. desc[14] = (((xmm & 0xf00) >> 4) |
  241. ((ymm & 0xf00) >> 8));
  242. desc[17] = 0x18;
  243. }
  244. static uint32_t edid_to_10bit(float value)
  245. {
  246. return (uint32_t)(value * 1024 + 0.5);
  247. }
  248. static void edid_colorspace(uint8_t *edid,
  249. float rx, float ry,
  250. float gx, float gy,
  251. float bx, float by,
  252. float wx, float wy)
  253. {
  254. uint32_t red_x = edid_to_10bit(rx);
  255. uint32_t red_y = edid_to_10bit(ry);
  256. uint32_t green_x = edid_to_10bit(gx);
  257. uint32_t green_y = edid_to_10bit(gy);
  258. uint32_t blue_x = edid_to_10bit(bx);
  259. uint32_t blue_y = edid_to_10bit(by);
  260. uint32_t white_x = edid_to_10bit(wx);
  261. uint32_t white_y = edid_to_10bit(wy);
  262. edid[25] = (((red_x & 0x03) << 6) |
  263. ((red_y & 0x03) << 4) |
  264. ((green_x & 0x03) << 2) |
  265. ((green_y & 0x03) << 0));
  266. edid[26] = (((blue_x & 0x03) << 6) |
  267. ((blue_y & 0x03) << 4) |
  268. ((white_x & 0x03) << 2) |
  269. ((white_y & 0x03) << 0));
  270. edid[27] = red_x >> 2;
  271. edid[28] = red_y >> 2;
  272. edid[29] = green_x >> 2;
  273. edid[30] = green_y >> 2;
  274. edid[31] = blue_x >> 2;
  275. edid[32] = blue_y >> 2;
  276. edid[33] = white_x >> 2;
  277. edid[34] = white_y >> 2;
  278. }
  279. static uint32_t qemu_edid_dpi_from_mm(uint32_t mm, uint32_t res)
  280. {
  281. return res * 254 / 10 / mm;
  282. }
  283. uint32_t qemu_edid_dpi_to_mm(uint32_t dpi, uint32_t res)
  284. {
  285. return res * 254 / 10 / dpi;
  286. }
  287. static void init_displayid(uint8_t *did)
  288. {
  289. did[0] = 0x70; /* display id extension */
  290. did[1] = 0x13; /* version 1.3 */
  291. did[2] = 4; /* length */
  292. did[3] = 0x03; /* product type (0x03 == standalone display device) */
  293. edid_checksum(did + 1, did[2] + 4);
  294. }
  295. static void qemu_displayid_generate(uint8_t *did, const Timings *timings,
  296. uint32_t xres, uint32_t yres,
  297. uint32_t xmm, uint32_t ymm)
  298. {
  299. did[0] = 0x70; /* display id extension */
  300. did[1] = 0x13; /* version 1.3 */
  301. did[2] = 23; /* length */
  302. did[3] = 0x03; /* product type (0x03 == standalone display device) */
  303. did[5] = 0x03; /* Detailed Timings Data Block */
  304. did[6] = 0x00; /* revision */
  305. did[7] = 0x14; /* block length */
  306. did[8] = timings->clock & 0xff;
  307. did[9] = (timings->clock & 0xff00) >> 8;
  308. did[10] = (timings->clock & 0xff0000) >> 16;
  309. did[11] = 0x88; /* leave aspect ratio undefined */
  310. stw_le_p(did + 12, 0xffff & (xres - 1));
  311. stw_le_p(did + 14, 0xffff & (timings->xblank - 1));
  312. stw_le_p(did + 16, 0xffff & (timings->xfront - 1));
  313. stw_le_p(did + 18, 0xffff & (timings->xsync - 1));
  314. stw_le_p(did + 20, 0xffff & (yres - 1));
  315. stw_le_p(did + 22, 0xffff & (timings->yblank - 1));
  316. stw_le_p(did + 24, 0xffff & (timings->yfront - 1));
  317. stw_le_p(did + 26, 0xffff & (timings->ysync - 1));
  318. edid_checksum(did + 1, did[2] + 4);
  319. }
  320. void qemu_edid_generate(uint8_t *edid, size_t size,
  321. qemu_edid_info *info)
  322. {
  323. Timings timings;
  324. uint8_t *desc = edid + 54;
  325. uint8_t *xtra3 = NULL;
  326. uint8_t *dta = NULL;
  327. uint8_t *did = NULL;
  328. uint32_t width_mm, height_mm;
  329. uint32_t refresh_rate = info->refresh_rate ? info->refresh_rate : 75000;
  330. uint32_t dpi = 100; /* if no width_mm/height_mm */
  331. uint32_t large_screen = 0;
  332. /* =============== set defaults =============== */
  333. if (!info->vendor || strlen(info->vendor) != 3) {
  334. info->vendor = "RHT";
  335. }
  336. if (!info->name) {
  337. info->name = "QEMU Monitor";
  338. }
  339. if (!info->prefx) {
  340. info->prefx = 1280;
  341. }
  342. if (!info->prefy) {
  343. info->prefy = 800;
  344. }
  345. if (info->width_mm && info->height_mm) {
  346. width_mm = info->width_mm;
  347. height_mm = info->height_mm;
  348. dpi = qemu_edid_dpi_from_mm(width_mm, info->prefx);
  349. } else {
  350. width_mm = qemu_edid_dpi_to_mm(dpi, info->prefx);
  351. height_mm = qemu_edid_dpi_to_mm(dpi, info->prefy);
  352. }
  353. generate_timings(&timings, refresh_rate, info->prefx, info->prefy);
  354. if (info->prefx >= 4096 || info->prefy >= 4096 || timings.clock >= 65536) {
  355. large_screen = 1;
  356. }
  357. /* =============== extensions =============== */
  358. if (size >= 256) {
  359. dta = edid + 128;
  360. edid[126]++;
  361. edid_ext_dta(dta);
  362. }
  363. if (size >= 384 && large_screen) {
  364. did = edid + 256;
  365. edid[126]++;
  366. init_displayid(did);
  367. }
  368. /* =============== header information =============== */
  369. /* fixed */
  370. edid[0] = 0x00;
  371. edid[1] = 0xff;
  372. edid[2] = 0xff;
  373. edid[3] = 0xff;
  374. edid[4] = 0xff;
  375. edid[5] = 0xff;
  376. edid[6] = 0xff;
  377. edid[7] = 0x00;
  378. /* manufacturer id, product code, serial number */
  379. uint16_t vendor_id = ((((info->vendor[0] - '@') & 0x1f) << 10) |
  380. (((info->vendor[1] - '@') & 0x1f) << 5) |
  381. (((info->vendor[2] - '@') & 0x1f) << 0));
  382. uint16_t model_nr = 0x1234;
  383. uint32_t serial_nr = info->serial ? atoi(info->serial) : 0;
  384. stw_be_p(edid + 8, vendor_id);
  385. stw_le_p(edid + 10, model_nr);
  386. stl_le_p(edid + 12, serial_nr);
  387. /* manufacture week and year */
  388. edid[16] = 42;
  389. edid[17] = 2014 - 1990;
  390. /* edid version */
  391. edid[18] = 1;
  392. edid[19] = 4;
  393. /* =============== basic display parameters =============== */
  394. /* video input: digital, 8bpc, displayport */
  395. edid[20] = 0xa5;
  396. /* screen size: undefined */
  397. edid[21] = width_mm / 10;
  398. edid[22] = height_mm / 10;
  399. /* display gamma: 2.2 */
  400. edid[23] = 220 - 100;
  401. /* supported features bitmap: std sRGB, preferred timing */
  402. edid[24] = 0x06;
  403. /* =============== chromaticity coordinates =============== */
  404. /* standard sRGB colorspace */
  405. edid_colorspace(edid,
  406. 0.6400, 0.3300, /* red */
  407. 0.3000, 0.6000, /* green */
  408. 0.1500, 0.0600, /* blue */
  409. 0.3127, 0.3290); /* white point */
  410. /* =============== established timing bitmap =============== */
  411. /* =============== standard timing information =============== */
  412. /* both filled by edid_fill_modes() */
  413. /* =============== descriptor blocks =============== */
  414. if (!large_screen) {
  415. /* The DTD section has only 12 bits to store the resolution */
  416. edid_desc_timing(desc, &timings, info->prefx, info->prefy,
  417. width_mm, height_mm);
  418. desc = edid_desc_next(edid, dta, desc);
  419. }
  420. xtra3 = desc;
  421. edid_desc_xtra3_std(xtra3);
  422. desc = edid_desc_next(edid, dta, desc);
  423. edid_fill_modes(edid, xtra3, dta, info->maxx, info->maxy);
  424. /*
  425. * dta video data block is finished at thus point,
  426. * so dta descriptor offsets don't move any more.
  427. */
  428. edid_desc_ranges(desc);
  429. desc = edid_desc_next(edid, dta, desc);
  430. if (desc && info->name) {
  431. edid_desc_text(desc, 0xfc, info->name);
  432. desc = edid_desc_next(edid, dta, desc);
  433. }
  434. if (desc && info->serial) {
  435. edid_desc_text(desc, 0xff, info->serial);
  436. desc = edid_desc_next(edid, dta, desc);
  437. }
  438. while (desc) {
  439. edid_desc_dummy(desc);
  440. desc = edid_desc_next(edid, dta, desc);
  441. }
  442. /* =============== display id extensions =============== */
  443. if (did && large_screen) {
  444. qemu_displayid_generate(did, &timings, info->prefx, info->prefy,
  445. width_mm, height_mm);
  446. }
  447. /* =============== finish up =============== */
  448. edid_checksum(edid, 127);
  449. if (dta) {
  450. edid_checksum(dta, 127);
  451. }
  452. if (did) {
  453. edid_checksum(did, 127);
  454. }
  455. }
  456. size_t qemu_edid_size(uint8_t *edid)
  457. {
  458. uint32_t exts;
  459. if (edid[0] != 0x00 ||
  460. edid[1] != 0xff) {
  461. /* doesn't look like a valid edid block */
  462. return 0;
  463. }
  464. exts = edid[126];
  465. return 128 * (exts + 1);
  466. }