cirrus_mode.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. * Copyright 2012 Red Hat
  3. *
  4. * This file is subject to the terms and conditions of the GNU General
  5. * Public License version 2. See the file COPYING in the main
  6. * directory of this archive for more details.
  7. *
  8. * Authors: Matthew Garrett
  9. * Dave Airlie
  10. *
  11. * Portions of this code derived from cirrusfb.c:
  12. * drivers/video/cirrusfb.c - driver for Cirrus Logic chipsets
  13. *
  14. * Copyright 1999-2001 Jeff Garzik <jgarzik@pobox.com>
  15. */
  16. #include <drm/drmP.h>
  17. #include <drm/drm_crtc_helper.h>
  18. #include <drm/drm_plane_helper.h>
  19. #include <video/cirrus.h>
  20. #include "cirrus_drv.h"
  21. #define CIRRUS_LUT_SIZE 256
  22. #define PALETTE_INDEX 0x8
  23. #define PALETTE_DATA 0x9
  24. /*
  25. * This file contains setup code for the CRTC.
  26. */
  27. static void cirrus_crtc_load_lut(struct drm_crtc *crtc)
  28. {
  29. struct cirrus_crtc *cirrus_crtc = to_cirrus_crtc(crtc);
  30. struct drm_device *dev = crtc->dev;
  31. struct cirrus_device *cdev = dev->dev_private;
  32. int i;
  33. if (!crtc->enabled)
  34. return;
  35. for (i = 0; i < CIRRUS_LUT_SIZE; i++) {
  36. /* VGA registers */
  37. WREG8(PALETTE_INDEX, i);
  38. WREG8(PALETTE_DATA, cirrus_crtc->lut_r[i]);
  39. WREG8(PALETTE_DATA, cirrus_crtc->lut_g[i]);
  40. WREG8(PALETTE_DATA, cirrus_crtc->lut_b[i]);
  41. }
  42. }
  43. /*
  44. * The DRM core requires DPMS functions, but they make little sense in our
  45. * case and so are just stubs
  46. */
  47. static void cirrus_crtc_dpms(struct drm_crtc *crtc, int mode)
  48. {
  49. struct drm_device *dev = crtc->dev;
  50. struct cirrus_device *cdev = dev->dev_private;
  51. u8 sr01, gr0e;
  52. switch (mode) {
  53. case DRM_MODE_DPMS_ON:
  54. sr01 = 0x00;
  55. gr0e = 0x00;
  56. break;
  57. case DRM_MODE_DPMS_STANDBY:
  58. sr01 = 0x20;
  59. gr0e = 0x02;
  60. break;
  61. case DRM_MODE_DPMS_SUSPEND:
  62. sr01 = 0x20;
  63. gr0e = 0x04;
  64. break;
  65. case DRM_MODE_DPMS_OFF:
  66. sr01 = 0x20;
  67. gr0e = 0x06;
  68. break;
  69. default:
  70. return;
  71. }
  72. WREG8(SEQ_INDEX, 0x1);
  73. sr01 |= RREG8(SEQ_DATA) & ~0x20;
  74. WREG_SEQ(0x1, sr01);
  75. WREG8(GFX_INDEX, 0xe);
  76. gr0e |= RREG8(GFX_DATA) & ~0x06;
  77. WREG_GFX(0xe, gr0e);
  78. }
  79. /*
  80. * The core passes the desired mode to the CRTC code to see whether any
  81. * CRTC-specific modifications need to be made to it. We're in a position
  82. * to just pass that straight through, so this does nothing
  83. */
  84. static bool cirrus_crtc_mode_fixup(struct drm_crtc *crtc,
  85. const struct drm_display_mode *mode,
  86. struct drm_display_mode *adjusted_mode)
  87. {
  88. return true;
  89. }
  90. static void cirrus_set_start_address(struct drm_crtc *crtc, unsigned offset)
  91. {
  92. struct cirrus_device *cdev = crtc->dev->dev_private;
  93. u32 addr;
  94. u8 tmp;
  95. addr = offset >> 2;
  96. WREG_CRT(0x0c, (u8)((addr >> 8) & 0xff));
  97. WREG_CRT(0x0d, (u8)(addr & 0xff));
  98. WREG8(CRT_INDEX, 0x1b);
  99. tmp = RREG8(CRT_DATA);
  100. tmp &= 0xf2;
  101. tmp |= (addr >> 16) & 0x01;
  102. tmp |= (addr >> 15) & 0x0c;
  103. WREG_CRT(0x1b, tmp);
  104. WREG8(CRT_INDEX, 0x1d);
  105. tmp = RREG8(CRT_DATA);
  106. tmp &= 0x7f;
  107. tmp |= (addr >> 12) & 0x80;
  108. WREG_CRT(0x1d, tmp);
  109. }
  110. /* cirrus is different - we will force move buffers out of VRAM */
  111. static int cirrus_crtc_do_set_base(struct drm_crtc *crtc,
  112. struct drm_framebuffer *fb,
  113. int x, int y, int atomic)
  114. {
  115. struct cirrus_device *cdev = crtc->dev->dev_private;
  116. struct drm_gem_object *obj;
  117. struct cirrus_framebuffer *cirrus_fb;
  118. struct cirrus_bo *bo;
  119. int ret;
  120. u64 gpu_addr;
  121. /* push the previous fb to system ram */
  122. if (!atomic && fb) {
  123. cirrus_fb = to_cirrus_framebuffer(fb);
  124. obj = cirrus_fb->obj;
  125. bo = gem_to_cirrus_bo(obj);
  126. ret = cirrus_bo_reserve(bo, false);
  127. if (ret)
  128. return ret;
  129. cirrus_bo_push_sysram(bo);
  130. cirrus_bo_unreserve(bo);
  131. }
  132. cirrus_fb = to_cirrus_framebuffer(crtc->primary->fb);
  133. obj = cirrus_fb->obj;
  134. bo = gem_to_cirrus_bo(obj);
  135. ret = cirrus_bo_reserve(bo, false);
  136. if (ret)
  137. return ret;
  138. ret = cirrus_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
  139. if (ret) {
  140. cirrus_bo_unreserve(bo);
  141. return ret;
  142. }
  143. if (&cdev->mode_info.gfbdev->gfb == cirrus_fb) {
  144. /* if pushing console in kmap it */
  145. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
  146. if (ret)
  147. DRM_ERROR("failed to kmap fbcon\n");
  148. }
  149. cirrus_bo_unreserve(bo);
  150. cirrus_set_start_address(crtc, (u32)gpu_addr);
  151. return 0;
  152. }
  153. static int cirrus_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  154. struct drm_framebuffer *old_fb)
  155. {
  156. return cirrus_crtc_do_set_base(crtc, old_fb, x, y, 0);
  157. }
  158. /*
  159. * The meat of this driver. The core passes us a mode and we have to program
  160. * it. The modesetting here is the bare minimum required to satisfy the qemu
  161. * emulation of this hardware, and running this against a real device is
  162. * likely to result in an inadequately programmed mode. We've already had
  163. * the opportunity to modify the mode, so whatever we receive here should
  164. * be something that can be correctly programmed and displayed
  165. */
  166. static int cirrus_crtc_mode_set(struct drm_crtc *crtc,
  167. struct drm_display_mode *mode,
  168. struct drm_display_mode *adjusted_mode,
  169. int x, int y, struct drm_framebuffer *old_fb)
  170. {
  171. struct drm_device *dev = crtc->dev;
  172. struct cirrus_device *cdev = dev->dev_private;
  173. int hsyncstart, hsyncend, htotal, hdispend;
  174. int vtotal, vdispend;
  175. int tmp;
  176. int sr07 = 0, hdr = 0;
  177. htotal = mode->htotal / 8;
  178. hsyncend = mode->hsync_end / 8;
  179. hsyncstart = mode->hsync_start / 8;
  180. hdispend = mode->hdisplay / 8;
  181. vtotal = mode->vtotal;
  182. vdispend = mode->vdisplay;
  183. vdispend -= 1;
  184. vtotal -= 2;
  185. htotal -= 5;
  186. hdispend -= 1;
  187. hsyncstart += 1;
  188. hsyncend += 1;
  189. WREG_CRT(VGA_CRTC_V_SYNC_END, 0x20);
  190. WREG_CRT(VGA_CRTC_H_TOTAL, htotal);
  191. WREG_CRT(VGA_CRTC_H_DISP, hdispend);
  192. WREG_CRT(VGA_CRTC_H_SYNC_START, hsyncstart);
  193. WREG_CRT(VGA_CRTC_H_SYNC_END, hsyncend);
  194. WREG_CRT(VGA_CRTC_V_TOTAL, vtotal & 0xff);
  195. WREG_CRT(VGA_CRTC_V_DISP_END, vdispend & 0xff);
  196. tmp = 0x40;
  197. if ((vdispend + 1) & 512)
  198. tmp |= 0x20;
  199. WREG_CRT(VGA_CRTC_MAX_SCAN, tmp);
  200. /*
  201. * Overflow bits for values that don't fit in the standard registers
  202. */
  203. tmp = 16;
  204. if (vtotal & 256)
  205. tmp |= 1;
  206. if (vdispend & 256)
  207. tmp |= 2;
  208. if ((vdispend + 1) & 256)
  209. tmp |= 8;
  210. if (vtotal & 512)
  211. tmp |= 32;
  212. if (vdispend & 512)
  213. tmp |= 64;
  214. WREG_CRT(VGA_CRTC_OVERFLOW, tmp);
  215. tmp = 0;
  216. /* More overflow bits */
  217. if ((htotal + 5) & 64)
  218. tmp |= 16;
  219. if ((htotal + 5) & 128)
  220. tmp |= 32;
  221. if (vtotal & 256)
  222. tmp |= 64;
  223. if (vtotal & 512)
  224. tmp |= 128;
  225. WREG_CRT(CL_CRT1A, tmp);
  226. /* Disable Hercules/CGA compatibility */
  227. WREG_CRT(VGA_CRTC_MODE, 0x03);
  228. WREG8(SEQ_INDEX, 0x7);
  229. sr07 = RREG8(SEQ_DATA);
  230. sr07 &= 0xe0;
  231. hdr = 0;
  232. switch (crtc->primary->fb->bits_per_pixel) {
  233. case 8:
  234. sr07 |= 0x11;
  235. break;
  236. case 16:
  237. sr07 |= 0x17;
  238. hdr = 0xc1;
  239. break;
  240. case 24:
  241. sr07 |= 0x15;
  242. hdr = 0xc5;
  243. break;
  244. case 32:
  245. sr07 |= 0x19;
  246. hdr = 0xc5;
  247. break;
  248. default:
  249. return -1;
  250. }
  251. WREG_SEQ(0x7, sr07);
  252. /* Program the pitch */
  253. tmp = crtc->primary->fb->pitches[0] / 8;
  254. WREG_CRT(VGA_CRTC_OFFSET, tmp);
  255. /* Enable extended blanking and pitch bits, and enable full memory */
  256. tmp = 0x22;
  257. tmp |= (crtc->primary->fb->pitches[0] >> 7) & 0x10;
  258. tmp |= (crtc->primary->fb->pitches[0] >> 6) & 0x40;
  259. WREG_CRT(0x1b, tmp);
  260. /* Enable high-colour modes */
  261. WREG_GFX(VGA_GFX_MODE, 0x40);
  262. /* And set graphics mode */
  263. WREG_GFX(VGA_GFX_MISC, 0x01);
  264. WREG_HDR(hdr);
  265. cirrus_crtc_do_set_base(crtc, old_fb, x, y, 0);
  266. /* Unblank (needed on S3 resume, vgabios doesn't do it then) */
  267. outb(0x20, 0x3c0);
  268. return 0;
  269. }
  270. /*
  271. * This is called before a mode is programmed. A typical use might be to
  272. * enable DPMS during the programming to avoid seeing intermediate stages,
  273. * but that's not relevant to us
  274. */
  275. static void cirrus_crtc_prepare(struct drm_crtc *crtc)
  276. {
  277. }
  278. /*
  279. * This is called after a mode is programmed. It should reverse anything done
  280. * by the prepare function
  281. */
  282. static void cirrus_crtc_commit(struct drm_crtc *crtc)
  283. {
  284. }
  285. /*
  286. * The core can pass us a set of gamma values to program. We actually only
  287. * use this for 8-bit mode so can't perform smooth fades on deeper modes,
  288. * but it's a requirement that we provide the function
  289. */
  290. static void cirrus_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  291. u16 *blue, uint32_t start, uint32_t size)
  292. {
  293. struct cirrus_crtc *cirrus_crtc = to_cirrus_crtc(crtc);
  294. int i;
  295. if (size != CIRRUS_LUT_SIZE)
  296. return;
  297. for (i = 0; i < CIRRUS_LUT_SIZE; i++) {
  298. cirrus_crtc->lut_r[i] = red[i];
  299. cirrus_crtc->lut_g[i] = green[i];
  300. cirrus_crtc->lut_b[i] = blue[i];
  301. }
  302. cirrus_crtc_load_lut(crtc);
  303. }
  304. /* Simple cleanup function */
  305. static void cirrus_crtc_destroy(struct drm_crtc *crtc)
  306. {
  307. struct cirrus_crtc *cirrus_crtc = to_cirrus_crtc(crtc);
  308. drm_crtc_cleanup(crtc);
  309. kfree(cirrus_crtc);
  310. }
  311. /* These provide the minimum set of functions required to handle a CRTC */
  312. static const struct drm_crtc_funcs cirrus_crtc_funcs = {
  313. .gamma_set = cirrus_crtc_gamma_set,
  314. .set_config = drm_crtc_helper_set_config,
  315. .destroy = cirrus_crtc_destroy,
  316. };
  317. static const struct drm_crtc_helper_funcs cirrus_helper_funcs = {
  318. .dpms = cirrus_crtc_dpms,
  319. .mode_fixup = cirrus_crtc_mode_fixup,
  320. .mode_set = cirrus_crtc_mode_set,
  321. .mode_set_base = cirrus_crtc_mode_set_base,
  322. .prepare = cirrus_crtc_prepare,
  323. .commit = cirrus_crtc_commit,
  324. .load_lut = cirrus_crtc_load_lut,
  325. };
  326. /* CRTC setup */
  327. static void cirrus_crtc_init(struct drm_device *dev)
  328. {
  329. struct cirrus_device *cdev = dev->dev_private;
  330. struct cirrus_crtc *cirrus_crtc;
  331. int i;
  332. cirrus_crtc = kzalloc(sizeof(struct cirrus_crtc) +
  333. (CIRRUSFB_CONN_LIMIT * sizeof(struct drm_connector *)),
  334. GFP_KERNEL);
  335. if (cirrus_crtc == NULL)
  336. return;
  337. drm_crtc_init(dev, &cirrus_crtc->base, &cirrus_crtc_funcs);
  338. drm_mode_crtc_set_gamma_size(&cirrus_crtc->base, CIRRUS_LUT_SIZE);
  339. cdev->mode_info.crtc = cirrus_crtc;
  340. for (i = 0; i < CIRRUS_LUT_SIZE; i++) {
  341. cirrus_crtc->lut_r[i] = i;
  342. cirrus_crtc->lut_g[i] = i;
  343. cirrus_crtc->lut_b[i] = i;
  344. }
  345. drm_crtc_helper_add(&cirrus_crtc->base, &cirrus_helper_funcs);
  346. }
  347. /** Sets the color ramps on behalf of fbcon */
  348. void cirrus_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  349. u16 blue, int regno)
  350. {
  351. struct cirrus_crtc *cirrus_crtc = to_cirrus_crtc(crtc);
  352. cirrus_crtc->lut_r[regno] = red;
  353. cirrus_crtc->lut_g[regno] = green;
  354. cirrus_crtc->lut_b[regno] = blue;
  355. }
  356. /** Gets the color ramps on behalf of fbcon */
  357. void cirrus_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  358. u16 *blue, int regno)
  359. {
  360. struct cirrus_crtc *cirrus_crtc = to_cirrus_crtc(crtc);
  361. *red = cirrus_crtc->lut_r[regno];
  362. *green = cirrus_crtc->lut_g[regno];
  363. *blue = cirrus_crtc->lut_b[regno];
  364. }
  365. static bool cirrus_encoder_mode_fixup(struct drm_encoder *encoder,
  366. const struct drm_display_mode *mode,
  367. struct drm_display_mode *adjusted_mode)
  368. {
  369. return true;
  370. }
  371. static void cirrus_encoder_mode_set(struct drm_encoder *encoder,
  372. struct drm_display_mode *mode,
  373. struct drm_display_mode *adjusted_mode)
  374. {
  375. }
  376. static void cirrus_encoder_dpms(struct drm_encoder *encoder, int state)
  377. {
  378. return;
  379. }
  380. static void cirrus_encoder_prepare(struct drm_encoder *encoder)
  381. {
  382. }
  383. static void cirrus_encoder_commit(struct drm_encoder *encoder)
  384. {
  385. }
  386. static void cirrus_encoder_destroy(struct drm_encoder *encoder)
  387. {
  388. struct cirrus_encoder *cirrus_encoder = to_cirrus_encoder(encoder);
  389. drm_encoder_cleanup(encoder);
  390. kfree(cirrus_encoder);
  391. }
  392. static const struct drm_encoder_helper_funcs cirrus_encoder_helper_funcs = {
  393. .dpms = cirrus_encoder_dpms,
  394. .mode_fixup = cirrus_encoder_mode_fixup,
  395. .mode_set = cirrus_encoder_mode_set,
  396. .prepare = cirrus_encoder_prepare,
  397. .commit = cirrus_encoder_commit,
  398. };
  399. static const struct drm_encoder_funcs cirrus_encoder_encoder_funcs = {
  400. .destroy = cirrus_encoder_destroy,
  401. };
  402. static struct drm_encoder *cirrus_encoder_init(struct drm_device *dev)
  403. {
  404. struct drm_encoder *encoder;
  405. struct cirrus_encoder *cirrus_encoder;
  406. cirrus_encoder = kzalloc(sizeof(struct cirrus_encoder), GFP_KERNEL);
  407. if (!cirrus_encoder)
  408. return NULL;
  409. encoder = &cirrus_encoder->base;
  410. encoder->possible_crtcs = 0x1;
  411. drm_encoder_init(dev, encoder, &cirrus_encoder_encoder_funcs,
  412. DRM_MODE_ENCODER_DAC);
  413. drm_encoder_helper_add(encoder, &cirrus_encoder_helper_funcs);
  414. return encoder;
  415. }
  416. static int cirrus_vga_get_modes(struct drm_connector *connector)
  417. {
  418. int count;
  419. /* Just add a static list of modes */
  420. if (cirrus_bpp <= 24) {
  421. count = drm_add_modes_noedid(connector, 1280, 1024);
  422. drm_set_preferred_mode(connector, 1024, 768);
  423. } else {
  424. count = drm_add_modes_noedid(connector, 800, 600);
  425. drm_set_preferred_mode(connector, 800, 600);
  426. }
  427. return count;
  428. }
  429. static struct drm_encoder *cirrus_connector_best_encoder(struct drm_connector
  430. *connector)
  431. {
  432. int enc_id = connector->encoder_ids[0];
  433. /* pick the encoder ids */
  434. if (enc_id)
  435. return drm_encoder_find(connector->dev, enc_id);
  436. return NULL;
  437. }
  438. static enum drm_connector_status cirrus_vga_detect(struct drm_connector
  439. *connector, bool force)
  440. {
  441. return connector_status_connected;
  442. }
  443. static void cirrus_connector_destroy(struct drm_connector *connector)
  444. {
  445. drm_connector_cleanup(connector);
  446. kfree(connector);
  447. }
  448. struct drm_connector_helper_funcs cirrus_vga_connector_helper_funcs = {
  449. .get_modes = cirrus_vga_get_modes,
  450. .best_encoder = cirrus_connector_best_encoder,
  451. };
  452. struct drm_connector_funcs cirrus_vga_connector_funcs = {
  453. .dpms = drm_helper_connector_dpms,
  454. .detect = cirrus_vga_detect,
  455. .fill_modes = drm_helper_probe_single_connector_modes,
  456. .destroy = cirrus_connector_destroy,
  457. };
  458. static struct drm_connector *cirrus_vga_init(struct drm_device *dev)
  459. {
  460. struct drm_connector *connector;
  461. struct cirrus_connector *cirrus_connector;
  462. cirrus_connector = kzalloc(sizeof(struct cirrus_connector), GFP_KERNEL);
  463. if (!cirrus_connector)
  464. return NULL;
  465. connector = &cirrus_connector->base;
  466. drm_connector_init(dev, connector,
  467. &cirrus_vga_connector_funcs, DRM_MODE_CONNECTOR_VGA);
  468. drm_connector_helper_add(connector, &cirrus_vga_connector_helper_funcs);
  469. drm_connector_register(connector);
  470. return connector;
  471. }
  472. int cirrus_modeset_init(struct cirrus_device *cdev)
  473. {
  474. struct drm_encoder *encoder;
  475. struct drm_connector *connector;
  476. int ret;
  477. drm_mode_config_init(cdev->dev);
  478. cdev->mode_info.mode_config_initialized = true;
  479. cdev->dev->mode_config.max_width = CIRRUS_MAX_FB_WIDTH;
  480. cdev->dev->mode_config.max_height = CIRRUS_MAX_FB_HEIGHT;
  481. cdev->dev->mode_config.fb_base = cdev->mc.vram_base;
  482. cdev->dev->mode_config.preferred_depth = 24;
  483. /* don't prefer a shadow on virt GPU */
  484. cdev->dev->mode_config.prefer_shadow = 0;
  485. cirrus_crtc_init(cdev->dev);
  486. encoder = cirrus_encoder_init(cdev->dev);
  487. if (!encoder) {
  488. DRM_ERROR("cirrus_encoder_init failed\n");
  489. return -1;
  490. }
  491. connector = cirrus_vga_init(cdev->dev);
  492. if (!connector) {
  493. DRM_ERROR("cirrus_vga_init failed\n");
  494. return -1;
  495. }
  496. drm_mode_connector_attach_encoder(connector, encoder);
  497. ret = cirrus_fbdev_init(cdev);
  498. if (ret) {
  499. DRM_ERROR("cirrus_fbdev_init failed\n");
  500. return ret;
  501. }
  502. return 0;
  503. }
  504. void cirrus_modeset_fini(struct cirrus_device *cdev)
  505. {
  506. cirrus_fbdev_fini(cdev);
  507. if (cdev->mode_info.mode_config_initialized) {
  508. drm_mode_config_cleanup(cdev->dev);
  509. cdev->mode_info.mode_config_initialized = false;
  510. }
  511. }